Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CPP | .h files (C++)

I was just wondering what the difference between .cpp and .h files is? What would I use a header file (.h) for and what would I use a cpp file for?

like image 915
jay_t55 Avatar asked Jan 02 '10 00:01

jay_t55


2 Answers

In general, and it really could be a lot less general:

.h (header) files are for declarations of things that are used many times, and are #included in other files

.cpp (implementation) files are for everything else, and are almost never #included

like image 85
James Avatar answered Oct 26 '22 20:10

James


Technically, there is no difference. C++ allows you to put your code in any file, with any format, and it should work.

By convention, you put your declarations (basically, that which makes up your API) in the .h files, and are referred to as "headers". The .cpp files are for the actual "guts" of your code - the implementation details.

Normally, you have the header files included with #include by other files in your project (and other projects, if you're making a library), so the compiler can get the interface required to compile. The implementation, in the .cpp files, is typically implemented so there is one .cpp file "filling in" the implementation per .h file.

like image 31
Reed Copsey Avatar answered Oct 26 '22 22:10

Reed Copsey