C++ Protected Inheritance

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.

Example:

C++
// C++ program to demonstrate the working
// of protected inheritance
#include <iostream>
using namespace std;

class Base {
private:
    int pvt = 1;

protected:
    int prot = 2;

public:
    int pub = 3;

    // function to access private member
    int getPVT() { return pvt; }
};

class ProtectedDerived : protected Base {
public:
    // function to access protected member from Base
    int getProt() { return prot; }

    // function to access public member from Base
    int getPub() { return pub; }
      
      // function to get access to private members from Base
      int try_getPVT() {Base::getPVT(); }
};

int main()
{
    ProtectedDerived object1;
    cout << "Private = " << object1.try_getPVT() << endl;
    cout << "Protected = " << object1.getProt() << endl;
    cout << "Public = " << object1.getPub() << endl;
    return 0;
}

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

C++ Inheritance Access

Prerequisites:

Before learning about Inheritance Access we need to know about access specifiers. There are three Access specifiers in C++. These are:

public – members are accessible from outside the class, and members can be accessed from anywhere.
private – members cannot be accessed (or viewed) from outside the class, i.e members are private to that class only.
protected – members cannot be accessed from outside the class, but, they can be accessed in inherited classes or derived classes.

Public, Protected, and Private inheritance in C++

public, protected, and private inheritance have the following features:

  • public inheritance makes public members of the base class public in the derived class, and the protected members of the base class remain protected in the derived class.
  • protected inheritance makes the public and protected members of the base class protected in the derived class.
  • private inheritance makes the public and protected members of the base class private in the derived class.

Accessibility Of Inheritance Access:

Inheritance Access

Similar Reads

1. C++ public Inheritance

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

2. C++ Protected Inheritance

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

3. C++ Private Inheritance

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