Discuss the role of acess specifiers in inheritance and show their visibility when they areinherited as public, private and protected.
Question
Discuss the role of acess specifiers in inheritance and show their visibility when they areinherited as public, private and protected.
Solution
Access specifiers in C++ are used to implement the encapsulation feature of OOP. They are used to set the visibility of classes, data, and methods. There are three types of access specifiers: public, private, and protected.
-
Public Specifier: When a base class is inherited publicly, the public members of the base class become public in the derived class and protected members of the base class become protected in the derived class.
-
Private Specifier: When a base class is inherited privately, both the public and protected members of the base class become private in the derived class.
-
Protected Specifier: When a base class is inherited protectedly, both the public and protected members of the base class become protected in the derived class.
Here is a simple visibility table:
| Base Class | Public Derivation | Private Derivation | Protected Derivation |
|---|---|---|---|
| Public | Public | Private | Protected |
| Private | Not Inherited | Not Inherited | Not Inherited |
| Protected | Protected | Private | Protected |
Remember, private members of a base class are never inherited in the derived class. They can only be accessed through methods of the base class.
Similar Questions
Explain the visibility of base class members for the access specifiers: private, protected and public while creating the derived class and also explain the syntax for creating derived class.
Inheritance is the concept which deals with
What are the rules of inheritance
Which statement best describes the method of inheritance in OOP?
When deriving a class from with protected inheritance, public members of the base class become___________ members of the derived class, and protected members of the base class become _____________ members of the derived class. a.private, private b.protected, protectedc.public, private d.private, protected
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.