C++ class member visibility:
In C++, members of the "
public:" section(s) of a class (or struct) definition are visible wherever that class is visible. The public members of the class form its interface to the external world.
One cannot illustrate the behaviour of
public: with code snippets, as all accesses tend to be legal. For (non-)examples of things that fail with
private:, see that node's writeup.
By default, classes have some predefined members; these members are public members. The default constructor, default copy constructor, operator=, and are all public members. If you wish, you can put them explicitly in some other section -- but then you lose the default definition, and must define them yourself if you use them. Declaring an unused default member
private:, and never defining it, is a good way to "delete" that member.
The default visibility of a
You may also specify inheritance as
public. Public inheritance means that the inheritance relationship is "in plain sight" for all to use. Of course, publically inheriting from another class keeps all visibility restrictions of the original class --
protected: members do not suddenly become visible in the inheriting class!
Public inheritance matches what is usually considered the "is-a" relationship; it is the form most commonly used.