Quote: "So as I understand, every Cpp has to have .h of their own?"
Header files can be used in a number of ways. The answer to your question is, "not necessarily." They can contain actual code of their own if necessary. They can declare variables used for other functions but at a global level. One of the most common uses of a header file is to present to other code segments the prototypes of the functions defined in the .cpp files.
Let's say you have a file main.cpp that contains your main() function and the bulk of your code. In another file called utility.cpp you have a number of functions you're defining. Let's call them
move (char *from, char *to){code}
copy (char *from, char *to){more code}
add (char *here, char *there){yet more code}
In the main () function in main.cpp you need to call these functions for one reason or another. By the time the compiler sees the calls to these functions it needs to know something about the function. It's possible that it's already compiled the code for utility.cpp and knows what the footprint of the three functions defined therein are. But that's not always the case so we have to let the compiler know what the footprint is of any functions it's apt to detect a call to before it sees the call.
To that end, we create a header file. In this case it would likely be
utility.h and it would have
move (char *from, char *to);
copy (char *from, char *to);
add (char *here, char *there);
in it. These define the footprint of the functions defined in the
utility.cpp file. It's probably advisory to put
#include "utility.h"
at the beginning of the utility.cpp file but not always necessary. Where you would really need to place it would be near the beginning of the main.cpp file. That way, when the compiler sees the directive to include the header file it reads in the prototypes before any calls in main() are made to the utility functions. You'd need to do this for any .cpp file that made calls to these functions. There is, however a bit of precaution that needs to be taken.
Since the utility.h file might be included in a number of source files it will get read in a number of times. Seeing a prototype more than once doesn't break the compile. However, if you declare variables, for example, in a header file that's read in more than once during a compile you'd find the compiler trying to declare the variable more than once and that would be a conflict.
That's why you should take precautions. Inside the utility.h file you might have something like:
#ifndef _UTILITY_H_
#define _UTILITY_H_
// prototypes
move (char *from, char *to);
copy (char *from, char *to);
add (char *here, char *there);
// necessary variables
int BigNumber;
char *Version = "1.2.2";
#endif
When the compiler encounters a directive to include utility.h the first time it sees that _UTILITY_H_ isn't defined and will include the remainder of the file in its input. If it encounters an include for utility.h a second time it will note that _UTILITY_H_ is now defined (it happened during the first pass) and skip to just after the #endif directive.
Lilith, Night Butterfly
I'm not a programmer but I play one in the office