- 2068 Baisakh · 2 marks
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 member | public derivation | protected derivation | private derivation |
|---|---|---|---|
| public | public | protected | private |
| protected | protected | protected | private |
| private | hidden | hidden | hidden |
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)
Related questions
- Protected specifier; student/test/result program · Object Oriented Programming, chapter 6
- Strowger subscriber access design approaches · Telecommunications, chapter 1