Skip to content
Related Articles

Related Articles

Visibility Modes in C++ with Examples
  • Difficulty Level : Medium
  • Last Updated : 04 Mar, 2020
GeeksforGeeks - Summer Carnival Banner

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.




// C++ implementation to show
// Visibility modes
  
#include <bits/stdc++.h>
using namespace std;
  
// Base class
// Class A will be inherited
class A {
public:
    int x;
  
protected:
    int y;
  
private:
    int z;
};
  
// Derived class
// Class B will inherit Class A
class B : public A {
};
  
// main function
int 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:

  1. 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.




    // C++ implementation to show
    // Public Visibility mode
      
    #include <bits/stdc++.h>
    using namespace std;
      
    // Base class
    // Class A will be inherited
    class A {
    public:
        int x;
      
    protected:
        int y;
      
    private:
        int z;
    };
      
    // Derived class
    // Class B will inherit Class A
    // using Public Visibility mode
    class B : public A {
    };
      
    // main function
    int 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:

    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;
                   ^
    
  2. 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.




    // C++ implementation to show
    // Protected Visibility mode
      
    #include <bits/stdc++.h>
    using namespace std;
      
    // Base class
    // Class A will be inherited
    class A {
    public:
        int x;
      
    protected:
        int y;
      
    private:
        int z;
    };
      
    // Derived class
    // Class B will inherit Class A
    // using Protected Visibility mode
    class B : protected A {
    };
      
    // main function
    int 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:

    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;
                   ^
    
  3. 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.




    // C++ implementation to show
    // Private Visibility mode
      
    #include <bits/stdc++.h>
    using namespace std;
      
    // Base class
    // Class A will be inherited
    class A {
    public:
        int x;
      
    protected:
        int y;
      
    private:
        int z;
    };
      
    // Derived class
    // Class B will inherit Class A
    // using Private Visibility mode
    class B : private A {
    };
      
    // main function
    int 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:

    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.




// C++ implementation to show how to
// change the Visibility modes
  
#include <bits/stdc++.h>
using namespace std;
  
// Base class
// Class A will be inherited
class BaseClass {
  
    // Public members
public:
    int a;
  
    // Protected members
protected:
    int b;
    int c;
  
    // Private members
private:
    int d;
    int e;
};
  
// BaseClass will be inherited as private
// to change all the members to private first
class DerivedClass : private BaseClass {
  
    // Now change the visibility of b
    // from private to public
public:
    using BaseClass::b;
};
  
// main function
int main()
{
    DerivedClass derivedClass;
  
    // d must be private and
    // hence generate visibility error
    cout << derivedClass.d << endl;
  
    // b must be now pubic 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;
                          ^
Want to learn from the best curated videos and practice problems, check out the C++ Foundation Course for Basic to Advanced C++ and C++ STL Course for foundation plus STL.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.
My Personal Notes arrow_drop_up
Recommended Articles
Page :