![]() |
VOOZH | about |
Inheritance access defines how the access specifiers (public, protected, private) of a base class are treated in the derived class.
Accessibility | Public Members | Protected Members | Private Memebers |
|---|---|---|---|
Base Class | Yes | Yes | Yes |
Derived Class | Yes | Yes | No |
In this example, public inheritance is demonstrated. Since private and protected members will not be directly accessed from main( ) so we have had to create functions name getPVT( ) to access the private variable and getProt( ) to access the protected variable from the inherited class.
Private = 1 Protected = 2 Public = 3
We know that protected members can only be accessed from the Derived class. These members cannot be directly accessed from outside the class. So we cannot use getPVT() from ProtectedDerived. This is also why we need to create getPub() function in the Derived class in order to access the pub variable.
Private = 1 Protected = 2 Public = 3
Note: There is still a way to access the Private members of base class. Since Base has a public member function that can access its private member variables, we can create a call to this member function (inherited as Protected member function in derived class) to access the Private inherited variable of Base class.
We know that private members cannot be accessed from the Derived class. These members cannot be directly accessed from outside the class. So we cannot use getPVT() from PrivateDerived. So we need to create the getPub() function in PrivateDerived in order to access the pub variable.
Private = 1 Protected = 2 Public = 3