≡ Menu

C++ Inheritance – Public Vs Private Vs Protected Explained with Example Program

In C++, the most common type of inheritance is public.

The public type of inheritance is used in order to broaden the parent class with some functionalities or some data members.

This type of public inheritance is sometimes called the ” is “, as the parent class and the child class are of the same type and that could be seen with conversions, where the child class has more to it, than parent one.

The private inheritance is of different type, it is of ” has ” type.

This means that private inheritance is suitable for situations in which we need to prevent main program to accidentally access some of the methods inherited from the parent class.

The protected inheritance is less restrictive than private and it is useful when we need some of the implementations from grand parent class.

The following table will illustrate this difference:

Public Members Protected Members Private Members
Public Inheritance public protected private
Protected inheritance protected protected private
Private inheritance private private private

.
As we can see from the above table, private inheritance is the most restrictive and the protected is somehow in between the private and the public type.

For your reference, the following will help to understand the basics of inheritance:

Before we start with an example program, I want to point out that the first time I dealt with private inheritance was really long long time ago. I have accidentally omitted to add public in the front of the parent class.

This was confusing at that time, because I have not seen that in any books. Since I was in a rush, I didn’t research further and just just added word public in the front of the parent class and moved-on.

At a later time, I realized how this worked when I had a chance to learn more about it from Lippman and Lajoje’s book C++ Primer (5th edition). I strongly recommend that you read that book if you are serious about programming in C++.

Private Inheritance Example Program

In order to understand how to use private inheritance, let us look at the following example program that uses two classes: the parent and the child classes.

# cat p1.cpp
#include <iostream>

using namespace std;

class Parent{
public:
  void parentMethod( void ){ cout<<"Inside parent method"<<endl;}
};

class Child : private Parent{
public:
  void childMethod( void){
    cout<<"Inside child method"<<endl;
    parentMethod();
  }
};

int main( void ){
  Child C;
  C.childMethod();
  return 0;
}

In the above example code:

  • We created one object of the Child type with name “C”
  • Then we applied childMethod(), which has some message and it will in-turn call the method parentMethod() that is placed in its body.
  • If you try to call method parentMethod() on the object “C”, you will get error message. From this we observe the most important property of private inheritance that it will disable the child object to accidental access some of the grand parent methods that would get inherited with public inheritance.

The following is the output of the above program:

# g++ p1.cpp

# ./a.out
Inside child method
Inside parent method

Protected Inheritance Example Code

The following example explains how the protected inheritance could be used in the program.

# cat p2.cpp
#include <iostream>

using namespace std;

class GrandParent{
public:
  void grandParentMethod( void ){ cout<<"Method in the grand parent class"<<endl; }
};

class Parent : protected GrandParent{
public:
  void parentMethod( void ){ cout<<"Method in the parent class"<<endl; }
};

class Child: protected Parent{
public:
  void 
  childMethod( void ){
    cout<<"Method in the child class"<<endl;
    parentMethod();
    grandParentMethod();
  }
};

int 
main( void ){

  Child C;
  C.childMethod();
  return 0;

}

In the above example code:

  • We created three levels of inheritance with classes: the grand parent, the parent and child.
  • From this chain we have one method at the each of the classes.
  • The main function has one object of the Child type, afterwards we call the method childMethod(), which has two calls of parentMethod() and grandParentMethod().

The following is the output of the above program:

# g++ p2.cpp

# ./a.out
Method in the child class
Method in the parent class
Method in the grand parent class

From these two examples, we have learned how this type of inheritance is implemented.

Now, I need to say that in this case, we have used protected inheritance in the parent class and if we have used the private inheritance in the second layer of chain inheritance, we would’ve end up with an error message.

In order to make the most of this two types of inheritance, I will show you how you could use particular method from parent class if we overload it with some name method in the child class.

That task should be achieved in the following three ways:

  • Parent::parentMethod();
  • using Parent::Method();
  • Object.Parent::parentMethod().

In another words, if the child class hides some of the methods from the parent class, we will have already mentioned methodologies to access hidden methods.

Private vs Protected Inheritance

Beside having difference in the syntax, we have learned that private inheritance is more restrictive that protected inheritance, and that difference is important.

From this we will know should we break the chain of inheritance with the private inheritance or should we keep implementations with protected inheritance.

So, if you would like to use implementation in the parent class but not in the child class you would use private class, but if you would like to use implementation in the child class too, you have the protected inheritance.

Therefore, we could say that private and protected inheritance will keep implementations, but it will create the restrictions to the interface.

Using Containment vs Private and Protected Inheritance

Technically speaking, the containment is utilized if we have one class inside the other one. By the way, we could also have pointer of other class type or even reference of the other class type.

The containment is also ” has ” type of the relationship between two classes.

i.e, the class could have some relationships to other classes and that will be useful in some of the situations.

It is usually more likely that you will use containment, rather than the private or the protected inheritance.

Scenarios to Use Private or Protected Inheritance

Let’s consider this scenario. The programmer is developing the parent class with array inside and the binary search method.

From this class we create child class that will not store elements of already mentioned array in the sorted manner.

So, we want to use the array in the main function for our child object, but we would not allow binary search to be used, because in order to use binary search array needs to be sorted.

This would be useful in situations when we develop the program with more programmers.

This types of inheritance could be useful with virtual functions, when we need to limit access to parent methods too.

One more situation in which we would need to use this types of inheritance is with multiple inheritance. For that we would need to have at least two parents and one of the methods should be inaccessible from outside of our class.

Based on this idea, I am sure you will find few situations on your own as well.

Final Thoughts

For many programmers, the containment is more likely the better choice. The private and protected inheritance are implemented just because we need to respect the consistence too. We could say that this type of inheritance has it’s own place also.

If you would like to become complete C++ programmer, you are going to master this technique as well and use it when you need it.

However, one question comes to my my mind at this point: Why is the private inheritance default type of inheritance?

If you enjoyed this article, you might also like..

  1. 50 Linux Sysadmin Tutorials
  2. 50 Most Frequently Used Linux Commands (With Examples)
  3. Top 25 Best Linux Performance Monitoring and Debugging Tools
  4. Mommy, I found it! – 15 Practical Linux Find Command Examples
  5. Linux 101 Hacks 2nd Edition eBook Linux 101 Hacks Book

Bash 101 Hacks Book Sed and Awk 101 Hacks Book Nagios Core 3 Book Vim 101 Hacks Book

{ 2 comments… add one }

Leave a Comment