Year End Sale: Get Upto 40% OFF on Live Training! Offer Ending in
D
H
M
S
Get Now
Types of Inheritance in C++ with Examples

Types of Inheritance in C++ with Examples

03 Jul 2024
Advanced
799 Views
17 min read
Learn with an interactive course and practical hands-on labs

Free C++ Course: Learn C++ In 21 Days

Types of Inheritance in C++: Overview

In the previous C++ tutorial, we saw the basics of Inheritance in C++. Now, we will move a step forward in our journey of learning OOP concepts in C++ and look at types of inheritance in C++. They constitute an important aspect of inheritance. For more information, check our C++ Certification Course.

Types of Inheritance in C++

C++ supports the following five types of inheritance:
  1. Single inheritance
  2. Multiple inheritance
  3. Multilevel inheritance
  4. Hierarchical inheritance
  5. Hybrid inheritance

Single Inheritance in C++

When a child/derived class inherits only one base/parent class, it is called a single inheritance.

Single Inheritance

Example to demonstrate single inheritance


#include <iostream>
using namespace std;
class Vehicle
{
 public:
 int speed;
 void StartEngine()
 {
 cout << "Engine started." << endl;
 }
};

class Car : public Vehicle
{
 public:
 void Accelerate()
 {
 cout << "Car is accelerating." << endl;
 }
};

int main()
{
 Car myCar; // object of the Car class
 myCar.speed = 60; // member of the base class
 myCar.StartEngine(); // member function of the base class
 myCar.Accelerate(); // member function of the derived class

 return 0;
}

Here, the derived class Car inherits a single base class i.e. Vehicle.

Output

Engine started.
Car is accelerating.

Read More - C++ Interview Questions Interview Questions for Freshers

Multiple Inheritance in C++

When a child/derived class inherits from more than one base/parent class, it is called multiple inheritance.

Multiple Inheritance

Example to demonstrate multiple inheritance


#include <iostream>
using namespace std;

class Birds {
 public:
 Birds() {
 cout << "Birds are of two types" << endl;
 }
};

class Oviparous {
 public:
 Oviparous() {
 cout << "Oviparous birds lay eggs." << endl;
 }
};

class Sparrow: public Birds, public Oviparous {
public:
 void display() {
 cout << "I am a sparrow." << endl;
 }
};

int main() {
 Sparrow h1;
 h1.display();
 return 0;
}

Here the class Sparrow is derived from base classes Birds and Oviparous. It makes sense because Sparrow is a bird as well as oviparous.

Output

Birds are of two types
Oviparous birds lay eggs.
  • Ambiguity in Multiple Inheritance: Suppose, two base classes have the same function. When you call that function using the object of the derived class, the compiler shows an error. It's because the compiler doesn't know which function to call.

    Example

    
    #include <iostream>
    using namespace std; 
    class base1 
    { 
     public: 
     void display() 
     { 
     cout << "Class base1" << endl; 
     } 
    }; 
    class base2 
    { 
     public: 
     void display() 
     { 
     cout << "Class base2" << endl; 
     } 
    }; 
    class derived : public base1, public base2 
    { 
     void view() 
     { 
     display(); 
     } 
    }; 
    int main() 
    { 
     derived d; 
     d.display(); 
     return 0; 
    } 
    

    Here the method display() is defined in both base classes, base1 and base2. The derived class method view() contains the function call to display() method. When the object d of the derived class calls the display() method, the compiler gets confused about which display() to call, the base1 class or the base2 class. Thus, it throws an error.

    Output

    error: reference to 'display' is ambiguous
     display();
    

    This ambiguity could be resolved in two ways

    • using the scope resolution operator ::. In the above example, it can be resolved in two ways
      • class derived : public base1, public base2 
        { 
         void view() 
         { 
         base1::display(); // Calling the display() function of class base1
         base2::display(); // Calling the display() function of class base2
         } 
        }; 
      • int main() {
         d.base1::display(); // Function of base1 class is called
         d.base2::display(); // Function of base2 class is called.
        }
    • The above error would not have occurred if the display() method had been defined in the derived class as well. In other words, if the display() method had been overridden in the derived class. We will understand this in the Polymorphism in C++ and C++ Function Overriding tutorial.

Multilevel Inheritance in C++

When one base class is inherited by a derived class which is further inherited by any other derived class, it is known as multilevel inheritance. Here, a derived class becomes the base class of another derived class. In the end, the last derived class acquires all the members of all its base classes.

