• ### Announcements

#### Archived

This topic is now archived and is closed to further replies.

# headers and junk

## Recommended Posts

Someone help me out, this wasnt included in my C++ reference... I want to make a little header file and source file so that i don''t have to put everything in my main source file. So which stuff goes in the .h file, and which stuff goes in the .cpp file, and what do i have to include in the main .cpp file? Help me help me, thanks! ...go on and live with no regrets, you only have one life...

##### Share on other sites
Mullen    122
That depends on your program. Here's an example of a really crappy console program with that type of idea.

  // headerf.h// Header file#include #define PI 3.14159265;double sqrnum(double na);double multpi(double na);// end headerf.h // badprog.cpp// Source program#include "headerf.h"int main() { cout << sqrnum(5.6) << endl; cout << multpi(10.0) << endl; return(0);}double sqrnum(double na) { return(na*na);}double multpi(double na) { return(na*PI);}// end badprog.cpp */

Edited by - Mullen on January 2, 2002 3:48:25 PM

##### Share on other sites
Mullen    122
The truth of the matter is I don''t really understand your question.

As far as I know the preproccessor directive #include includes the file into the program at actually replaces the "#include <" with the file itself... It''s pretty self-explanatory...

##### Share on other sites
Guest Anonymous Poster
Usually you put class definitions, type definitions, function prototypes, #define directives, etc. in the header files and put the actual code for the functions & methods in the .cpp files.

##### Share on other sites
Let me try to restate my problem... right now, I have this huge main.cpp file with all my #includes, #defines, macros, types, globals, prototypes, and functions. I''m sick of having them all in one place so what i want to do is, put them all into another file(s), like library.h|cpp. I want my main.cpp to just contain the the functions, variables, types and such that are specifically for that program, but still be able to call functions that are in the library. What i need to know is what do i put in the library.h, and what do i put in the library.cpp.

Ugh...

...go on and live with no regrets, you only have one life...

##### Share on other sites
*sigh*... thanks anon, i''ll try that

...go on and live with no regrets, you only have one life...

##### Share on other sites
Guest Anonymous Poster
Also it was not directly your question ...

If you''ve made a useful C++ class / class set that has a high level of reuseability. You should make a seperate header ( .h ) file which includes the class declaration, or the declarartions of the class set, and the stuff ( macros, enums, types, typedefs , etc. ) for this particular class / class set and so on, plus an implementation file ( .cpp ) which contains the implementations and only the implementations for that particular class / class set.

If you do it like I described it above, you could integrate that class or class set ( or even function or function set if you like to use free functions too / or only ) very simple into other projects.

I''m doing it like this, and it works very well.

##### Share on other sites
arkule    122
I have a bit of a question of my own to add to this...

In the tutorials i read, they say to put the declarations, and stuff like you said in a .h file, and the definitions in a .cpp file... Why not just put them all in the .h file? And if you do put them in two seperate files, how would you add them to a different program where your main() function is? Do you #include the .cpp file also? Because i didn''t think .cpp files could be #included. I hope you understand where i am going with this, but if not, i have an example, sort of.
Lets say you are making a program with several really big classes. If you give each of the classes their own .h file for declarations and prototypes, and a .cpp file for function and member definitions, how would you include all of these onto the .cpp file that has your main() function? Wouldn''t it be easiest just to put them all in .h files and only have the one .cpp file for the main() function alone?
I hope i didn''t confuse anyone else besides myself with that right there, but i''ve been wondering that.

##### Share on other sites
Guest Anonymous Poster
quote:
Original post by arkule
I have a bit of a question of my own to add to this...

In the tutorials i read, they say to put the declarations, and stuff like you said in a .h file, and the definitions in a .cpp file... Why not just put them all in the .h file? And if you do put them in two seperate files, how would you add them to a different program where your main() function is? Do you #include the .cpp file also? Because i didn''t think .cpp files could be #included. I hope you understand where i am going with this, but if not, i have an example, sort of.
Lets say you are making a program with several really big classes. If you give each of the classes their own .h file for declarations and prototypes, and a .cpp file for function and member definitions, how would you include all of these onto the .cpp file that has your main() function? Wouldn''t it be easiest just to put them all in .h files and only have the one .cpp file for the main() function alone?
I hope i didn''t confuse anyone else besides myself with that right there, but i''ve been wondering that.

