Loading...
C++ Inheritance Access Control

C++ Inheritance Access Control

In this tutorial, we will learn to use Inheritance Access Control (public, protected and private) in C++ with the help of examples.

Inheritance Access Control

  • When creating a derived class from a base class then, you can use different access specifiers to inherit the data members of the base class.
  • The derived class can access all the non-private members of its base class. And the base class members that are not accessible to the member functions of derived classes should be declared private in the base class.
  • The access specifiers that are used are public, private and protected.
  • When deriving a class from a base class, the base class may be inherited through public, private and protected inheritance.

In C++ inheritance, we can derive a child class from the base class in different access modes. For example,

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

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

Notice the keyword public in the code

class Derived : public Base

This means that we have created a derived class from the base class in public mode. Alternatively, we can also derive classes in protected or private modes.

These 3 keywords (public, protected, and private) are known as access specifiers in C++ inheritance.


Syntax of Inheritance Access Control in C++

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

  1. Public inheritance: This is the most used inheritance mode. In this the protected member of Base class becomes protected members of Derived class and public becomes public.
  2. Protected inheritance: In protected mode, Then both public member and protected members of the Base class will become protected in Derived class.
  3. Private inheritance: In private mode, the protected and public members of Base class become private members of Derived class.

Note: private members of the base class are inaccessible to the derived class.

Syntax:-

class Base {
public:
int a;
protected:
int b;
private:
int c;
};

class PublicDerived: public Base {
// a is public
// b is protected
// c is not accessible from PublicDerived
};

class ProtectedDerived: protected Base {
// a is protected
// b is protected
// c is not accessible from ProtectedDerived
};

class PrivateDerived: private Base {
// a is private
// b is private
// c is not accessible from PrivateDerived
}

Example 1: 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

Here, we have derived PublicDerived from Base in public mode.

As a result, in PublicDerived:

  • prot is inherited as protected.
  • pub and getPVT() are inherited as public.
  • pvt is inaccessible since it is private in Base.

Since private and protected members are not accessible, we need to create public functions getPVT() and getProt() to access them:

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

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

Accessibility in Public Inheritance

Accessibility private members protected members public members
Base Class Yes Yes Yes
Derived Class No Yes Yes

Example 2: C++ Program to demonstrate the working of protected inheritance

#include <iostream>
#include <string>
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

Here, we have derived ProtectedDerived from Base in protected mode.

As a result, in ProtectedDerived:

  • prot, pub and getPVT() are inherited as protected.
  • pvt is inaccessible since it is private in Base.

As we know, protected members cannot be accessed directly.

As a result, we cannot use getPVT() from ProtectedDerived. That is also why we need to create the getPub() function in ProtectedDerived in order 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;

Accessibility in Protected Inheritance

Accessibility private members protected members public members
Base Class Yes Yes Yes
Derived Class No Yes Yes (inherited as protected variables)

Example 3: 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

Here, we have derived PrivateDerived from Base in private mode.

As a result, in PrivateDerived:

  • prot, pub and getPVT() are inherited as private.
  • pvt is inaccessible since it is private in Base.

As we know, private members cannot be accessed directly.

As a result, we cannot use getPVT() from PrivateDerived. That is also why we need to create the getPub() function in PrivateDerived in order 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;

Accessibility in Private Inheritance

Accessibility private members protected members public members
Base Class Yes Yes Yes
Derived Class No Yes (inherited as private variables) Yes (inherited as private variables)

Next Tutorial

We hope that this tutorial helped you develop better understanding of the concept of Inheritance Access Control in C++.

Keep Learning : )

In the next tutorial, you'll learn about C++ Inheritance Types.

- Related Topics