Types of Inheritance in C++
Last Updated :
07 Aug, 2025
The inheritance can be classified on the basis of the relationship between the derived class and the base class.
In C++, we have 5 types of inheritances:
- Single Inheritance
- Multilevel Inheritance
- Multiple Inheritance
- Hierarchical Inheritance
- Hybrid Inheritance
1.Single Inheritance
In single inheritance, a class is allowed to inherit from only one class. i.e one base class is inherited by one derived class only.
C++
#include <iostream>
using namespace std;
class Animal{
public:
void eat(){
cout<<"Eating"<<endl;
}
};
class Dog:public Animal{
public:
void bark(){
cout<<"Barking"<<endl;
}
};
int main() {
Dog myDog ;
myDog.eat();
myDog.bark();
return 0;
}
2.Multilevel Inheritance
In multilevel inheritance, a derived class is created from another derived class and that derived class can be derived from a base class or any other derived class. There can be any number of levels.
C++
#include <bits/stdc++.h>
using namespace std;
class Vehicle {
public:
Vehicle() {
cout << "This is a Vehicle"<< endl;
}
};
class fourWheeler : public Vehicle {
public:
fourWheeler() {
cout << "4 Wheeler Vehicles"<< endl;
}
};
class Car : public fourWheeler {
public:
Car() {
cout << "This 4 Wheeler Vehical is a Car";
}
};
int main() {
// Creating object of sub class will
// invoke the constructor of base classes.
Car obj;
return 0;
}
OutputThis is a Vehicle
4 Wheeler Vehicles
This 4 Wheeler Vehical is a Car
3.Multiple Inheritance
Multiple Inheritance is a feature of C++ where a class can inherit from more than one class,i.e one subclass is inherited from more than one base class.
C++
#include <bits/stdc++.h>
using namespace std;
class LandVehicle {
public:
LandVehicle() {
cout << "This is a LandVehicle"<< endl;
}
};
class WaterVehicle {
public:
WaterVehicle() {
cout << "This is a WaterVehicle"<< endl;
}
};
// sub class derived from two base classes
class AmphibiousVehicle : public WaterVehicle, public LandVehicle {
public:
AmphibiousVehicle() {
cout << "This is an AmphibiousVehicle"<< endl;
}
};
int main() {
// Creating object of sub class will
// invoke the constructor of base classes.
AmphibiousVehicle obj;
return 0;
}
OutputThis is a WaterVehicle
This is a LandVehicle
This is an AmphibiousVehicle
4. Hierarchical Inheritance
In hierarchical inheritance, more than one subclass is inherited from a single base class. i.e. more than one derived class is created from a single base class. For example, cars and buses both are vehicle.
C++
#include <bits/stdc++.h>
using namespace std;
class Vehicle {
public:
Vehicle() {
cout << "This is a Vehicle"<< endl;
}
};
class Car : public Vehicle {
public:
Car() {
cout << "This Vehicle is Car"<< endl;
}
};
class Bus : public Vehicle {
public:
Bus() {
cout << "This Vehicle is Bus"<< endl;
}
};
int main() {
// Creating object of sub class will
// invoke the constructor of base class.
Car obj1;
Bus obj2;
return 0;
}
OutputThis is a Vehicle
This Vehicle is Car
This is a Vehicle
This Vehicle is Bus
5. Hybrid Inheritance
Hybrid Inheritance is implemented by combining more than one type of inheritance. For example: Combining Hierarchical inheritance and Multiple Inheritance will create hybrid inheritance in C++.
There is no particular syntax of hybrid inheritance. We can just combine two of the above inheritance types.
C++
#include <bits/stdc++.h>
using namespace std;
class Vehicle
{
public:
Vehicle()
{
cout << "This is a Vehicle" << endl;
}
};
class Fare
{
public:
Fare()
{
cout << "Fare of Vehicle" << endl;
}
};
class Car : public Vehicle
{
public:
Car()
{
cout << "This Vehical is a Car" << endl;
}
};
class Bus : public Vehicle, public Fare
{
public:
Bus()
{
cout << "This Vehicle is a Bus with Fare";
}
};
int main()
{
// Creating object of sub class will
// invoke the constructor of base class.
Bus obj2;
return 0;
}
OutputThis is a Vehicle
Fare of Vehicle
This Vehicle is a Bus with Fare