.cpp and .hpp | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

.cpp and .hpp

What will happen if we put both definitions and declarations in .hpp in c++? Overall what’s the difference between .cpp and .hpp file ?

4th Jan 2019, 7:07 AM
saghi
5 Answers
+ 3
.hpp or just .h is used as header, which means that you should only declare all functions and classes in it, so the compiler can link them correctly in case you want to access the document from your main program file: // print.hpp void print(int); .cpp files now define the functions and classes you previously declared in the .hpp file: // print.cpp #include "print.hpp" void print(int num) { cout << num << endl; } If you just use a single file this doesn't matter, but working with multiple files can be really nice. In this case for example, you can now include the "print.hpp" file which will work just like a library, so you can call the print() function from your main source file.
4th Jan 2019, 7:24 AM
Aaron Eberhardt
Aaron Eberhardt - avatar
+ 6
ShortCode connection issues, sololearn has poor servers
4th Jan 2019, 8:51 AM
᠌᠌Brains[Abidemi]
᠌᠌Brains[Abidemi] - avatar
4th Jan 2019, 7:19 AM
᠌᠌Brains[Abidemi]
᠌᠌Brains[Abidemi] - avatar
+ 1
.hpp is a header file, such like .h in C .cpp runs the main program
4th Jan 2019, 7:16 AM
ShortCode
0
᠌᠌brains posted it twice
4th Jan 2019, 7:25 AM
ShortCode