Inheritance in C++ programming
Introduction
What is Inheritance in C++

Advantages of Inheritance in C++
Types of Inheritance in c++
- Single Inheritance
- Multiple Inheritance
- Hierarchical Inheritance
- Multilevel Inheritance
- Hybrid Inheritance
Single Inheritance

Example
#include <iostream>
using namespace std;
class Account
{
public:
float salary = 75000;
};
class Programmer: public Account
{
public:
float bonus = 10000;
};
int main(void)
{
Programmer p1;
cout<<"Salary: "<<p1.salary<<endl;
cout<<"Bonus: "<<p1.bonus<<endl;
return 0;
}
Output
Salary: 75000
Bonus: 10000
Example
#include <iostream>
using namespace std;
class A
{
protected:
int a;
public:
void get_a(int n)
{
a = n;
}
};
class B
{
protected:
int b;
public:
void get_b(int n)
{
b = n;
}
};
class C : public A,public B
{
public:
void display()
{
std::cout << "The value of a is : " <<a<< std::endl;
std::cout << "The value of b is : " <<b<< std::endl;
cout<<"The addition of a and b is : "<<a+b;
}
};
int main()
{
C c;
c.get_a(120);
c.get_b(80);
c.display();
return 0;
}
Output
The value of a is : 120
The value of b is : 80
The addition of a and b is : 200
Hierarchical Inheritance

Example
#include <iostream>
using namespace std;
class Shape // Declaration of base class.
{
public:
int X;
int Y;
void get_data(int n,int m)
{
X= n;
Y= m;
}
};
class Rectangle : public Shape // inheriting Shape class
{
public:
int rect_area()
{
int result = X*Y;
return result;
}
};
class Triangle : public Shape // inheriting Shape class
{
public:
int triangle_area()
{
float result = 0.5*X*Y;
return result;
}
};
int main()
{
Rectangle r;
Triangle t;
int length,breadth,base,height;
std::cout << "Enter the length and breadth of a rectangle: " << std::endl;
cin>>length>>breadth;
r.get_data(length,breadth);
int m = r.rect_area();
std::cout << "Area of the rectangle is : " <<m<< std::endl;
std::cout << "Enter the base and height of the triangle: " << std::endl;
cin>>base>>height;
t.get_data(base,height);
float n = t.triangle_area();
std::cout <<"Area of the triangle is : " << n<<std::endl;
return 0;
}
Output
Enter the length and breadth of a rectangle:
24
20
Area of the rectangle is : 480
Enter the base and height of the triangle:
2
5
Area of the triangle is : 5
Multilevel Inheritance

Example
#include <iostream>
using namespace std;
class Animal
{
public:
void eat()
{
cout<<"Eating..."<<endl;
}
};
class Dog: public Animal
{
public:
void bark()
{
cout<<"Barking..."<<endl;
}
};
class BabyDog: public Dog
{
public:
void sleep()
{
cout<<"Sleeping...";
}
};
int main(void)
{
BabyDog d1;
d1.eat();
d1.bark();
d1.sleep();
return 0;
}
Output
Eating...
Barking...
Sleeping...
Hybrid Inheritance
Example
#include <iostream>
using namespace std;
class A
{
protected:
int a;
public:
void get_a()
{
std::cout << "Enter the value of 'a' : " << std::endl;
cin>>a;
}
};
class B : public A
{
protected:
int b;
public:
void get_b()
{
std::cout << "Enter the value of 'b' : " << std::endl;
cin>>b;
}
};
class C
{
protected:
int c;
public:
void get_c()
{
std::cout << "Enter the value of c is : " << std::endl;
cin>>c;
}
};
class D : public B, public C
{
protected:
int d;
public:
void mul()
{
get_a();
get_b();
get_c();
std::cout << "Multiplication of a,b,c is : " <<a*b*c<< std::endl;
}
};
int main()
{
D d;
d.mul();
return 0;
}
Output
Enter the value of 'a' :
120
Enter the value of 'b' :
20
Enter the value of c is :
310
Multiplication of a,b,c is : 744000
Visibility mode in Inheritance in C++
- Public- if the member is declared as a public member then it is accessible to all the functions of c++ program
- Private- if the member is declared as a private member the it is only accessible by some particular class
- Protected- if the member is declared as a protected member then only its own class and the derived class can only access them
Visibility of the inherited member
Base class visibility | Derived class visibility | ||
Public | Private | Protected | |
Private | Not Inherited | Not Inherited | Not Inherited |
Protected | Protected | Private | Protected |
Public | Public | Private | Protected |
Summary
Learning about inheritance in C++ programming can help you better understand how objects are related to one another. This can be a helpful tool when working with large programs. If you want to learn more about inheritance in C++ programming this article can give you a vast idea about inheritance, other than this there are many resources available online and in libraries. Do some research to find the resources that will work best for you and your learning style. You can also find helpful resources on our website. inheritance in c++ including visibility mode in c++ provides a lot of benefits for programmers. It helps them to reuse code and make their programs more efficient. Thanks for reading!
Take our free skill tests to evaluate your skill!

In less than 5 minutes, with our skill test, you can identify your knowledge gaps and strengths.