Because every .cpp file that includes that header file would then contain the actual code for the functions. This would increase the size of your .exe as you would end up with the same code duplicated several times.

##### Share on other sites
Myopic Rhino    2315
quote:
Original post by arkule
In the tutorials i read, they say to put the declarations, and stuff like you said in a .h file, and the definitions in a .cpp file... Why not just put them all in the .h file?

Lots of reasons. One of the best is that putting everything in the header means that everything will have to be recompiled whenever you make a change.
quote:
Original post by arkule
And if you do put them in two seperate files, how would you add them to a different program where your main() function is?
You just link them in. In VC++, and most IDEs, this is just a matter of adding them to the project. If you have a bunch of files that you use all the time that don''t change much, create a library.
quote:
Original post by arkule
Do you #include the .cpp file also? Because i didn''t think .cpp files could be #included.

#include just tells the preprocessor to go find that file, and insert the contents of it into the current file. It doesn''t matter what extension the filename has. But no, you wouldn''t want to include all of your .cpps this way.

##### Share on other sites
B-Barlow    122
Recall how the whole compile-link process works.

Each .cpp file is compiled individually into .obj files which basically contain machine language code that doesn't have all the external addressing(calls to functions in other .cpp files or libraries, etc..) resolved.

Then all the .obj files are linked which resolves all the addressing and the final .exe is created.

If you call a function that is defined in a different .cpp file without providing a function prototype, the compiler gets pissy because it doesn't know anything at compile time about the function you're calling. That's what all the function prototypes, type definintions, etc. really are, just ways to tell the compiler to quit bitching and that everything will be taken care of at link time. So that's the stuff you put in the .h files. This is an oversimplification, but I hope you get the general idea.

Edited by - B-Barlow on January 2, 2002 6:45:47 PM

##### Share on other sites
Guest Anonymous Poster
I want to add this, because I think the question about including
implementation files, refers to my point of integrating the class / class set or function / function set into other projects.

You have to include the header file ( often .h ) and LINK with the object file ( often .obj ) compiled from the implementation file ( often .cpp or .c in C/C++ progs ).

Or if you want to customize the implementation for your current project and want to have it in your project workspace, you could add the implementaion file to your project ( if you''ve an IDE that supports projects ). Then most IDEs automate the thing and you don''t have to say explicitly to link with that object file.
Also they usually compile the implementation files when you build your project and they aren''t compiled yet.

The rest of the question is explained pretty good, I think, by B_Barlow ( sorry if the name isn''t spelled correct ).

##### Share on other sites
Gaiiden    5710
quote:
Original post by Myopic Rhino
Lots of reasons. One of the best is that putting everything in the header means that everything will have to be recompiled whenever you make a change.

Not entirely true. There is a compiler switch in VC++ that tells the compiler to compile the header files only once, unless a change has been made to one of them. This means that after the first compile, subsequent compiles are much quicker.

However this certainly does justify sticking it all in a header, I''m just pointing out (for general purpose) that you can precompile like that.

_________________________________________________________________

Drew Sikora
A.K.A. Gaiiden

ICQ #: 70449988
AOLIM: DarkPylat

Staff Member, GDNet
Public Relations, Game Institute

3-time Contributing author, Game Design Methods , Charles River Media (coming GDC 2002)
Online column - Design Corner at Pixelate

NJ IGDA Chapter - NJ developers unite!! [Chapter Home | Chapter Forum]

##### Share on other sites
Myopic Rhino    2315
quote:
Original post by Gaiiden
Not entirely true. There is a compiler switch in VC++ that tells the compiler to compile the header files only once, unless a change has been made to one of them. This means that after the first compile, subsequent compiles are much quicker.

Funny. When I wrote that, I thought that someone might bring precompiled headers up, but chose not to mention them because (1) doing so would unnecessarily complicate the explanation, (2) the fact that you can use them doesn''t justify putting everything in the header anyway, and (3) I was speaking in a broader sense - not every compiler supports precompiled headers.