# Confused about .h and .cpp

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

## Recommended Posts

Until now so have i made ALL functions in one .cpp file. And now so do i wonder what's the difference is between .h and .cpp files and how i should put them into a Project File in Code::Blocks (I migrated to it now). Can anyone help a semi-newbie to clear this up?

##### Share on other sites
The difference is that a .h (header) file doesn't need to be compiled, it's just declarations. You can separate your function prototypes from their definitions, and keep your .cpp files (where you define your prototypes) in a place all to their own. You can look at your .h files to see what functions you have and their signatures (int multiple(int, int)) and you don't need to see their definitions.

Also, if you're giving your program to someone else to use, you can lock away your .cpp files and just give them access to the .h file, thus allowing them to use the functions without knowing how they're defined. I'm sure there are other differences, these are a few that come to mind.

##### Share on other sites
The most common area for using something like this (well for me anyway) is classes. Declare your class in a header file (myClass.h) with all it's member variables and member functions, and implement these functions in the cpp file (myClass.cpp [can be called anything you like]).
Make sure myClass.cpp has #include "myClass.h" there, and for any other file that requires myClass.

##### Share on other sites
From your Googling and reading efforts, ChJees, what do you think the difference between the two is? Or, at least what impressions do you get?

##### Share on other sites
Well, the only thing they stated is that .h is just for showing what the compiler has to expect from the .cpp file. Not so much on how to actually slap this into the program.

Should i just #include "x.h" of the .h files into the main compile .cpp or should i change their prioroties?

That's what i gathered from Googling.

##### Share on other sites
header files (.h, .hpp) are used to give another source file the bare minimum information it needs to use functions, objects etc. e.g.:

/* *  one_liners.h *  a set of functions to print one liners */    void arnie_one_liner();    void chuck_norris_one_liner();    void jackie_chan_one_liner();

the contents are directly copied into the file that includes the header.

.cpp files are compiled only once, unless you #include them, in which case they will be treated as header files. They should contain all the information NOT in the header file. eg:

/* *  one_liners.cpp *  a set of functions to print one liners */#include <iostream>#include "one_liners.h"void arnie_one_liner(){    std::cout<<"I'll be back!"<<std::endl;}     void chuck_norris_one_liner(){    std::cout<<"Chuck Norris doesn't wear a watch, he decides what time it is."<<std::endl;}void jackie_chan_one_liner(){     std::cout<<"Do not let circumstances control you. You change your circumstances. "<<std::endl;}

so, a file in your application may want to use one liners, so you would #include "one_liners.h". This gives it "function prototypes" and it knows the interface with the functions you call, which satisfied the compiler / linker that the function exists. When the .cpp file is eventually compiled, the linker will already know what function your file needs, and can link it to the code in the .cpp file.

Also, note that to use one_liners.cpp you don't need to include <iostream> in the file, because one_liners .cpp already includes iostream.

to use one_liners in your project, simply create each file, and copy and paste the code from here into them. in the file you want to use one liners in, just #include "one_liners.h" and let code::blocks take care of the rest. It will ensure one_liners.cpp is added to the list of files to compile.

##### Share on other sites
speciesUnknown, i think you explained it in a idiot proof way for me :P.
I do think i understand now. Thanks for all your help here :).

##### Share on other sites

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