Thread: Can someone explain headers to me better

  1. #1
    Unleashed
    Join Date
    Sep 2001
    Posts
    1,765

    Can someone explain headers to me better

    Please review the program attached.
    It's a small database program just taking off.
    My question though, is about header files.
    I still haven't quite understood them yet.
    When you make a modular program, and all these different functions require this or that, I get confused.
    Will someone use the source code I provided to explain headers to me?
    I'm mainly concerned with how to include what, and where!?

    Thanks.
    The world is waiting. I must leave you now.

  2. #2
    Registered User xds4lx's Avatar
    Join Date
    Nov 2001
    Posts
    630
    First thing I saw is that you dont use any include guards. In every header you should have a #define that keeps the header from being re-opened by the compiler over and over. For your file
    Data.h do something like:

    [source]
    #ifndef(__DATA_H__)
    #define(__DATA_H__)
    .....
    .....
    #endif
    [/source]

    Make sure you put the #endif after all the prototypes.
    "only two things are infinite, the universe and human stupidity, and im not sure about the former." - albert einstein

  3. #3
    Seeking motivation... endo's Avatar
    Join Date
    May 2002
    Posts
    537
    header files are used to hold all your declarations and stuff. It is typical to define a class in a header file, then define all its functions and static members in a similarly-named .cpp file. This way if you want to use the class again you can simply copy the 2 files to your next project. Also if someone wants to know how to use your class they can see all the available functions in the header file, take a look at math.h for a good example.

    Hope that helps a little...
    Couldn't think of anything interesting, cool or funny - sorry.

  4. #4
    Unleashed
    Join Date
    Sep 2001
    Posts
    1,765
    Well, I mean I understand the basics of it all.
    However, when you start to get the ball rolling on a several file modular program, it gets confusing.

    Take the program I posted for example.
    I have a few Windows dependant functions in it.
    While they can be removed easily, I only need windows.h for that one source file.
    That same source file, appropriately tittled Misc.cpp (for the time being) has miscellaneous functions in it.
    Some require iostream while only 1 or 2 require time.h and windows.h.

    I get confused on how to piece it all together so each file gets only what it needs. Misc.cpp is the only file that needs time.h and windows.h While all need iostream. Then, you want a header file to try to localize everything ..and I just get confused with it all when it starts to grow.

    I hear Salem explains header files excellently.

    Footnote:
    Now that I look at the code, I can make it easier by removing the Print function from Misc.cpp because then, if I am correct, Misc.cpp will no longer need iostream as it's all time.h and windows.h stuff.
    The world is waiting. I must leave you now.

  5. #5
    Seeking motivation... endo's Avatar
    Join Date
    May 2002
    Posts
    537
    Send Salem a private message and get his opinion, he can probably give you a better answer than me
    Couldn't think of anything interesting, cool or funny - sorry.

  6. #6
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Shadow, I've had a look your source, and rearranged the headers somewhat. Have a look and see what you think.

    I'm not saying what I've done is right, but it's one example of how to use multiple header files.

    To create the program, you'll need to compile the sources, and then link them. With my compiler, I can do this in one command:
    >>bcc32 *.cpp
    ...providing the only source files in the current directory are for this project. You can do the same kind of thing with gcc too.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  7. #7
    Unleashed
    Join Date
    Sep 2001
    Posts
    1,765
    Thanks Hammer.

    I understand compiling & linking . I just get all confused when this requires that, or that requires this, etc.

    I'll have a look, and maybe discuss problems or gaines to look forward to for headers in a PM.

    [EDIT]
    We'll, I've had a look Hammer.

    And, from what I can tell, it's sort of how I predicted it.
    I was just checking to get a second opinion.
    There pretty much is no convienent way to have few header files.

    Thanks for all the input everyone.
    [/EDIT]
    Last edited by Shadow; 08-18-2002 at 10:26 PM.
    The world is waiting. I must leave you now.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. im extreamly new help
    By rigo305 in forum C++ Programming
    Replies: 27
    Last Post: 04-23-2004, 11:22 PM
  2. include question
    By Wanted420 in forum C++ Programming
    Replies: 8
    Last Post: 10-17-2003, 03:49 AM
  3. Headers that use each other
    By nickname_changed in forum C++ Programming
    Replies: 7
    Last Post: 10-03-2003, 04:25 AM
  4. how do you handle all your headers?
    By mart_man00 in forum Linux Programming
    Replies: 0
    Last Post: 06-16-2003, 03:17 PM
  5. Can someone explain "extern" to me?
    By valar_king in forum C++ Programming
    Replies: 3
    Last Post: 09-16-2001, 12:22 AM