Skip to main content

Access specifiers in inheritance

Short answer

Access specifiers private, protected, and public control class member access, and inheritance mode further changes accessibility: public inheritance preserves access; protected lowers public to protected; private makes all inherited members private. Private base members remain hidden in derived classes.

Explain different types of access specifiers used in inheritance.

C++ has three access specifiers that control how class members can be accessed, and also how they are inherited:

  • private - accessible only inside the class itself (and friends). In inheritance, private members of the base are not directly accessible in the derived class.
  • protected - accessible inside the class and in its derived classes, but not from outside.
  • public - accessible from anywhere.

When a class is inherited, the specifier used in the derivation (public, protected, private) changes the access of the inherited members:

Base memberpublic derivationprotected derivationprivate derivation
publicpublicprotectedprivate
protectedprotectedprotectedprivate
privatehiddenhiddenhidden

Example: class D : public B { }; keeps B's public members public in D, and its protected members protected.

✓ Answer checked by Claude Opus 5.5 (high effort)

All Object Oriented Programming chapter 6 questions →