Possible Duplicate:
When should you use a class vs a struct in C++?
When, if ever, should one use a STRUCT declaration rather than a CLASS declaration when writing a program using C++?
- Never !! ?
- Any thing...whichever makes one feel better?
Possible Duplicate:
When should you use a class vs a struct in C++?
When, if ever, should one use a STRUCT declaration rather than a CLASS declaration when writing a program using C++?
Besides the default access level, a struct
and a class
are completely equivalent.
I find that the difference is mostly in the mind of a programmer - by convention, we typically use struct
s to put together small objects, without complex functionality - for example, a Point
with just 3 members - the coordinates. POD-types.
On the other hand, we typically use class
es to encompass somewhat bigger objects, with complex functionality and fairly large interface.
For example:
struct Point {
int x,y,z;
};
class Shape{
std::vector<Point> corners;
public:
Shape(const std::vector<Point> corners);
double getVolume();
double getArea();
bool isConvex();
//etc.
};
The only difference between the two is that by default struct
are public while class
members are private.
My rule is to use struct
when I mean a clump of related data without any special semantics on reading/writing them. Use class
when I intend to wrap the data in richer semantics (read methods) that are more meaningful, provide any needed protection, or implement something more abstract.