Example to demonstrate multilevel inheritance


#include <iostream>
using namespace std; 
class base
{ 
 public: 
 void display() 
 { 
 cout << "Class base1" << endl; 
 } 
}; 
class derived1 : public base
{ 
 public: 
 void print() 
 { 
 cout << "This is the 1st derived class" << endl; 
 } 
}; 
class derived2: public derived1
{ 
 void view() 
 { 
 cout << "This is the 2nd derived class" << endl; 
 } 
}; 
int main() 
{ 
 derived2 d; 
 d.display();
 d.print();
 d.view(); 
 return 0; 
} 

Here, class derived1 is inheriting the class, base which is further inherited by the derived2 class. The object d of the derived2 class accesses all the methods of the base classes, base and derived1.

Output

Class base1
This is the 1st derived class
This is the 2nd derived class

Hierarchical Inheritance in C++

If more than one derived class inherits a single base class, a hierarchy of base classes is formed and so it is known as hierarchical inheritance.

Hierarchical Inheritance

Example to demonstrate hierarchical inheritance


#include <iostream>
using namespace std;
class Birds {
 public:
 void type() {
 cout << "I'm an oviparous bird" << endl;
 }
};

class pigeon: public Birds{
 public:
 void name1() {
 cout << "I am a pigeon." << endl;
 }
};

class Sparrow: public Birds {
 public:
 void name2() {
 cout << "I am a sparrow." << endl;
 }
};

int main() {
 pigeon p; // object of pigeon class
 cout << "\npigeon Class:" << endl;
 p.type(); // base class method
 p.name1();

 Sparrow h; // object of Sparrow class
 cout << "\nSparrow Class:" << endl;
 h.type(); // base class method
 h.name2();

 return 0;
} 

Here, both the pigeon and Sparrow classes are derived from the Birds class. As such, both the derived classes can access the type() function belonging to the Birds class.

Output

pigeon Class:
I'm an oviparous bird
I am a pigeon.

Sparrow Class:
I'm an oviparous bird
I am a sparrow.

Hybrid Inheritance in C++

It is the combination of more than one type of inheritance i.e hybrid variety of inheritance

Hybrid Inheritance

Example to demonstrate hybrid inheritance


#include <iostream>
using namespace std;

class Birds {
public:
 Birds() {
 cout << "Birds are of two types" << endl;
 }
};

class Oviparous : public Birds {
public:
 Oviparous() {
 cout << "Oviparous birds lay eggs." << endl;
 }
};

class Sparrow {
public:
 void name2() {
 cout << "I am a sparrow." << endl;
 }
};

class HybridSparrow : public Oviparous, public Sparrow {
public:
 void display() {
 name2();
 cout << "HybridSparrow class is a combination of Oviparous and Sparrow." << endl;
 }
};

int main() {
 HybridSparrow h1;
 h1.display();
 return 0;
}
  • In the above code, class Oviparous inherits from class Birds. This is a single inheritance.
  • The class HybridSparrowinherits from both classOviparous and class Sparrow. This is multiple inheritance.
  • The Oviparousclass inheritsBirdsclass which is further inherited by theHybridSparrowclass. This is a multilevel inheritance.

Output

Birds are of two types
Oviparous birds lay eggs.
I am a sparrow.
HybridSparrow class is a combination of Oviparous and Sparrow.
Summary
Here, we have completed all types of inheritances in C++. Go through the article very sincerely to understand each type in detail. For more information, check our C++ Certification course.
Share Article
About Author
Shailendra Chauhan (Microsoft MVP, Founder & CEO at ScholarHat)

Shailendra Chauhan, Founder and CEO of ScholarHat by DotNetTricks, is a renowned expert in System Design, Software Architecture, Azure Cloud, .NET, Angular, React, Node.js, Microservices, DevOps, and Cross-Platform Mobile App Development. His skill set extends into emerging fields like Data Science, Python, Azure AI/ML, and Generative AI, making him a well-rounded expert who bridges traditional development frameworks with cutting-edge advancements. Recognized as a Microsoft Most Valuable Professional (MVP) for an impressive 9 consecutive years (2016–2024), he has consistently demonstrated excellence in delivering impactful solutions and inspiring learners.

Shailendra’s unique, hands-on training programs and bestselling books have empowered thousands of professionals to excel in their careers and crack tough interviews. A visionary leader, he continues to revolutionize technology education with his innovative approach.
Accept cookies & close this