The difference is the .cpp file and the .h file (with the same content in cpp)?

I recently started learning cpp from the basics and was very confused about the following:

Let's say I have a header ( test.h

which contains only declarations) with some content and some source file ( source.cpp

), and the program has produced some output.

If I copied the same content of this header file into a .cpp ( testcpp.cpp

) file and included it insource.cpp

In this case, I didn't understand, what's the difference?

( I won't include this testcpp.cpp

in the makefile
)

I've seen some threads similar to this but couldn't get a clear idea !!!

I've studied the use of header and cpp files and have used them correctly in projects so far. Please answer your specific scenario (I know this makes the confusion, but just want to know). Will there be any difference in this, or is this just a common practice that people follow?

+3


source to share


3 answers


who cares?

The header file extension doesn't affect anything. You could name the file the same test.mpg

, .test

or just test

(obviously by changing the include directive) and it would work just as well. The extension is intended for the programmer, not the toolchain.

However, it is a bad idea to call it something other than .h, .hpp, or whatever. If you name it .mpg, people will think that it is a video, and without realizing that this is a header file, try playing it in a media player. If you name it .cpp, people will think it is a source file and might try to compile it or add definitions to it.



Including a file with a preprocessor is technically just copying the contents of one file to another. No more no less. Everything else about them is just an agreement.

In the makefile, when the source file is specified, can I provide the source files with any extension (.fsfs, .xxx) and not with the extension .cpp

Technically yes, however compilers usually use the source file extension to detect a language they cannot do in this case, so you will need to specify it explicitly.

+3


source


It doesn't change anything. It's just a convention about whether you use the * .h or * .cpp or * .asdasd suffix if it is not compiled by itself.

Some projects use the .hxx extension for the header files and .cc for the source file.



Please, in the interests of other programmers you will be working with, stick to general conventions and do not put header code in .cpp files.

+3


source


#include

just copies-to-paste the file you include in the current file. The file name doesn't matter a single bit - you can name it "foo.exe" if you like; if it contains valid source code in the context where it's included, that's okay (but please don't use unconventional names, you're just confusing people).

+2


source







All Articles