What is Visibility Mode?
Visibility mode specifies whether the features of the base class are privately derived or publicly derived.
Default visibility mode is private.
When a class is privately inherited by a derived class, public members of base class become private members of derived class.
When the base class is publicly inherited, public members of base class become public members of derived class.
In both above cases, private members of base class are never inherited.
Protected Visibility mode: Since private members are not participating in the inheritance, C++ is providing another visibility mode protected to provide medium secured members that participate in the inheritance. Protected members also not accessible through objects of class like private members. They can only be accessible through public members of their own class. The difference between protected and private members is, protected members participate in inheritance while private members don’t.
Security of Members of class
Private – more secured
Protected – less secured than private, but more secured than public
Public – less secured
Participation of protected members in Inheritance
If a class is derived publicly, protected data members of base class become protected member in derived class too(i.e. it is ready for further inheritance).
If a class is derived privately, protected data member becomes private in the derived class(i.e. it becomes not inheritable).
If a base class has been inherited in protected mode, both public and protected member of base class become protected member for derived class.
The keywords public, private and protected can be appeared in any order and any number of times within a class definition.
Public Inheritance:
Accessibility |
Private |
Protected |
Public |
From own class |
Yes |
Yes |
Yes |
From derived class |
No |
Yes |
Yes |
Outside derived class |
No |
No |
Yes |
Protected Inheritance:
Accessibility |
Private |
Protected |
Public |
From own class |
Yes |
Yes |
Yes |
From derived class |
No |
Yes |
Yes |
Outside derived class |
No |
No |
No |
Private Inheritance:
Accessibility |
Private |
Protected |
Public |
From own class |
Yes |
Yes |
Yes |
From derived class |
No |
Yes |
Yes |
Outside derived class |
No |
No |
No |