Visibility Modes in C++ with Examples

02 Feb, 2022
When a base class is derived by a derived class with the help of inheritance, the accessibility of base class by the derived class is controlled by visibility modes. The derived class doesn’t inherit access to private data members. However, it does inherit a full parent object, which contains any private members which that class declares.  
CPP
// C++ implementation to show// Visibility modes #include <bits/stdc++.h>using namespace std; // Base class// Class A will be inheritedclass A {public:    int x; protected:    int y; private:    int z;}; // Derived class// Class B will inherit Class Aclass B : public A {}; // main functionint main(){    B b;     // x is public    // so its value will be printed    cout << b.x << endl;     // y is protected    // so it will give visibility error    cout << b.y << endl;     // z is not accessible from B    // so it will give visibility error    cout << b.z << endl;};
Compile Errors:  
prog.cpp: In function 'int main()':
prog.cpp:14:6: error: 'int A::y' is protected
  int y;
      ^
prog.cpp:34:12: error: within this context
  cout << b.y << endl;
            ^
prog.cpp:17:6: error: 'int A::z' is private
  int z;
      ^
prog.cpp:37:12: error: within this context
  cout << b.z << endl;
            ^
What does visibility means in this program?  
Since the member ‘x’ in A is public, its visibility will be open to all. It means that any class can access and use this x. That is the reason there is no error in ‘b.x’.  
The member ‘y’ in A is protected, its visibility will be only to the derived class. It means that any derived class can access and use this y.
The member ‘z’ in A is private, its visibility will not be open to any other class. It means that any derived class cannot access and use this z.
Types of Visibility modes: 
There are three types of Visibility modes:  
Public Visibility mode: If we derive a subclass from a public base class. Then the public member of the base class will become public in the derived class and protected members of the base class will become protected in the derived class. 
Public Visibility mode: If we derive a subclass from a public base class. Then the public member of the base class will become public in the derived class and protected members of the base class will become protected in the derived class. 
CPP
// C++ implementation to show// Public Visibility mode #include <bits/stdc++.h>using namespace std; // Base class// Class A will be inheritedclass A {public:    int x; protected:    int y; private:    int z;}; // Derived class// Class B will inherit Class A// using Public Visibility modeclass B : public A {}; // main functionint main(){    B b;     // x is public and it will remain public    // so its value will be printed    cout << b.x << endl;     // y is protected and it will remain protected    // so it will give visibility error    cout << b.y << endl;     // z is not accessible from B as    // z is private and it will remain private    // so it will give visibility error    cout << b.z << endl;};
Compile Errors:  
Compile Errors:  
prog.cpp: In function 'int main()':
prog.cpp:14:9: error: 'int A::y' is protected
     int y;
         ^
prog.cpp:37:15: error: within this context
     cout << b.y << endl;
               ^
prog.cpp:17:9: error: 'int A::z' is private
     int z;
         ^
prog.cpp:42:15: error: within this context
     cout << b.z << endl;
               ^
 Protected Visibility mode: If we derive a subclass from a Protected base class. Then both public member and protected members of the base class will become protected in the derived class. 
 
Protected Visibility mode: If we derive a subclass from a Protected base class. Then both public member and protected members of the base class will become protected in the derived class. 
CPP
// C++ implementation to show// Protected Visibility mode #include <bits/stdc++.h>using namespace std; // Base class// Class A will be inheritedclass A {public:    int x; protected:    int y; private:    int z;}; // Derived class// Class B will inherit Class A// using Protected Visibility modeclass B : protected A {}; // main functionint main(){    B b;     // x is public and it will become protected    // so it will give visibility error    cout << b.x << endl;     // y is protected and it will remain protected    // so it will give visibility error    cout << b.y << endl;     // z is not accessible from B as    // z is private and it will remain private    // so it will give visibility error    cout << b.z << endl;};
Compile Errors:  
Compile Errors:  
prog.cpp: In function 'int main()':
prog.cpp:11:9: error: 'int A::x' is inaccessible
     int x;
         ^
