Advertisement

headers and junk

Started by January 02, 2002 02:38 PM
13 comments, last by untalkative_monkey 22 years, 10 months ago
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...
...go on and live with no regrets, you only have one life...
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 <iostream.h>#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
" When you know what is; you''ll know what isn''t. "~Miyamoto Musashi
Advertisement
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...
" When you know what is; you''ll know what isn''t. "~Miyamoto Musashi
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.
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...
...go on and live with no regrets, you only have one life...
*sigh*... thanks anon, i''ll try that

...go on and live with no regrets, you only have one life...
...go on and live with no regrets, you only have one life...
Advertisement
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.
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.
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.
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.

This topic is closed to new replies.

Advertisement