开发者

CPP | .h files (C++)

I was just wondering what the difference between .cpp and .h files is? What would I use a header file (开发者_如何学Python.h) for and what would I use a cpp file for?


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


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.


By convention, .h files is something that you #include. CPP files are something you add to your project for compiling into separate object file, and then passing to the linker.


The .h file is called the header file. You usually put your interface there (the stuff you want to be public). The cpp file is where you actually implement your interface.


First, both are text files that contain code for the C++ compiler or pre-processor. As far as the system is concerned there is no difference.

By convention different file name extensions are used to indicate the content of files. In C programs you tend to see .h and .c files while in C++ .hpp and .cpp serve the same purposes.

The first group, .h and .hpp files, called header files, contains mostly non-executing code such as definitions of constants and function prototypes. They are added to programs via #include directive and used not only by the program or library in question but by other programs or libraries that will make use of them, declaring interface points and contracts defining values. They are also used to set metadata that may change when compiling for different operating systems.

The second group, .c and .cpp files, contain the executing parts of the code for the library or program.


Correct me if I'm wrong but,

When you #include something, it more-or-less inserts the entire included file into the one with the include command; that is, when I include, say "macros.h" in "genericTools.cpp", the entire contents of "macros.h" is placed in "genericTools.cpp" at that point. This is why you need to use things like "#pragma once" or other protections, to prevent including the same file twice.

Of note, templated code needs to be entirely in the file you're going to be including elsewhere. (I'm unsure of this - can template specializations be ommited from the included files, and linked like a normal function?)


The .cpp that is the implementation file is our actual program or code. When we need to use different inbuilt functions in our code, we must include the header file that is .h files.

These .h files contains the actual code of the inbuilt functions that we use hence we can simply call the respective functions.

Therefore, while we compile our code we can see more number of lines compiled than what we have actually coded because not only our code is compiled but along with that the (code of the) functions (that are included in .h files) are also compiled.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