Hybrid Inheritance In C++

Last Updated : 27 Jun, 2026

Hybrid inheritance is a type of inheritance in C++ that combines two or more inheritance types within a single class hierarchy. It allows a program to use the advantages of multiple inheritance structures together to model complex real-world relationships.

  • Combines multiple inheritance types such as single, multiple, multilevel, and hierarchical inheritance.
  • Provides greater flexibility in designing class hierarchies.
  • Can lead to ambiguity issues such as the diamond problem.
Hybrid-Inheritance-in-CPP
Hybrid Inheritance

Working of Hybrid Inheritance

In hybrid inheritance, different inheritance relationships coexist in the same hierarchy. A class may inherit from multiple classes while also participating in a multilevel or hierarchical inheritance chain.

  • Combines features from different inheritance models.
  • Improves code reuse across related classes.
  • Helps represent complex relationships more naturally.
  • Requires careful design to avoid ambiguity.

Example1: Hybrid Inheritance Using Hierarchical and Multiple Inheritance

C++
#include <bits/stdc++.h>
using namespace std;

// Base class
class Person {
protected:
    string name;

public:
    Person(const string& name)
        : name(name)
    {
    }
    void display() { cout << "\nName: " << name << endl; }
};

// Derived class 1: Employee (Single Inheritance)
class Employee : public Person {
protected:
    int employeeId;

public:
    Employee(const string& name, int id)
        : Person(name)
        , employeeId(id)
    {
    }
    void displayEmployee()
    {
        display();
        cout << "Employee ID: " << employeeId << endl;
        cout << "Method inside Derived Class Employee"
             << endl;
    }
};

// Derived class 2: Student (Single Inheritance)
class Student : public Person {
protected:
    int studentId;

public:
    Student(const string& name, int id)
        : Person(name)
        , studentId(id)
    {
    }
    void displayStudent()
    {
        display();
        cout << "Student ID: " << studentId << endl;
        cout << "Method inside Derived Class Student"
             << endl;
    }
};

// Derived class 3: StudentIntern (Multiple Inheritance)
class StudentIntern : public Employee, public Student {
public:
    StudentIntern(const string& name, int empId, int stuId)
        : Employee(name, empId)
        , Student(name, stuId)
    {
    }
    void displayStudentIntern()
    {
        cout << "Methods inside Derived Class "
                "StudentIntern : "
             << endl;
        displayEmployee();
        displayStudent();
    }
};

// driver code
int main()
{
    StudentIntern SI("Riya", 67537, 2215);
    SI.displayStudentIntern();

    return 0;
}

Output
Methods inside Derived Class StudentIntern : 

Name: Riya
Employee ID: 67537
Method inside Derived Class Employee

Name: Riya
Student ID: 2215
Method inside Derived Class Student

Explanation:

  • Employee and Student are derived from Person (hierarchical inheritance).
  • StudentIntern inherits from both Employee and Student (multiple inheritance).
  • Together, they form a hybrid inheritance structure.

Diamond Problem in Hybrid Inheritance

One common issue in hybrid inheritance is the diamond problem. When two classes inherit from the same base class and another class inherits from both of them, multiple copies of the base class may be created.

example-of-hybrid-inheritance-in-cpp

In this hierarchy:

  • Employee inherits Person.
  • Student inherits Person.
  • Intern inherits both Employee and Student.

As a result, Intern may contain two copies of Person, causing ambiguity.

Example 2: Hybrid Inheritance Using Hierarchical and Multilevel Inheritance

C++
#include <bits/stdc++.h>
using namespace std;

// Base class 1
class Meal {
public:
    void print()
    {
        cout << "Different types of meals are served :"
             << endl;
    }
};

// Derived class 1 from Meal (Hierarchical Inheritance)
class Breakfast : public Meal {
public:
    void print()
    {
        cout << "\nBreakfast is a type of meal." << endl;
    }
};

// Derived class from breakfast (Multilevel Inheritance)
class Milk : public Breakfast {
public:
    void print()
    {
        cout << "Milk served in breakfast." << endl;
    }
};

// Derived class from Milk (Multilevel Inheritance)
class Yoghurt : public Milk {
public:
    void print()
    {
        cout << "Yoghurt made from milk." << endl;
    }
};

// Derived class 2 from Meal (Hierarchical Inheritance)
class Dessert : public Meal {
public:
    void print()
    {
        cout << "\nDessert is a type of meal." << endl;
    }
};

// Derived class from Dessert (Multilevel Inheritance)
class Sweets : public Dessert {
public:
    void print()
    {
        cout << "Sweets served in Dessert." << endl;
    }
};

// Derived class from Sweets (Multilevel Inheritance)
class Pastry : public Sweets {
public:
    void print()
    {
        cout << "Pastry is a type of sweet." << endl;
    }
};

int main()
{
    Meal types;
    Breakfast servedBreakfast;
    Milk milk;
    Yoghurt yoghurt;
    Dessert servedDessert;
    Sweets sweet;
    Pastry pastry;

    types.print();
    servedBreakfast.print();
    milk.print();
    yoghurt.print();
    servedDessert.print();
    sweet.print();
    pastry.print();

    return 0;
}