codingstuff.io
ExploreTutorialsProblemsCS Subjects
Get Started
ExploreTutorialsProblemsCS Subjects
Get Started
codingstuff.io

Master the art of building software through interactive tutorials, real-world problems, and guided projects.

Pune, Maharashtra, India

codingstuffmail@gmail.com

Product

  • Explore
  • Tutorials
  • Problems
  • CS Subjects

Company

  • About
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Sitemap

© 2026 codingstuff.io. All rights reserved.

Built with ❤️ for developers everywhere

/
/
All Tutorials
⚡

C++ Programming

50 / 87 topics
48Inheritance Basics49Public, Protected, and Private Inheritance50Types of Inheritance51Polymorphism52Function Overriding53Virtual Functions54Abstract Class and Pure Virtual Function
Tutorials/C++ Programming/Types of Inheritance
⚡C++ Programming

Types of Inheritance

Updated 2026-04-20
3 min read

Introduction

Inheritance is a fundamental concept in object-oriented programming (OOP) that allows a class to inherit properties and methods from another class. This tutorial will explore the different types of inheritance available in C++, including single, multiple, multilevel, hierarchical, and hybrid inheritance. Understanding these types is crucial for designing robust and maintainable software systems.

Single Inheritance

Single inheritance is the simplest form of inheritance where a derived class inherits from only one base class. This type of inheritance is straightforward and commonly used in scenarios where a clear parent-child relationship exists.

Example

#include <iostream>
using namespace std;

class Animal {
public:
    void eat() {
        cout << "This animal eats." << endl;
    }
};

class Dog : public Animal {
public:
    void bark() {
        cout << "The dog barks." << endl;
    }
};

int main() {
    Dog myDog;
    myDog.eat();  // Inherited method
    myDog.bark(); // Derived class method
    return 0;
}

Explanation

  • Animal is the base class with a method eat().
  • Dog is the derived class that inherits from Animal and adds its own method bark().

Multiple Inheritance

Multiple inheritance allows a derived class to inherit from more than one base class. This can be useful when a class needs to combine features from multiple classes. However, it also introduces complexity and potential issues such as the "diamond problem."

Example

#include <iostream>
using namespace std;

class Vehicle {
public:
    void start() {
        cout << "Vehicle starts." << endl;
    }
};

class Flyable {
public:
    void fly() {
        cout << "It can fly." << endl;
    }
};

class Airplane : public Vehicle, public Flyable {
public:
    void land() {
        cout << "Airplane lands." << endl;
    }
};

int main() {
    Airplane myPlane;
    myPlane.start(); // Inherited from Vehicle
    myPlane.fly();   // Inherited from Flyable
    myPlane.land();  // Derived class method
    return 0;
}

Explanation

  • Vehicle and Flyable are base classes.
  • Airplane inherits from both Vehicle and Flyable.

Multilevel Inheritance

Multilevel inheritance involves a derived class inheriting from another derived class, forming a chain of inheritance. This can be useful for creating complex hierarchies but should be used judiciously to avoid unnecessary complexity.

Example

#include <iostream>
using namespace std;

class Animal {
public:
    void eat() {
        cout << "This animal eats." << endl;
    }
};

class Mammal : public Animal {
public:
    void breathe() {
        cout << "It breathes air." << endl;
    }
};

class Dog : public Mammal {
public:
    void bark() {
        cout << "The dog barks." << endl;
    }
};

int main() {
    Dog myDog;
    myDog.eat();     // Inherited from Animal
    myDog.breathe(); // Inherited from Mammal
    myDog.bark();    // Derived class method
    return 0;
}

Explanation

  • Animal is the base class.
  • Mammal inherits from Animal.
  • Dog inherits from Mammal.

Hierarchical Inheritance

Hierarchical inheritance involves multiple derived classes inheriting from a single base class. This type of inheritance is useful when several classes share common properties and behaviors.

Example

#include <iostream>
using namespace std;

class Vehicle {
public:
    void start() {
        cout << "Vehicle starts." << endl;
    }
};

class Car : public Vehicle {
public:
    void drive() {
        cout << "Car drives on roads." << endl;
    }
};

class Bike : public Vehicle {
public:
    void ride() {
        cout << "Bike rides on paths." << endl;
    }
};

int main() {
    Car myCar;
    myCar.start(); // Inherited from Vehicle
    myCar.drive(); // Derived class method

    Bike myBike;
    myBike.start(); // Inherited from Vehicle
    myBike.ride();  // Derived class method
    return 0;
}

Explanation

  • Vehicle is the base class.
  • Car and Bike inherit from Vehicle.

Hybrid Inheritance

Hybrid inheritance combines two or more types of inheritance into a single structure. It can be complex and should be used only when necessary, as it increases the risk of ambiguity and other issues.

Example

#include <iostream>
using namespace std;

class Vehicle {
public:
    void start() {
        cout << "Vehicle starts." << endl;
    }
};

class Flyable {
public:
    void fly() {
        cout << "It can fly." << endl;
    }
};

class Car : public Vehicle {
public:
    void drive() {
        cout << "Car drives on roads." << endl;
    }
};

class Airplane : public Vehicle, public Flyable {
public:
    void land() {
        cout << "Airplane lands." << endl;
    }
};

int main() {
    Airplane myPlane;
    myPlane.start(); // Inherited from Vehicle
    myPlane.fly();   // Inherited from Flyable
    myPlane.land();  // Derived class method

    Car myCar;
    myCar.start(); // Inherited from Vehicle
    myCar.drive(); // Derived class method
    return 0;
}

Explanation

  • Vehicle and Flyable are base classes.
  • Car inherits from Vehicle.
  • Airplane inherits from both Vehicle and Flyable.

Best Practices

  1. Avoid Deep Inheritance Hierarchies: Keep the inheritance chain shallow to maintain simplicity and reduce complexity.
  2. Use Virtual Base Classes: To prevent ambiguity in multiple inheritance, use virtual base classes.
  3. Favor Composition Over Inheritance: When possible, prefer composition (using objects of other classes) over inheritance to achieve code reuse.
  4. Document Inheritance Relationships: Clearly document the inheritance relationships and their implications for maintainability.

Conclusion

Understanding the different types of inheritance in C++ is essential for effective object-oriented design. Each type has its own use cases and potential pitfalls, so it's important to choose the right one based on the specific requirements of your application. By following best practices and carefully considering the design implications, you can create robust and maintainable software systems.


PreviousPublic, Protected, and Private InheritanceNext Polymorphism

Recommended Gear

Public, Protected, and Private InheritancePolymorphism