Class question

Hi there

while I am learning about classes, I have been told over and over that
variables are private and function are public.
However, I am reading in a book that opposite might happen as well.
so I will have variables in public and functions in private.

Is that really the case?
What was the context of that?

Obviously it makes no sense for all the functions to be private - one can't call them.

Public functions are for the interface, that is the functions the user can call to interact with the object.

One can have private and protected functions that are "internal" , that is may be called by the public functions and each other. An example might be for an Arc class, which has a constructor with 3 point arguments. One would have a private functions to work out the distances between the points, and their bisectors and their intersection at the centre, also to check if the 3 points are not collinear.

Variables on the other hand should be private, otherwise anyone could change them - that destroys the concept of encapsulation. Protected variables is possible, but should be avoided, instead use the interface of the base class. However one can have a struct, where by default all the variables are public, this is OK if that struct is a private member of another class. Note that a struct is almost the same as as a class, the only difference is the default access (when public, protected, private is not specified): public for struct, private for class.

One can have protected constructors to ensure that a class can't be instantiated, making it like an abstract class - usually if there are no pure virtual functions that will force it to be so.
Last edited on
@TheIdeaMan - I will answer on the other copy of the post (once again I am sorry about this - i have absolutely no idea how did it created second copy!!!)
Topic archived. No new replies allowed.