Making Classes header files.

This is a discussion on Making Classes header files. within the C++ Programming forums, part of the General Programming Boards category; Is it good practice to make a class in a different program then save it as a .h or .cpp ...

  1. #1
    1479
    Join Date
    Aug 2003
    Posts
    253

    Making Classes header files.

    Is it good practice to make a class in a different program then save it as a .h or .cpp and then use it in a header file for another program. I am not sure about this. In Sams book(24 hours) he says that it is good practice and I can see why. It would make the actuall code a lot shorter. But is this customary?
    Knowledge is power and I want it all

    -0RealityFusion0-

  2. #2
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    Personally, I use that method often, and I like it. Mostly, I create a wrapper class for a control (Win32 API), with the declarations in a .h file, and the definitions in a .cpp file. That way, you just include the header file in each of your other source files, like this:

    Code:
    //clsTree.h
    class clsTree
    {
    public:
    HWND hTree;
    void DoStuff();
    };
    Code:
    //clsTree.cpp
    #include "clsTree.h"
    void clsTree::DoStuff()
    {
    //code
    }
    Code:
    //other source file
    #include "clsTree.h"
    fooblah
    Or, you could create a dynamic link library. More complicated though.
    Last edited by bennyandthejets; 08-22-2003 at 01:52 AM.
    benforbes@optusnet.com.au
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

  3. #3
    Me -=SoKrA=-'s Avatar
    Join Date
    Oct 2002
    Location
    Europe
    Posts
    448
    Actually in the sources you should include the .cpp file. Otherwise you don't include the definitions of the functions and the coputer wouldn't know what to do.
    SoKrA-BTS "Judge not the program I made, but the one I've yet to code"
    I say what I say, I mean what I mean.
    IDE: emacs + make + gcc and proud of it.

  4. #4
    *******argv[] - hu? darksaidin's Avatar
    Join Date
    Jul 2003
    Posts
    314
    Originally posted by -=SoKrA=-
    Actually in the sources you should include the .cpp file. Otherwise you don't include the definitions of the functions and the coputer wouldn't know what to do.
    Nope, you include the h file so the "names" (classes, defs, types...) declared in there become valid. Then you add the c(**) files to make's list of stuff that needs to be compiled.

  5. #5
    Me -=SoKrA=-'s Avatar
    Join Date
    Oct 2002
    Location
    Europe
    Posts
    448
    >>Nope, you include the h file so the "names"
    >>(classes, defs, types...) declared in there become valid.
    Are you talking about the definition file or the sources where the program is?
    What I meant was that when bennyandthejets put in the other sources the .h file, the declarations wouldn't get included. That way, since the .cpp file already includes the definitions file, you should include the .cpp file.

    It looks to me that you're talking about the program make. I'm talking about only using the compiler.
    SoKrA-BTS "Judge not the program I made, but the one I've yet to code"
    I say what I say, I mean what I mean.
    IDE: emacs + make + gcc and proud of it.

  6. #6
    *******argv[] - hu? darksaidin's Avatar
    Join Date
    Jul 2003
    Posts
    314
    Originally posted by -=SoKrA=-
    >>Nope, you include the h file so the "names"
    >>(classes, defs, types...) declared in there become valid.
    Are you talking about the definition file or the sources where the program is?
    What I meant was that when bennyandthejets put in the other sources the .h file, the declarations wouldn't get included. That way, since the .cpp file already includes the definitions file, you should include the .cpp file.

    It looks to me that you're talking about the program make. I'm talking about only using the compiler.
    Well, I think we were talking about "good practice". If you ask me, it's good practice not to use #include on non header files, but thats of course just me.
    However, you are right, the cpp files won't get included if you only #include the .h file. Thats why I said it would be a good idea to add them (the .cpp's) to the list of stuff that needs to be compiled (and linked).

    The reason is, that once you have your cpp's compiled to a nice handy .lib, your code that uses these cpps won't compile anymore if you used include on cpps.
    Last edited by darksaidin; 08-23-2003 at 02:26 AM.

  7. #7
    Cat
    Cat is offline
    Registered User
    Join Date
    May 2003
    Posts
    1,571
    You do NOT want to #include a .cpp file. The compiler doesn't need to have the function definitions, only the declarations. So you should #include the .h file and compile, then compile the .cpp file, and link the two together (along with any other .cpp files).

  8. #8
    *******argv[] - hu? darksaidin's Avatar
    Join Date
    Jul 2003
    Posts
    314
    You see, Sokra ? Whatever Cat says is true

  9. #9
    Me -=SoKrA=-'s Avatar
    Join Date
    Oct 2002
    Location
    Europe
    Posts
    448
    Well, I can see you're all aginst me
    I'm in a slightly different mindset. I've always been using command line and, being very lazy, just included everything into a main file and just told the compiler to compile and link that.
    When I made the first post I wasn't thinking that the deffinitions file would also get compiled and that's why I said that the .cpp files would need to get included.
    I'll try to think about that next time
    SoKrA-BTS "Judge not the program I made, but the one I've yet to code"
    I say what I say, I mean what I mean.
    IDE: emacs + make + gcc and proud of it.

  10. #10
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    So, at the end of all this, was I right? I mean, that method always works for me.
    benforbes@optusnet.com.au
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

  11. #11
    Registered User
    Join Date
    Nov 2002
    Posts
    126
    Is it good practice to make a class in a different program then save it as a .h or .cpp and then use it in a header file for another program
    Yes, reusing classes is a good practice. That's why it's good to keep reusability in mind when you are designing a class. Reusable classes can prove to be huge time savers.
    So, at the end of all this, was I right? I mean, that method always works for me.
    Yes, as long as the object code defining the class's members is linked against the other program code, as Cat said.

  12. #12
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    There are very good reasons for only including header files (with the interface only). Whenever you modify a file, you will have to recompile every other file that depends upon that one. Often, your class interfaces will be well thought out, and so the interface (header file) is fairly unlikely to change. The implementation, however, is quite likely to change as algorithms are modified, or bugs are fixed. Now everything calling the class needs only to know how to call it (the declarations which have not changed), so changing the implementation will not force a recompile of your entire project. Rebuilding a single object file and then relinking your program (for very large programs) can significantly increase the speed of your rebuilds.
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  13. #13
    Registered User
    Join Date
    Jul 2003
    Posts
    450
    Thanks for the info, I am just covering this in Deitel & Deitel.

  14. #14
    Cat
    Cat is offline
    Registered User
    Join Date
    May 2003
    Posts
    1,571
    In general, the best approach is always "include as little as you can get away with". If you don't need a definition, use only a declaration. And you'd be surprised at how rarely you need a definition. For example, if you had this in a header file:

    Code:
    class Teenager: public Person{
    public:
      Teenager(Mother &m, Father &f);
      
      Mother GetMother();
      Father GetFather();
      
      void IgnoreParents();
      void BeMoody();
    
    private:
      Mother & m_mom;
      Father * m_dad;
    };
    In the above class definition, ONLY "Person" must be defined. Mother and Father must only be declared. So you can get away with merely this in that header:

    Code:
    #include "Person.h" // Can't get around this include
    
    // But we don't need to include "Mother.h" or "Father.h"
    // This works equally well:
    class Mother;
    class Father;

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Checking array for string
    By Ayreon in forum C Programming
    Replies: 87
    Last Post: 03-09-2009, 03:25 PM
  2. Header Files
    By Volair in forum C Programming
    Replies: 2
    Last Post: 12-09-2005, 09:51 AM
  3. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 09:33 AM
  4. doin' classes in header files?
    By face_master in forum C++ Programming
    Replies: 9
    Last Post: 11-14-2001, 02:56 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21