private
is preferred for member data. Members in C++ classes are private
by default.
public
is preferred for member functions, though it is a matter of opinion. At least some methods must be accessible. public
is accessible to all. It is the most flexible option and least safe. Anybody can use them, and anybody can misuse them.
private
is not accessible at all. Nobody can use them outside the class, and nobody can misuse them. Not even in derived classes.
protected
is a compromise because it can be used in derived classes. When you derive from a class, you have a good understanding of the base class, and you are careful not to misuse these members.
MFC is a C++ wrapper for Windows API, it prefers public
and protected
. Classes generated by Visual Studio wizard have an ugly mix of protected
, public
, and private
members. But there is some logic to MFC classes themselves.
Members such as SetWindowText
are public
because you often need to access these members.
Members such as OnLButtonDown
, handle notifications received by the window. They should not be accessed, therefore they are protected
. You can still access them in the derived class to override these functions.
Some members have to do threads and message loops, they should not be accessed or override, so they are declared as private
In C++ structures, members are public
by default. Structures are usually used for data only, not methods, therefore public
declaration is considered safe.