Types of Inheritance in C++ with Examples

Types of Inheritance in C++ with Examples

03 Jul 2024
Advanced
408 Views
17 min read
Learn via Video Course & by Doing Hands-on Labs

Free C++ Course Online with Certificate [Full Course]

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

Live Classes Schedule

Our learn-by-building-project method enables you to build practical/coding experience that sticks. 95% of our learners say they have confidence and remember more when they learn by building real world projects.
ASP.NET Core Certification TrainingSep 21SAT, SUN
Filling Fast
09:30AM to 11:30AM (IST)
Get Details
Advanced Full-Stack .NET Developer Certification TrainingSep 21SAT, SUN
Filling Fast
09:30AM to 11:30AM (IST)
Get Details
Software Architecture and Design TrainingSep 22SAT, SUN
Filling Fast
07:00AM to 09:00AM (IST)
Get Details
.NET Solution Architect Certification TrainingSep 22SAT, SUN
Filling Fast
07:00AM to 09:00AM (IST)
Get Details
ASP.NET Core Certification TrainingSep 29SAT, SUN
Filling Fast
08:30PM to 10:30PM (IST)
Get Details
Advanced Full-Stack .NET Developer Certification TrainingSep 29SAT, SUN
Filling Fast
08:30PM to 10:30PM (IST)
Get Details
Angular Certification TrainingOct 06SAT, SUN
Filling Fast
08:30PM to 10:30PM (IST)
Get Details
ASP.NET Core ProjectOct 13SAT, SUN
Filling Fast
10:00AM to 12:00PM (IST)
Get Details

Can't find convenient schedule? Let us know

About Author
Shailendra Chauhan (Microsoft MVP, Founder & CEO at Scholarhat by DotNetTricks)

Shailendra Chauhan is the Founder and CEO at ScholarHat by DotNetTricks which is a brand when it comes to e-Learning. He provides training and consultation over an array of technologies like Cloud, .NET, Angular, React, Node, Microservices, Containers and Mobile Apps development. He has been awarded Microsoft MVP 8th time in a row (2016-2023). He has changed many lives with his writings and unique training programs. He has a number of most sought-after books to his name which has helped job aspirants in cracking tough interviews with ease.
Self-paced Membership
  • 24+ Video Courses
  • 825+ Hands-On Labs
  • 400+ Quick Notes
  • 125+ Skill Tests
  • 10+ Interview Q&A Courses
  • 10+ Real-world Projects
  • Career Coaching Sessions
  • Email Support
Upto 60% OFF
Know More
Accept cookies & close this