What is the difference between a .cpp file and a .h file?
What is the difference between a .cpp file and a .h file?
Nothing, except for how you use them.
cpp files you compile
h files you include
MagosX.com
Give a man a fish and you feed him for a day.
Teach a man to fish and you feed him for a lifetime.
cpp are the guts of the code. h files are headers, which usually only contain declarations and macros. You shoudl not put functions in h files because there willa rise a conflict if you include the h file from more than one cpp file. The compiler will complain about redefinition.
the cpp file woudl contain
the .h file would containCode:#include <stdio.h> void HelloWorld(){ printf("Hello World!\n); return; }
any other cpp fiels that needed to use the HelloWorld function would simply include the .h file.Code:void HelloWorld(void);
Last edited by abachler; 07-02-2008 at 09:13 AM.
Note that in C++, () and (void) both specify empty parameter lists. So your example is okay. Still, I like to have the prototypes match the implementations . . . .
Plus, your example is in C, where () and (void) are not the same thing. (void) means, "this function takes no parameters." () means, "this function could take any number of parameters -- no information about its parameters is available."
For example:
In C, one should use (void) whenever possible. The behaviour of () is a holdover from K&R C.Code:#include <stdio.h> void func1() { } void func2(void) { } int main() { func1(1); /* this does not generate an error */ func2(2); /* this does generate an error */ return 0; }
In C++, as I mentioned, () and (void) are the same thing.
Last edited by dwks; 07-02-2008 at 12:20 PM.
dwk
Seek and ye shall find. quaere et invenies.
"Simplicity does not precede complexity, but follows it." -- Alan Perlis
"Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
"The only real mistake is the one from which we learn nothing." -- John Powell
Other boards: DaniWeb, TPS
Unofficial Wiki FAQ: cpwiki.sf.net
My website: http://dwks.theprogrammingsite.com/
Projects: codeform, xuni, atlantis, nort, etc.
[edit] Never mind, it seems you're right.
Though there is one possible pitfall that I can see: if you leave out the (void) from a function definition, and don't have a prototype, then functions calling that function from later in the file will evoke the allow-any-number-of-arguments behaviour. Rather unlikely, but there you have it [/edit]
Last edited by dwks; 07-02-2008 at 12:35 PM.
dwk
Seek and ye shall find. quaere et invenies.
"Simplicity does not precede complexity, but follows it." -- Alan Perlis
"Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
"The only real mistake is the one from which we learn nothing." -- John Powell
Other boards: DaniWeb, TPS
Unofficial Wiki FAQ: cpwiki.sf.net
My website: http://dwks.theprogrammingsite.com/
Projects: codeform, xuni, atlantis, nort, etc.
Of course. It's even deprecated.
BTW, "implicit function declaration" is not the same thing as we're discussing here. Implicit function declaration is the behaviour that assumes a prototype of int func() for an unprototyped function. It's deprecated too.
This might be better termed . . . I dunno, empty parameter list behaviour or something.
dwk
Seek and ye shall find. quaere et invenies.
"Simplicity does not precede complexity, but follows it." -- Alan Perlis
"Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
"The only real mistake is the one from which we learn nothing." -- John Powell
Other boards: DaniWeb, TPS
Unofficial Wiki FAQ: cpwiki.sf.net
My website: http://dwks.theprogrammingsite.com/
Projects: codeform, xuni, atlantis, nort, etc.
Also, it's a good idea to make your header-files this way:
This will make sure that if you use multiple header files, it will only be included once.Code:#ifndef SOMENAMEFORTHEHEADER #define SOMENAMEFORTHEHEADER ...code goes here... #endif
It reads like:
-If SOMENAMEFORTHEHEADER isnt defined, then define SOMENAMEFORTHEHEADER, and process all code untill #endif
Before compilation, the preprocessor-commandoes like this come into work, and so, if this have allready been included, SOMENAMEFORTHEHEADER will allready be defined, and therefore, nothing happens when included multiple times.
A good name to use for the header guard is HEADER_H, where that's the name of the file. For example, for "core.h", you might use CORE_H.
dwk
Seek and ye shall find. quaere et invenies.
"Simplicity does not precede complexity, but follows it." -- Alan Perlis
"Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
"The only real mistake is the one from which we learn nothing." -- John Powell
Other boards: DaniWeb, TPS
Unofficial Wiki FAQ: cpwiki.sf.net
My website: http://dwks.theprogrammingsite.com/
Projects: codeform, xuni, atlantis, nort, etc.
I'm not that experienced with C++... but is it to prevent your names from accidently messing with reserved words or other other defined constants, while using consistent names for the defines?A good name to use for the header guard is HEADER_H, where that's the name of the file. For example, for "core.h", you might use CORE_H.
Last edited by Drogin; 07-02-2008 at 03:49 PM.