What are files with .cpp and .h extension?


What are these files with extensions .cpp and .h ? How do they interact?

asked by anonymous 26.09.2017 / 14:47

4 answers


They are files with the C ++ source code ( .cpp ) and C ( h ) , although everything that can be used in C can be used in C ++ as well and some programmers use this extension even in C ++. Ideally when in C ++ the most appropriate extension is .hpp .

The .h or .hpp are conceptually header files and have codes that the compiler needs to compile other parts. In general, it contains only those structured data (classes for example) with the signatures of methods and functions, and implementations that must be linearized or evaluated. Nothing prevents you from using other extensions for this, but this is the default.

The file .cpp has the implementations, which after compiling no longer interests for who will use what is there in the source. It's also just a convention, could use another extension.

Generally within .cpp you "call" .hpp with a build directive #include so that the compiler has access to what was defined in this header. There may be include inside other headers as well.

Not strictly necessary to do this , you should only create a header when the code should be used generically in other parties. It is possible to program without headers, but in complex applications it is almost unfeasible without abandoning code reuse.

There is a small difference in the use of include .

26.09.2017 / 14:55

.cpp files are the files that contain the sources of C ++ implementations. .h files are called headers , used to extract the declaration of functions, classes and other implementation declarations, allowing reuse.

In this answer I talk about the process of compiling C files. In general terms, it is very similar to of C ++ because it contains preprocessing of the source file, generation of object files and subsequent linking.

26.09.2017 / 14:57

.cpp are C ++ source codes and .h are C headers (although they can be used in C ++) C ++ has its own .hpp headers extension

26.09.2017 / 15:08

The .cpp file is the source of your c ++ code and the .h are header files.

When you make an include you are adding a library to your code:

#include "header.h"
26.09.2017 / 14:55