prog.cpp:33:15: error: within this context
     cout << b.x << endl;
               ^
prog.cpp:14:9: error: 'int A::y' is protected
     int y;
         ^
prog.cpp:37:15: error: within this context
     cout << b.y << endl;
               ^
prog.cpp:17:9: error: 'int A::z' is private
     int z;
         ^
prog.cpp:42:15: error: within this context
     cout << b.z << endl;
               ^
 Private Visibility mode: If we derive a subclass from a Private base class. Then both public member and protected members of the base class will become Private in the derived class.  
 
Private Visibility mode: If we derive a subclass from a Private base class. Then both public member and protected members of the base class will become Private in the derived class.  
CPP
// C++ implementation to show// Private Visibility mode #include <bits/stdc++.h>using namespace std; // Base class// Class A will be inheritedclass A {public:    int x; protected:    int y; private:    int z;}; // Derived class// Class B will inherit Class A// using Private Visibility modeclass B : private A {}; // main functionint main(){    B b;     // x is public and it will become private    // so it will give visibility error    cout << b.x << endl;     // y is protected and it will become private    // so it will give visibility error    cout << b.y << endl;     // z is not accessible from B as    // z is private and it will remain private    // so it will give visibility error    cout << b.z << endl;};
Compile Errors:  
Compile Errors:  
prog.cpp: In function 'int main()':
prog.cpp:11:9: error: 'int A::x' is inaccessible
     int x;
         ^
prog.cpp:33:15: error: within this context
     cout << b.x << endl;
               ^
prog.cpp:14:9: error: 'int A::y' is protected
     int y;
         ^
prog.cpp:37:15: error: within this context
     cout << b.y << endl;
               ^
prog.cpp:17:9: error: 'int A::z' is private
     int z;
         ^
prog.cpp:42:15: error: within this context
     cout << b.z << endl;
               ^
 
 
How to change the Visibility mode after inheritance?After inheriting a base class with the help of a specific Visibility mode, the members will automatically change its visibility as mentioned above. But inorder to change the visibility after this inheritance, we need to do it manually.Syntax:  
<visibility_mode>:
    using base::<member>;
For example:  
// inorder to change the visibility of x to public
<public>:
    using base::<x>;
Example: Consider a base class containing a public member ‘a’, protected members ‘b’ and ‘c’, and private members ‘d’ and ‘e’. Below program explains how to change the visibility of ‘b’ from protected to public. 
CPP
// C++ implementation to show how to// change the Visibility modes #include <bits/stdc++.h>using namespace std; // Base class// Class A will be inheritedclass BaseClass {     // Public memberspublic:    int a;     // Protected membersprotected:    int b;    int c;     // Private membersprivate:    int d;    int e;}; // BaseClass will be inherited as private// to change all the members to private firstclass DerivedClass : private BaseClass {     // Now change the visibility of b    // from private to publicpublic:    using BaseClass::b;}; // main functionint main(){    DerivedClass derivedClass;     // d must be private and    // hence generate visibility error    cout << derivedClass.d << endl;     // b must be now public and hence should    // not generate visibility error    cout << derivedClass.b << endl;     return 0;};
Compile Errors:  
prog.cpp: In function 'int main()':
prog.cpp:22:9: error: 'int BaseClass::d' is private
     int d;
         ^
prog.cpp:47:26: error: within this context
     cout << derivedClass.d << endl;
                          ^
 
sumitgumber28
access modifiers
C++-Inheritance
Articles
C++
CPP
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Flutter - InkWell Widget
Asymptotic Notations and how to calculate them
How to use gotoxy() in codeblocks?
Print all subsequences of a string using ArrayList
File globbing in Linux
Vector in C++ STL
Map in C++ Standard Template Library (STL)
std::sort() in C++ STL
Initialize a vector in C++ (7 different ways)
Bitwise Operators in C/C++