I am confused on how to separate implementation and declarations code of a simple class into a new header and cpp file. For example, how would I separate the code for the following class?
class A2DD
{
private:
int gx;
int gy;
public:
A2DD(int x,int y)
{
gx = x;
gy = y;
}
int getSum()
{
return gx + gy;
}
};
In general your .h contains the class defition, which is all your data and all your method declarations. Like this in your case:
A2DD.h:
class A2DD
{
private:
int gx;
int gy;
public:
A2DD(int x,int y);
int getSum();
};
And then your .cpp contains the implementations of the methods like this:
A2DD.cpp:
A2DD::A2DD(int x,int y)
{
gx = x;
gy = y;
}
int A2DD::getSum()
{
return gx + gy;
}
You leave the declarations in the header file:
class A2DD
{
private:
int gx;
int gy;
public:
A2DD(int x,int y); // leave the declarations here
int getSum();
};
And put the definitions in the implementation file.
A2DD::A2DD(int x,int y) // prefix the definitions with the class name
{
gx = x;
gy = y;
}
int A2DD::getSum()
{
return gx + gy;
}
You could mix the two (leave getSum()
definition in the header for instance). This is useful since it gives the compiler a better chance at inlining for example. But it also means that changing the implementation (if left in the header) could trigger a rebuild of all the other files that include the header.
Note that for templates, you need to keep it all in the headers.
Basically a modified syntax of function declaration/definitions:
a2dd.h
class A2DD
{
private:
int gx;
int gy;
public:
A2DD(int x,int y);
int getSum();
};
a2dd.cpp
A2DD::A2DD(int x,int y)
{
gx = x;
gy = y;
}
int A2DD::getSum()
{
return gx + gy;
}
I won't refer too your example as it is quite simple for a general answer (for example it doesn't contain templated functions ,which force you to implement them on the header) , what I follow as a rule of thumb is the pimpl idiom
It has quite some benefits as you get faster compilation times and the syntactic sugar :
class->member
instead of class.member
The only drawback is the extra pointer you pay.
A2DD.h
class A2DD
{
private:
int gx;
int gy;
public:
A2DD(int x,int y);
int getSum();
};
A2DD.cpp
A2DD::A2DD(int x,int y)
{
gx = x;
gy = y;
}
int A2DD::getSum()
{
return gx + gy;
}
The idea is to keep all function signatures and members in the header file.
This will allow other project files to see how the class looks like without having to know the implementation.
And besides that, you can then include other header files in the implementation instead of the header. This is important because whichever headers are included in your header file will be included (inherited) in any other file that includes your header file.
It's important to point out to readers stumbling upon this question when researching the subject in a broader fashion that the accepted answer's procedure is not required in the case you just want to split your project into files. It's only needed when you need multiple implementations of single classes. If your implementation per class is one, just one header file for each is enough.
Hence, from the accepted answer's example only this part is needed:
#ifndef MYHEADER_H
#define MYHEADER_H
//Class goes here, full declaration AND implementation
#endif
The #ifndef etc. preprocessor definitions allow it to be used multiple times.
PS. The topic becomes clearer once you realize C/C++ is 'dumb' and #include is merely a way to say "dump this text at this spot".
Usually you put only declarations and really short inline functions in the header file:
For instance:
class A {
public:
A(); // only declaration in the .h unless only a short initialization list is used.
inline int GetA() const {
return a_;
}
void DoSomethingCoplex(); // only declaration
private:
int a_;
};
Source: Stackoverflow.com