C++ classes | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

C++ classes

Whats the difference between declaring a class in a c++ program and creating one in a separate file which as header and source. Are they the same or different things? Im confused

23rd Jun 2018, 3:05 PM
Praful M
Praful M - avatar
5 Answers
+ 1
hi! .h files normally contains function declaration of a class and .cpp contains the implementation details of those function of a class. for example,: math.h class math { public: static double add(double a, double b); }; math.cpp double math::add(double a, double b) { return a+b; } main.cpp #include <iostream> #include “math.h” int main() { std::cout << math::add(1.0, 2.0); return 0; } // this should output 3.0 on the console hope it helps
23rd Jun 2018, 3:45 PM
Flash
+ 1
btw, main difference is hiding ur source code. if u have separate .h and .cpp files for that math class then u can just distribute math.h to ur clients with a static or dynamic lib file. that way they can use ur code without knowing how it’s done.
23rd Jun 2018, 3:49 PM
Flash
0
Flash thanx man
23rd Jun 2018, 3:53 PM
Praful M
Praful M - avatar
0
Praful M u r welcome!
23rd Jun 2018, 3:56 PM
Flash
0
They are the same . It just makes your main project simple
24th Oct 2019, 6:36 AM
Eyouel Haile
Eyouel Haile - avatar