Modes of inheritance in C++

Yash Paneliya
4 min readJul 12, 2022

--

C++ supports multiple types of inheritance like simple, multiple, multilevel, hybrid, and hierarchical. Whenever we inherit a class in c++ we need to specify the mode of inheritance.

There are three types of inheritance modes:

  1. Private (default)
  2. Public
  3. Protected

I have taken the below table from javatpoint.com which describes the visibility of parent class members to its child class.

It is hard to get the exact idea of how it will behave in each scenario. I have written an example code with two classes Parent and Child and will try to describe modes of inheritance and their behavior.

Note: In any of the visibility modes, private members are not inherited.

Important Notations:

  1. Error-1: Cannot access private members outside the class.
  2. Error-2: Cannot inherit private member.
  3. Error-3: Cannot access protected member outside the class.

Visibility mode: Private

Theory:

  • When the mode of inheritance is private, all the protected and public members behave as private members of the child class.
#include<iostream>
using namespace std;
class Parent{
int age; //private member
public:
int salary;
Parent(){
age=30;
salary=20000;
}
void display(){
cout<<"Age: "<<age<<" Salary: "<<salary<<endl;
}
int getage(){
return age;
}
};
class Child:private Parent{
int bonus; // private member
public:
void data(){
cout<<"Age: "<<getage()<<" Salary: "<<salary<<endl;
//cout<<age<<endl; //Error-2
}
void addAge(){
cout<<"Adding 5 to age: "<<getage()+5<<endl;
}
void addBonus(){
cout<<"Adding bonus: "<<salary+5000<<endl;
}
};
int main(){
Parent p;
Child c;
//c.display(); // Error-2
//c.getage(); // Error-2
c.data();
c.addAge();
c.addBonus();
//cout<<c.salary<<endl; // Error-1
return 0;
}

Explanation:

salary and age are public and private members of the parent class respectively. Now, when we try to access age inside a child's method (void data()), we get an error, because age is a private member of the parent and can’t be accessed by the child object. So, to access age we use getters and setters (getage() method). We can access salary directly in child methods because it is public in the parent and thus private to the child.

Note: getter and setter must be public for this case

When we try to access public methods of Parent through child object (c.display() and c.getage()) outside the class, we get an error because both of these methods have become private members of the child class due to private visibility. So, they can’t be accessed outside the class.

Visibility mode: public

Theory:

  • All the public members become public and protected become protected to the child class.
#include<iostream>
using namespace std;
class Parent{
int age;
public:
int salary;
Parent(){
age=30;
salary=20000;
}
void display(){
cout<<"Age: "<<age<<" Salary: "<<salary<<endl;
}
int getage(){
return age;
}
};
class Child:public Parent{
int bonus;
public:
void data(){
cout<<"Age: "<<getage()<<" Salary: "<<salary<<endl;
}
void addAge(){
cout<<"Adding 5 to age: "<<getage()+5<<endl;
}
void addBonus(){
cout<<"Adding bonus: "<<salary+5000<<endl;
}
};
int main(){
Parent p;
Child c;
// calling public members of parent from child object
c.display();
c.getage();
cout<<c.salary<<endl;
return 0;
}

Explanation:

Now, we can access the public members of the parent class through a child object outside of the child class directly. We still can not access private members of the parent class inside or outside of the child class.

Visibility mode: Protected

Theory:

  • All the public and protected members of the parent class become protected of the child class.
#include<iostream>
using namespace std;
class Parent{
int age;
protected:
string name;
public:
int salary;
Parent(){
age=30;
salary=20000;
name="temp";
}
void display(){
cout<<"Age: "<<age<<" Salary: "<<salary<<endl;
}
int getage(){
return age;
}
};
class Child:protected Parent{
int bonus;
public:
void data(){
cout<<"Name: "<<name<<" Age: "<<getage()<<" Salary: "<<salary<<endl;
}
void addAge(){
cout<<"Adding 5 to age: "<<getage()+5<<endl;
}
void addBonus(){
cout<<"Adding bonus: "<<salary+5000<<endl;
}
};
int main(){
Parent p;
Child c;
c.data();

// Error-3 in below 3 lines of code
c.getage();
cout<<c.name<<endl;
cout<<c.salary<<endl;
return 0;
}

Explanation:

name is a protected member of the Parent class and when inherited, remains protected in the Child class. All the public members (salary, getage(), display()) become protected members of Child. We can access them inside the Child class. But, if we try to access these members outside the Child class, we get an error.

Conclusion

Try out all the above examples and you will understand different scenarios of inheritance. If you find something wrong or any mistake in the blog then please do comment. It’ll help other learners and me also.

Give some claps if you find this article helpful and share this with your peers!!

Know more about me: https://linktr.ee/yashpaneliya

--

--

Yash Paneliya

Building @Krutrim | Data Science Intern @NPCI | MTech CSE'24 IIT KGP | Developer | Trying to simplify things as much as I can | linktr.ee/yashpaneliya