• Advertisement
Sign in to follow this  

.h or .cpp?

This topic is 4857 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

Share this post


Link to post
Share on other sites
Advertisement
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:


main.cpp:
---------

#include "foo.h"

int main(int argc, char* argv[])
{
foo();
return 0;
}

foo.h:
------

int foo();

foo.cpp:
--------

int foo()
{
return 1;
}



Share this post


Link to post
Share on other sites
.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.

Share this post


Link to post
Share on other sites
Quote:
Original post by Max_Payne
The ".h" files are header files. They are there to provide definitions.
Declarations, not definitions. Having a definition in a header files leads to redefinition link errors.

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.

Share this post


Link to post
Share on other sites
Quote:
Original post by Oluseyi
Quote:
Original post by Max_Payne
The ".h" files are header files. They are there to provide definitions.
Declarations, not definitions. Having a definition in a header files leads to redefinition link errors.



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.

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
Quote:
Original post by Jingo
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.
Correct. These are, however, exceptions to the general rule because of peculiarities of their definition or usage.

Share this post


Link to post
Share on other sites
class definitions are all I use header files for. I think definitions are more the rule than the exception.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement