2

So a pretty simple question, but I can't seem to find a general rule of choosing one over the other in some cases.

Let's say I have a simple Point class, like this:

class Point
{
public:
    Point();
    Point(double, double, double);

    Point(const Point& other);
    Point& operator=(const Point& other);

    bool operator==(Point& lhs, Point& rhs);

    void translate(double, double, double);

    double getX() const;
    double getY() const;    
    double getZ() const;

    void setX(const double);
    void setY(const double);
    void setZ(const double);

private:
    double x_, y_, z_;
}

All well, but why not make it a struct with public x, y, z and save half of the code?

Another example lets say I have a Header struct like this:

struct Header
{
    short int id;
    short int version;        
    size_t indexOffset;
    size_t indexSize;
}

Under what circumstances I'd want to make it a class? Also is there any difference between the above and something I've also seen in a quality code like this:

class Header
{
public:
    short int id;
    short int version;        
    size_t indexOffset;
    size_t indexSize;
}

So I guess a sub-question to this is how do I decide when to make member variables private. I know OO purists will probably say always, but I'm not sure about the benefit.

Many thanks.

jaho
  • 4,852
  • 6
  • 40
  • 66
  • 2
    http://stackoverflow.com/questions/2750270/c-c-struct-vs-class – Caribou Nov 24 '12 at 16:43
  • also http://stackoverflow.com/questions/146452/what-are-pod-types-in-c – Caribou Nov 24 '12 at 16:47
  • 1
    http://stackoverflow.com/questions/92859/what-are-the-differences-between-struct-and-class-in-c?rq=1 – Bo Persson Nov 24 '12 at 16:51
  • @Caribou: I don't see how these are relevant to my question. I'm asking about some general rules of OOP and encapsulation. I know what the difference between class and a struct is. – jaho Nov 24 '12 at 16:52
  • @Marian and after people posting 3 links from stack overflow you answered your own question - basically telling us what the difference between a stuct and class is... and a rehash of your own comment "I know OO purists will probably say always, but I'm not sure about the benefit." – Caribou Nov 24 '12 at 17:18
  • I'm still not sure. The conclusion seems to be it's a matter of taste. – jaho Nov 24 '12 at 18:20
  • possible duplicate of [When should you use a class vs a struct in C++?](http://stackoverflow.com/questions/54585/when-should-you-use-a-class-vs-a-struct-in-c) – Peter O. Nov 25 '12 at 04:01

2 Answers2

2
regarding class vs. struct :

use struct if you don't need member functions and class otherwise.

regarding getters and setters :

if you need direct access to the members -- make them public. the only reason to make them private and access through getters/setters is if you need to perform some kind of processing when accessing them, like checking for validity or recalculate other dependent members.

lenik
  • 23,228
  • 4
  • 34
  • 43
  • Ok, still OO purists could say: what if one day you need some checking done when getting or setting a variable, thus they should all be private. – jaho Nov 24 '12 at 18:22
  • I'd say, when I need checking I'll add it. However, there might be another type of issue with private/public members: if you're working in a very large group or give your sources away, it might be wise to not expose the easy to damage parts, but create some kind of API and allow the access through this API only to firewall bad behaviour and invalid parameters. And even in this case, internally, your classes may still be just as public as you want. – lenik Nov 24 '12 at 18:59
0

Thanks to the link provided by Bo Persson I've found the following to be a relevant answer:

The members and base classes of a struct are public by default, while in class, they default to private. Note: you should make your base classes explicitly public, private, or protected, rather than relying on the defaults.

struct and class are otherwise functionally equivalent.

OK, enough of that squeaky clean techno talk. Emotionally, most developers make a strong distinction between a class and a struct. A struct simply feels like an open pile of bits with very little in the way of encapsulation or functionality. A class feels like a living and responsible member of society with intelligent services, a strong encapsulation barrier, and a well defined interface. Since that's the connotation most people already have, you should probably use the struct keyword if you have a class that has very few methods and has public data (such things do exist in well designed systems!), but otherwise you should probably use the class keyword.

and

Which style you use depends on circumstances and taste. I usually prefer to use struct for classes that have all data public. I think of such classes as "not quite proper types, just data structures.

Source:
http://www.parashift.com/c++-faq/struct-vs-class.html
http://www.amazon.com/dp/0201700735/?tag=stackoverfl08-20

jaho
  • 4,852
  • 6
  • 40
  • 66