C++ Public, Protected, and Private Inheritance

Inheritance – The property of one class to derive the properties of another class is known as Inheritance.

SubClass (Base Class) – A subclass is a base class whose properties are inherited.

Superclass(Derived Class) – A superclass is a derived class that inherits the properties of another Class.

Access Specifiers – keywords public, protected, and private are the three keywords that are called Access Specifiers.

 Public Protected and Private Inheritance in C++

  1. Public Inheritance – When members of the base class are derived publicly, then the protected members of the base class will become protected members in the derived class and public members of the base class will become public members in the derived class.
  1. Private Inheritance  ( By default ) – If the derived class inherits the properties of the base class in private mode, then both public and protected members will become private in the derived class. 
  1. Protected mode – If the derived class inherits the properties of the base class in protected mode, then both public and protected members will become protected in the derived class.

Note – Private members of the base class are not accessible to the derived class.

Code:

C++ Code

/* Base Class */
class Base
{
    public: 
            int a;
    protected: 
            int b;
    private: 
            int c;
};
/* Some of the Derived classes */
/* First Derived Class - privately inherit the base class 'Base' */
class Derived1:private Base
{
 
    a is accessible - private
    b is accessible - private
    c is not accessible
    
};
/* Second Derived Class - Publicly inherit the base class 'Base' */
class Derived2: public Base
{
    
    a is accessible - public
    b is accessible - protected
    c is not accessible 
    
};
/* Third Derived Class - protectedly inherit the base class 'Base' */
class Derived3: protected Base
{
    
    a is accessible - protected
    b is accessible - protected
    c is not accessible
    
}

Public Inheritance – 

Let’s take an example of a class that is inheriting the base class publicly

Code:

C++ Code

/* Program to show the Public Inheritance */
 
#include<iostream>
using namespace std;
/* Base Class */
class Base
{
    protected: 
            int b=2;
    private: 
            int c=3;
    public: 
            int a=1;
    // Function to access private data member of the class
    int getPrivate()
    {
        return c;
    }
};
/* Derived Class is inheriting the Base Class publicly */ 
class Derived: public Base
{
    public:
    int getProtected()
    {
        return b;
    }
};
/* Driver Function */
int main()
{
    Derived obj;
    cout<<"Public member for the given class is: "<<obj.a<<endl;
    cout<<"Private member for the given class is: "<<obj.getPrivate()<<endl;
    cout<<"Protected member for the given class is: "<<obj.getProtected()<<endl;
 
    return 0;
}

Output:

Public member for the given class is: 1
Private member for the given class is: 3
Protected member for the given class is: 2

Here, Derived Class is inheriting the base class publicly, this means we can access the public members of the base class easily, but to get the value of private data member, we have to use the function getPrivate() which helps in extracting the Private value outside the class scope. Protected members can be inherited, so they can be accessible from the derived class having a function called getProtected() which will return the Protected data value of the class. 

Protected Inheritance – 

Let’s take an example of a class that is inheriting the base class protected.

Code:

C++ Code

/* Program to show the Protected Inheritance */
 
#include<iostream>
using namespace std;
/* Base Class */
class Base
{
    protected: 
            int b=2;
    private: 
            int c=3;
    public: 
            int a=1;
    // Function to access private data member of the class
    int getPrivate()
    {
        return c;
    }
};
/* Derived Class is inheriting the Base Class protectedly */ 
class Derived: protected Base
{
    public:
    // Function to access the public value of the base class
    int getPublic()
    {
        return a;
    }
    // Function to access the protected value of the base class
    int getProtected()
    {
        return b;
    }
};
/* Driver Function */
int main()
{
    Derived obj;
    cout<<"Public member for the given class is: "<<obj.getPublic()<<endl;
    cout<<"Private members are not accessible in the derived class"<<endl;
    cout<<"Protected member for the given class is: "<<obj.getProtected()<<endl;
 
    return 0;
}

Output:

Public member for the given class is: 1
Private members are not accessible in the derived class
Protected member for the given class is: 2

Here, the private members are not accessible since we are accessing the members of the base class in protected mode. We can access the protected and public member of the base class but they are inaccessible directly since we inherited them protected, so we need to create a function for accessing them.

Private Inheritance – 

Let’s take an example of a class that is inheriting the base class privately.

Code:

C++ Code

/* Program to show the Private Inheritance */
 
#include<iostream>
using namespace std;
/* Base Class */
class Base
{
    protected: 
            int b=2;
    private: 
            int c=3;
    public: 
            int a=1;
    // Function to access private data member of the class
    int getPrivate()
    {
        return c;
    }
};
/* Derived Class is inheriting the Base Class privately */ 
class Derived: private Base
{
    public:
    // Function to access the public value of the base class
    int getPublic()
    {
        return a;
    }
    // Function to access the protected value of the base class
    int getProtected()
    {
        return b;
    }
};
/* Driver Function */
int main()
{
    Derived obj;
    cout<<"Public member for the given class is: "<<obj.getPublic()<<endl;
    cout<<"Private members are not accessible in the derived class"<<endl;
    cout<<"Protected member for the given class is: "<<obj.getProtected()<<endl;
 
    return 0;
}

Output:

Public member for the given class is: 1
Private members are not accessible in the derived class
Protected member for the given class is: 2

Here, we are accessing the member of the base class privately, so this means both the protected and public members of the base class will become private members of the derived class. So, we need to use the function for accessing the private members of the derived class. Also, a private member of the base class cannot be accessed in the derived class.

Special thanks to Gurmeet Singh for contributing to this article on takeUforward. If you also wish to share your knowledge with the takeUforward fam, please check out this article