Public Protected and Private Inheritance In C++

Public Protected and Private Inheritance In C++

Through the use of examples, we will learn how to use public protected, and private inheritance in C++ in this tutorial.

Different access modes can be used to derive a child class from the base class in C++ inheritance. For instance,

class Base {
.... ... ....
};

class Derived : public Base {
.... ... ....
};

Observe the code’s use of the word “public”

class Derived : public Base

This indicates that a derived class from the base class was produced in public mode. We can also create classes in protected or private modes as an alternative.

In C++ inheritance, these three keywords—public, protected, and private—are known as access specifiers.

public, protected, and private inheritance in C++

The characteristics of public, protected, and private inheritance is as follows:

Public inheritance makes protected base class members protected in the derived class and makes public base class members public in the derived class.

The public and protected members of the base class are protected in the derived class due to protected inheritance.

The public and protected members of the base class become private in the derived class due to private inheritance.

Note: It should be noted that derived classes cannot access private base class members.

class Base {
  public:
    int x;
  protected:
    int y;
  private:
    int z;
};

class PublicDerived: public Base {
  // x is public
  // y is protected
  // z is not accessible from PublicDerived
};

class ProtectedDerived: protected Base {
  // x is protected
  // y is protected
  // z is not accessible from ProtectedDerived
};

class PrivateDerived: private Base {
  // x is private
  // y is private
  // z is not accessible from PrivateDerived
};

Example 1: C++ public Inheritance

// C++ program to demonstrate the working of public 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 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

In this case, we used public mode to derive PublicDerived from Base. Because of this, in PublicDerived:

Protected is inherited under prot.

GetPVT() and pub are sent on as public.

Since PVT is private in Base, it cannot be accessed.

We must build the public functions get PVT() and getProt(), to access private and protected members as main(), does not provide access to them:

// Error: member "Base::pvt" is inaccessible
cout << "Private = " << object1.pvt;

// Error: member "Base::prot" is inaccessible
cout << "Protected = " << object1.prot;

Take note that Base contains a definition for the get PVT() method. But PublicDerived contains the definition of the get Prot() function.

This is because PublicDerived cannot access Pvt, which is private in Base.

Prot, however, is reachable by PublicDerived because of public inheritance. So, from within PublicDerived, get Prot() has access to the protected variable.

Public Accessibility Inheritance

Accessibilityprivate membersprotected memberspublic members
Base Class Yes Yes Yes
Derived Class No Yes Yes

Example 2: C++ protected Inheritance(Public Protected and Private Inheritance)

// 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;
    }
};

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

Output

Private cannot be accessed.
Protected = 2
Public = 3

In this case, we used protected mode to derive ProtectedDerived from Base.

Consequently, ProtectedDerived:

get PVT(), port, and pub are inherited as protected.
Since Pvt is private in Base, it cannot be accessed.
Protected members are known to be inaccessible from outside the class. We are unable to use get PVT() from ProtectedDerived as a result.

Consequently, in ProtectedDerived, the getPub() function is required to access the pub variable.

// Error: member "Base::getPVT()" is inaccessible
cout << "Private = " << object1.getPVT();

// Error: member "Base::pub" is inaccessible
cout << "Public = " << object1.pub;

Protected inheritance accessibility

Accessibilityprivate membersprotected memberspublic members
Base ClassYesYesYes
Derived ClassNoYesYes (inherited as protected variables)

Example 3: C++ private Inheritance(Public Protected and Private Inheritance)

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

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

int main() {
  PrivateDerived object1;
  cout << "Private cannot be accessed." << endl;
  cout << "Protected = " << object1.getProt() << endl;
  cout << "Public = " << object1.getPub() << endl;
  return 0;
}

Output

Private cannot be accessed.
Protected = 2
Public = 3

In this case, we used private mode to derive PrivateDerived from Base.

Consequently, PrivateDerived:

get PVT(), port, and pub are inherited as private.
Since Pvt is private in Base, it cannot be accessed.
Private members cannot be immediately accessed from outside the class, as is common knowledge. We are unable to use get PVT() from PrivateDerived as a result.

To access the pub variable, we must additionally build the getPub() function in PrivateDerived.

// Error: member "Base::getPVT()" is inaccessible
cout << "Private = " << object1.getPVT();

// Error: member "Base::pub" is inaccessible
cout << "Public = " << object1.pub;

Private Inheritance and Accessibility(Public Protected and Private Inheritance)

Accessibilityprivate membersprotected memberspublic members
Base ClassYesYesYes
Derived ClassNoYes (inherited as private variables)Yes (inherited as private variables)

You should review these topics as well:

Functions in C++ with Example | C++ Programming

While and do…while loop with Example | C++ Programming

Leave a Reply