C++ Reference ยท Types and Language Concepts
Access specifiers: public, protected, private
Reference for C++ access specifiers: what public, protected and private allow, default access in class and struct, inheritance access and friends.
Access specifiers control which code may use the members of a class. A public member can be used by anyone. A protected member can be used by the class itself, its friends and its derived classes. A private member can be used only by the class itself and its friends.
Access is checked at compile time, per name, not per object. That is why one object of a class may read the private members of another object of the same class. The checks apply to names found by lookup, not to memory: access control is a design tool, not a security feature.
class Account { public: void deposit(int cents); // part of the interface protected: int fee() const; // for derived classes private: int balance_ = 0; // implementation detail };
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with std::cin or std::getline), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with std::cin or std::getline), it gets the next line from this box.
Syntax
class X { public: /* ... */ protected: /* ... */ private: /* ... */ }; class D : public B {}; // inheritance access: public, protected or private
| Specifier | Accessible from |
|---|---|
public |
Any code. |
protected |
Members and friends of the class, and members and friends of derived classes (through objects of the derived type). |
private |
Members and friends of the class only. |
| Inheritance | Public members of the base become | Protected members of the base become |
|---|---|---|
public |
public in the derived class | protected |
protected |
protected | protected |
private |
private | private |
Private members of the base are never accessible in the derived class, whatever the inheritance kind.
Usage notes
- Defaults:
classmembers and base classes are private by default.structmembers and base classes are public by default. That is the only difference between the two keywords. - You may use the specifiers any number of times, in any order. They apply until the next specifier.
- Access applies to names, not visibility. A private member still takes part in overload resolution; a private overload that wins is then rejected with an access error. Make unwanted functions
= deleteinstead of private, so the error says what happened. - Friends (
friend) get the same access as members. Seefriend. - Protected data is a weak boundary. Any derived class can change it, so prefer protected functions to protected data members.
- Private inheritance means "implemented in terms of". The conversion to the base is not available outside, and the base members are hidden. A
usingdeclaration can re-expose single names, as in the second example. - Access of virtual functions is checked at the static type. A private virtual function can still be overridden by a derived class and called through the base, which is the basis of the non-virtual interface idiom.
- Nested classes are members, so they can use the private names of the enclosing class (since C++11). The enclosing class has no special access to the nested class.
protectedthrough a base reference: a derived class member can access a protected member only through an object of its own type (or deeper), not through a plainBase&.
Related
classandstruct,friend, Inheritance,using- Lessons: Access specifiers, Inheritance, Friend