Thread: the proper way to make .h/header files

  1. #1
    Registered User
    Join Date
    Dec 2002
    Posts
    221

    the proper way to make .h/header files

    I want to define a class in a .h file then actually write out all the methods in a .cpp file

    my question is... how does one exactly do it

    in your main program do u include the .h
    then in the .h you include the .cpp

    thanks

    ps if anyone has any dope links that goes in depth about it, that'd be great. i tried looking up on google, but all i got were the default header files in c/c++

  2. #2
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,820
    All you do is write you class info in the header file and then you class definitions in the class.cpp and then include the class header in your main.cpp
    e.g.(cause I am nice)
    Code:
    //class.h
    #ifndef class_h
    #define class_h
    
    class test
    {
      public:
        void doStuff();
      private:
        int randomVariable;
    };
    
    #endif//class_h
    Code:
    //class.cpp
    #include "class.h"
    
    void test::doStuff()
    {
      //do Stuff!
    }
    Code:
    #include <iostream>
    #include "class.h"
    
    int main(void)
    {
      test something;
      something.doStuff();
      return 0;
    }
    Woop?

  3. #3
    Registered User
    Join Date
    Aug 2004
    Location
    San Diego, CA
    Posts
    313
    Question: How does the linker know to include <headername>.cpp in the compilation?

  4. #4
    Registered User
    Join Date
    Oct 2004
    Posts
    120
    The makefile tells the linker to link and the compiler what to build. The auto generated makefiles will just compile and link all source files in a given directory.

  5. #5
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    You don't want the CPP put into the compilation. A separate object file will be created for each distinct unit (usually a CPP file and it's associated headers), with references to external symbols. At link time, the object files are combined (linked) and the external symbols resolved (and if they can't be found, you get the infamous "Unresolved External Error").

    That is an essentially right description, which should help understand it.

    The reason you don't want to include CPP files will not come up really until you work on large projects. Basically, if you changed a CPP that was included somewhere, you need to recompile everything that included it. If you include only the header, leave the header (interface) unchanged, and modify the implementation in the CPP, then you need only recompile that unit and then relink.
    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.

  6. #6
    Registered User
    Join Date
    Dec 2002
    Posts
    221
    awesome thanks !

  7. #7
    Registered User
    Join Date
    Sep 2004
    Posts
    719
    Quote Originally Posted by Lithorien
    Question: How does the linker know to include <headername>.cpp in the compilation?
    it doesn't...you put #include "headername.h" in your .cpp file....
    you compile the .cpp file and it includes the .h file.....

    i take it you use and ide compiler....if you were using a command line compiler you would type something like

    g++ main.cpp class1.cpp class2.cpp -o main.exe

  8. #8
    Registered User
    Join Date
    Aug 2004
    Location
    San Diego, CA
    Posts
    313
    I'm using Visual Studio 2003 Toolkit.. a command-line compiler. I've just never used .h/.cpp combos.. I just keep everything in .h files.

  9. #9
    Some Guy
    Join Date
    Jul 2004
    Posts
    32
    I've never understood this...
    Code:
    #ifndef class_h
    #define class_h
    What exactly is it doing? Is it checking if there are other classes with the same name? Secondly, does the name matter?

  10. #10
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    Those are preprocessor directives. They have no meaning during the actual running of the program, they just add a bit more control to the process of compilation. If you're just learning, you'll learn more about those later (assuming you're learning from a fairly good book).

    #ifndef class_h
    #define class_h

    if class_h is not defined, define it. Ideally this block should be followed by, "#endif", and most of the time the #define directive will also have another word after class_h to actually define it AS something.

    edit:

    #include <ABC>/"XYZ" - adds file ABC from the standard library to your program, or file XYZ that you've added yourself

    #ifdef ABC - executes the following directive if ABC has been defined, until it reaches #endif

    #ifndef ABC - same as above, but if the macro is NOT defined

    #endif - See above

    #define ABC XYZ - Replaces all occurences of ABC with XYZ, literally. There are some fancy tricks for using parameters in these defintions, but I'll keep this simple.

    Those are the common ones.
    Last edited by sean; 10-10-2004 at 07:14 PM.

  11. #11
    Some Guy
    Join Date
    Jul 2004
    Posts
    32
    Ok, I sort of understand, but could you give me an example of when 2 files will be defined by the same name? Does this happen often with large projects?

  12. #12
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,820
    No the ifndef is to protect you from double including the header like so.
    Code:
    //test.h
    #ifndef test_h
    #define test_h
    #include "hello.h"
    
    void test(hello hi);
    
    #endif//test_h
    Code:
    //test.cpp
    #include "test.h"
    #include "hello.h"//we included hello.h which test.h already included  
    
    void test(hello hi)
    {
    //stuff
    }
    Code:
    //hello.h
    #ifndef hello_h
    #define hello_h
    
    class hello
    {
      //stuff
    };
    
    #endif
    Basically if you don't have the header guards youll get some errors when you try to compile
    Woop?

  13. #13
    Some Guy
    Join Date
    Jul 2004
    Posts
    32
    Ah, got it. In fact, I think I had that error last week. Thanks.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. trying to make a KenGen ( for a game tool )
    By lonewolfy in forum C# Programming
    Replies: 4
    Last Post: 03-28-2007, 08:23 AM
  2. make variables visible on multiple files
    By jagerhans in forum C++ Programming
    Replies: 4
    Last Post: 08-28-2004, 06:32 PM
  3. "Cannot make pipe"
    By crepincdotcom in forum C Programming
    Replies: 5
    Last Post: 08-16-2004, 12:43 PM
  4. Help with Header/Make files
    By Unregistered in forum C Programming
    Replies: 0
    Last Post: 04-10-2002, 10:57 PM
  5. creating make files
    By Unregistered in forum Linux Programming
    Replies: 4
    Last Post: 09-22-2001, 01:58 AM