Originally Posted by
Lithorien
..Wouldn't the code compile fine if it's all packed in the header file..
no, you can not compile a header file alone, so don't think of it as a code file. If you were going to use only 1 file then it would be in somefile.cpp.. -a code file. But, if you need to use some code here, then a header should come into play.
Along with the posts previously mentioned, a header file can be used to expose the desired functions, variables, or perhaps classes, that are defined within the code file. You can expose them to outside usage so that other compilation units can have usage to the stuff.
Generally, in a more OOP oriented posture you would want to write up the header so that perhaps only the other people(or code) who need to use specific functions would see it. So you don't just put all references the everything in the header, but only what should be relevant on the outside.. -so this is the "interface".
Maybe in a C fashion you would have 10 code functions and some global variables, where only 4 functions need to be called from outside code to get at the functionality, and the rest just call around between each other. In this case, you could have a header with all 10 function declarations as well as the globals, or it would also be an okay choice to only have the 4 functions listed in the header.
Used in a fashion as Sebastiani mentioned you might have previously compiled 4 useful code files(and hence have their respective .obj files) and decide to right 1 header that encapsulates all that functionality into 1 mere header.
Here's a rough example(non OOP), let's say you write a compilation unit that retrieves the current time and does some specific formatting for it into a char string. On the 'outside', all you need access to is the 1 main function that handles the whole scheme, and let's say some global variable that has some useful meaning for something. Here's the 2 files:
Code:
//custom time getter header file
//custimget.h
extern int count;//pretend this would be useful to the outside
int GetFormattedTimeString(char* buf);
Code:
//custom time getter code file
//custimget.cpp
#include "custimget.h"
int count = 0;
int somethingusefultoo;
void DoSomeFormatting(void);
int DoAnything(char* buf);
int GetFormattedTimeString(char* buf);//notice this
int GetFormattedTimeString(char* buf);//notice this
int GetFormattedTimeString(char* buf)
{
//blah blah
DoSomeFormatting();
return DoAnything(buf);
}
void DoSomeFormatting(void)
{
int something;
//some support code
//blah blah
count += something;
}
int DoAnything(char* buf)
{
//blah blah
return 1;
//else
return 0;
}
-the extern 'count' in the header is not a declaration and definition(like it is in the code file), it is a special case declaration. And in this example assumably it would have some usefulness to the outside, otherwise you wouldn't especially want it there.
-You generally don't want to outright declare anything in the header if you plan to include that header elsewhere(because it will already be included into it's code file so during linkage you'd have an issue about it being multiply defined).
So, this example could be easily used by merely including the header into the file where you need to use the code.. -matter of fact you could include this header into multiple files of your project without any problems, because there are no real declaration definitions.
The topic is a broad area, because really it comes down to programming styles, and the situation, but hope that helps as far as some basics.