cpp/language/access

In a of a  or, define the accessibility of subsequent members.

In a of a  declaration, define the accessibility of inherited members of the subsequent base class.

Syntax
@1@ The members declared after the access specifier have public member access. @2@ The members declared after the access specifier have protected member access. @3@ The members declared after the access specifier have private member access. @4@ : the public and protected members of the listed after the access specifier keep their member access in the derived class while the private members of the base class are inaccessible to the derived class. @5@ : the public and protected members of the listed after the access specifier are protected members of the derived class while the private members of the base class are inaccessible to the derived class. @6@ : the public and protected members of the listed after the access specifier are private members of the derived class while the private members of the base class are inaccessible to the derived class.

Explanation
The name of every member (static, non-static, function, type, etc) has an associated "member access". When a name of the member is used anywhere a program, its access is checked, and if it does not satisfy the access rules, the program does not compile:

Access specifiers give the author of the class the ability to decide which class members are accessible to the users of the class (that is, the interface) and which members are for internal use of the class (the implementation).

In detail
All members of a class (bodies of, initializers of member objects, and the entire ) have access to all names the class can access. A local class within a member function has access to all names the member function can access.

A class defined with the keyword has private access for its members and its base classes by default. A class defined with the keyword has public access for its members and its base classes by default. A has public access for its members by default.

To grant access to additional functions or classes to protected or private members, a may be used.

Accessibility applies to all names with no regard to their origin, so a name introduced by a or s (except inheriting constructors) is checked, not the name it refers to:

Member access does not affect visibility: names of private and privately-inherited members are visible and considered by overload resolution, implicit conversions to inaccessible base classes are still considered, etc. Member access check is the last step after any given language construct is interpreted. The intent of this rule is that replacing any with  never alters the behavior of the program.

Access checking for the names used in as well as in the default  is performed at the point of declaration, not at the point of use.

Access rules for the names of are checked at the call point using the type of the expression used to denote the object for which the member function is called. The access of the final overrider is ignored:

A name that is private according to unqualified, may be accessible through qualified name lookup:

A name that is accessible through multiple paths in the inheritance graph has the access of the path with the most access:

Any number of access specifiers may appear within a class, in any order. Member access specifiers may affect class layout: the addresses of non-static are only guaranteed to increase in order of declaration for the members.

When a member is redeclared within the same class, it must do so under the same member access:

Public member access
Public members form a part of the public interface of a class (other parts of the public interface are the non-member functions found by ).

A public member of a class is accessible anywhere:

Protected member access
Protected members form the interface of a class to its derived classes (which is distinct from the public interface of the class).

A protected member of a class is only accessible @1@ to the members and friends of that class; @2@ to the members of any derived class of that class, but only when the class of the object through which the protected member is accessed is that derived class or a derived class of that derived class:

When a pointer to a protected member is formed, it must use a derived class in its declaration:

Private member access
Private members form the implementation of a class, as well as the private interface for the other members of the class.

A private member of a class is only accessible to the members and friends of that class, regardless of whether the members are on the same or different instances:

The (C-style and function-style) allows casting from a derived lvalue to reference to its private base, or from a pointer to derived to a pointer to its private base.

Inheritance
See for the meaning of public, protected, and private inheritance.