Types of Inheritance in C++: As we have already learned regarding inheritance in the previous articles. Let us learn about the different types of inheritances present in C++.
Types of Inheritance in C++
- Single Inheritance
- Multiple Inheritance
- Hierarchical Inheritance
- Multilevel Inheritance
- Hybrid Inheritance
Single Inheritance
The single inheritance can be used in such a way that one derived class inherits from only one base class. And it is also known as the simplest form of inheritance.
Syntax:
class subclass_name : access_mode base_class
{
//body of subclass
};
Example:
#include<iostream> using namespace std; //base class class Vehicle { public: Vehicle() { cout<<"This is a Vehicle"<<endl; } }; //subclass derived from two base classes class Car: public Vehicle{ }; //main function int main() { //creating object of sub class will //invoke the constructor of base classes Car obj; return 0; }
Output:
This is a Vehicle
Multiple Inheritance
The multiple inheritances a single derived class may inherit from two or more base classes.
Syntax:
class A
{
……….
};
class B
{
………..
} ;
class C: acess_specifier A,access_specifier A // derived class from A and B
{
………..
} ;
Example:
#include<iostream> using namespace std; //first base class class Vehicle { public: Vehicle() { cout<<"This is a Vehicle"<< endl; } }; //second base class class FourWheeler { public: FourWheeler() { cout<<"This is a 4 wheeler Vehicle" << endl; } }; //subclass derived from two base classes class Car: public Vehicle, public FourWheeler { }; //main function int main() { //creating object of sub class will //invoke the constructor of base classes Car obj; return 0; }
Output:
This is a Vehicle
This is a 4 wheeler Vehicle
Hierarchical Inheritance
The hierarchical inheritance can be used to derive more than one subclass is inherited from a single base class.
Syntax:
class A // base class
{
…………..
};
class B: access_specifier A // derived class from A
{
………..
} ;
class C: access_specifier A // derived class from A
{
………..
} ;
class D: access_specifier A // derived a class from A
{
………..
} ;
Example:
#include<iostream> using namespace std; //base class class Vehicle { public: Vehicle() { cout<<"This is a Vehicle" << endl; } }; //first sub class class Car: public Vehicle { }; //second sub class class Bus: public Vehicle { }; //main function int main() { //creating object of sub class will //invoke the constructor of base class Car obj1; Bus obj2; return 0; }
Output:
This is a Vehicle
This is a Vehicle
Multilevel Inheritance
The multi-level inheritance can be used if the derived class is obtained from another derived class.
Example:
#include<iostream> using namespace std; // base class class Vehicle { public: Vehicle() { cout << "This is a Vehicle" << endl; } }; class fourWheeler: public Vehicle { public: fourWheeler() { cout<<"Objects with 4 wheels are vehicles"<<endl; } }; // sub class derived from two base classes class Car: public fourWheeler{ public: car() { cout<<"Car has 4 Wheels"<<endl; } }; // main function int main() { //creating object of sub class will //invoke the constructor of base classes Car obj; return 0; }
Output:
This is a Vehicle
Objects with 4 wheels are vehicles
The car has 4 Wheels
Hybrid (virtual) Inheritance
The hybrid inheritance can be used for combining more than one type of inheritance.
Example: Combining hierarchical and multiple inheritances
#include <iostream> using namespace std; //base class class Vehicle { public: Vehicle() { cout<<"This is a Vehicle" << endl; } }; //base class class Fare { public: Fare() { cout<<"Fare of Vehicle\n"; } }; // first sub class class Car: public Vehicle { }; // second sub class class Bus: public Vehicle, public Fare { }; // main function int main() { // creating object of sub class will // invoke the constructor of base class Bus obj2; return 0; }