.h or .cpp?
Hi all, I want to know what the exact difference is between the two extentions ".h" and ".cpp", in my previous projects I always had one ".cpp" and all the rest was ".h", but this was just coincidence. I really don't know what the difference appart from the fact that main() should be in a ".cpp" (at least I think so). Can anyone explain me the difference between the two and give me the advantage/disadvantage of using ".cpp"?
Thanks,
Joshua
The ".cpp" files are your code files. They're files that actually get compiled by the compilers. The ".h" files are header files. They are there to provide definitions. They are required because the C++ compilers compile your code in a single pass (Java does not have header files). You could say they are sort of a "bridge" between multiple cpp files. You can't really talk in terms of advantages and disadvantages as both are needed in the C++ language.
Suppose you have two files. main.cpp, containing your main() function, and foo.cpp, containing the foo() function. Now suppose you wan't to call foo() from inside main(). What are you going to do? The compiler is going to complain that foo() is not defined, unless.. Unless you define the prototype of the foo() function (that is, the name and the arguments the function takes, along with its return values). You can do this in two manners. Either in main.cpp you write int foo(); before the main() function, or you create the file foo.h, that contains int foo();. And you include foo.h in main. This allows the compiler to know in advance that a function named foo() exists and that it takes no parameters and returns an integer. This is enough for you to call foo() from inside main().
Example:
Suppose you have two files. main.cpp, containing your main() function, and foo.cpp, containing the foo() function. Now suppose you wan't to call foo() from inside main(). What are you going to do? The compiler is going to complain that foo() is not defined, unless.. Unless you define the prototype of the foo() function (that is, the name and the arguments the function takes, along with its return values). You can do this in two manners. Either in main.cpp you write int foo(); before the main() function, or you create the file foo.h, that contains int foo();. And you include foo.h in main. This allows the compiler to know in advance that a function named foo() exists and that it takes no parameters and returns an integer. This is enough for you to call foo() from inside main().
Example:
main.cpp:---------#include "foo.h"int main(int argc, char* argv[]){ foo(); return 0;}foo.h:------int foo();foo.cpp:--------int foo(){ return 1;}
.h is usualy used for function/class prototypes, constants (#defines), and structure prototyping. You then include this file in every file needing access to those classes, variables, and structures.
.cpp contains the definitions of what I said above, as well as global variables and such.
.cpp contains the definitions of what I said above, as well as global variables and such.
Quote:Original post by Max_PayneDeclarations, not definitions. Having a definition in a header files leads to redefinition link errors.
The ".h" files are header files. They are there to provide definitions.
Quote:They are required because the C++ compilers compile your code in a single pass (Java does not have header files).Actually, it's technically because of the forward declaration requirement. A multi-pass compiler would make it possible to eliminate header files, but it would be semantically invalid until the forward declaration requirement was lifted.
See Zahlman's link to Kylotan's article for much more detail.
Quote:Original post by OluseyiQuote:Original post by Max_PayneDeclarations, not definitions. Having a definition in a header files leads to redefinition link errors.
The ".h" files are header files. They are there to provide definitions.
The standard allows certain definitions to appear more than once in a program, such as const variables that have not been declared/defined to have external linkage, template functions, inline functions, classes, and many more.
beware though if you put something in a header be it variables function whatever every time you modify it (if you want those changes to take place) you must rebuild the project because the compiler dosnt compile the .h files if you hit compile
just a word of warning [grin]
just a word of warning [grin]
Quote:Original post by JingoCorrect. These are, however, exceptions to the general rule because of peculiarities of their definition or usage.
The standard allows certain definitions to appear more than once in a program, such as const variables that have not been declared/defined to have external linkage, template functions, inline functions, classes, and many more.
class definitions are all I use header files for. I think definitions are more the rule than the exception.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement