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
5 Respuestas
+ 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
+ 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.
0
Flash thanx man
0
Praful M u r welcome!
0
They are the same . It just makes your main project simple