Gianni said:
Being a C guy pre C++, I still make everything public and then fix the
code later
And I suppose "fixing the code later" often doesn't get done at
all?
... Having said that, I do think that it's not a clear cut
rule that all member variables need to be private in every class. For
example, returning a value that contains a number of different
elements.
Well, I think a return value is a bit of an exception.
Functions return things by value all the time, and it's completely
normal. For example, a function may return an integer.
Of course in some cases you might need to, for example, return
*two* integers instead of one. Thus it makes sense to return them
in a struct.
In a way one could think that return values are not internal
implementation.
But anyways, as someone already insinuated, if you find yourself
having to write tons of accessor methods (or alternatively, if you
are lazy, having to put tons of variables in the public part of the
class), then there's probably a flaw in your OO design. It means that
there's way too much dependency between two or more classes. When a
program is well designed then you usually don't need tons of accessors.
Sometimes a module just has some values which are accessed constantly.
For example, you might have an object which has screen coordinates, and
these coordinates are used all the time everywhere. One would be tempted
to put these coordinates in the public part of the class instead of
having some stupid getX(), getY() and setCoords() functions.
However, abstracting those coordinates away is still a good thing.
It may be that at some point you might want to make some optimizations
to that class. For example, you might want to create a piece of code
which searches for the closest object to a given point. In that case
it would be handy if the objects were arranged in a Kd-tree or such.
Of course if you didn't abstract the coordinates of the object, but
everything accesses these coordinates directly, you can't optimize
the objects in such way that they are stored in the Kd-tree (and
moved inside it when the setCoords() function is called).