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?


thanks :)
In Object Oriented Programming (OOP) it is generally considered best practice to make all variables private, but there is nothing in the language itself that prevents you from having public variables.

An example of a class in the standard library that has public variables is std::pair.
1
2
std::pair<int, int> p {2, 5};
std::cout << p.first << " " << p.second << "\n";
http://www.cplusplus.com/reference/utility/pair/

It's relatively common to use public variables for classes that are just used to store a bunch of variables together, without having any invariants. In those cases the keyword struct are often used instead of class but technically a struct is a class. The only difference is that struct use public by default while class use private by default.

Private functions are not controversial. Use them whenever you want to define a function that you use internally inside the class but don't want to be called from outside the class.
Last edited on
@xxvms

Please try not to have duplicate topics. Now Peter87 and I have answered your question at about the same time, and said almost the same things.

Regards
> However, I am reading in a book that opposite might happen as well.
> so I will have variables in public and functions in private.

An immutable member variable that is part of the logical interface of a class can be (should be) public.
eg. std::chrono::system_clock::is_steady

A member variable that has no invariants associated with it and is part of the logical interface of a class can be public.
eg. std::pair mentioned by Peter87

Member functions which are implementation details should be private (or less often, protected).
In particular, virtual functions in a base class should ideally be private or protected.
In summary, prefer to make base class virtual functions private (or protected if you really must). This separates the concerns of interface and implementation, which stabilizes interfaces and makes implementation decisions easier to change and refactor later.
http://www.gotw.ca/publications/mill18.htm
@TheIdeaMan

I am not sure how did that happen? I didn't post it twice :( I think I had tab opened and while it refreshed it it submitted second time? (is that even possible?)

One thing for sure I am as twice grateful for all your guys taking time to answer my question :)
@Peter87 - that is one spot on explanation! make sense. Thank you :)
@JLBorgers - I think I understand :) but I'll have to wait a bit to fully comprehend your answer as I am still sweating about simple class not to mention base class (as far as I remember it is class at the base and above you have children classes?? )

thank you :)
@TheIdeasMan
This is the context of my question

Book Object Orientated Programming in c++ 4th Edition https://goo.gl/s3UGGo
page 220 Chapter 6

Functions are Public, Data is Private
Usually the data within a class is private and the functions are public. This is result of the way classes are used. The data is hidden so it will be safe from accidental manipulation, while the functions that operate on the data are public so they can be accessed from outside the class.
However, there is no rule that says data must be private and functions public; in some circumstances you may find you'll need to use private functions and public data.

it sounded wired (without context of your explanations) bearing in mind that this is very old book, I tough it might be wise to check if this is still true or is made obsolete by new versions of C++.

Now I have same doubts about Constractors ;) (but I will ask in separate thread)

once again thank you.
Topic archived. No new replies allowed.