C++ Inheritance Access

Last Updated : 9 Nov, 2025

Inheritance access defines how the access specifiers (public, protected, private) of a base class are treated in the derived class.

  • It controls the visibility and accessibility of base class members in the derived class and to outside code.
  • The type of inheritance (public, protected, private) determines how the base class members are inherited.

Types of Inheritance Access

  • Public: Base class members retain their access levels in the derived class (public → public, protected → protected).
  • Protected: Base class members become protected in the derived class, regardless of original access (public/protected → protected).
  • Private: Base class members become private in the derived class, restricting access outside the derived class.

Accessibility

Public Members

Protected Members

Private Memebers

Base Class

Yes

Yes

Yes

Derived Class

Yes

Yes

No

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.

C++
#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 PublicDerived : public Base
{
  public:
    // function to access protected member from Base
    int getProt()
    {
        return prot;
    }
};

int main()
{
    PublicDerived object1;
    cout << "Private = " << object1.getPVT() << endl;
    cout << "Protected = " << object1.getProt() << endl;
    cout << "Public = " << object1.pub << endl;
    return 0;
}

Output
Private = 1
Protected = 2
Public = 3

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.

C++
#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()
    {
        return 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++ 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.

C++
#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 PrivateDerived : private Base
{
  public:
    // function to access protected member from Base
    int getProt()
    {
        return prot;
    }

    // function to access public member
    int getPub()
    {
        return pub;
    }

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

int main()
{
    PrivateDerived 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
Comment