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.

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
#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.

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
#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;
}
Output
Different types of meals are served : Breakfast is a type of meal. Milk served in breakfast. Yoghurt made from milk. Dessert is a type of meal. Sweets served in Dessert. Pastry is a type of sweet.
Explanation:
Mealacts as the common base class.- Breakfast and Dessert inherit from Meal, forming hierarchical inheritance.
- Milk -> Yoghurt and Sweets -> Pastry extend the hierarchy further, forming multilevel inheritance.
- Since the hierarchy combines both hierarchical and multilevel inheritance, it represents hybrid inheritance.

Advantages of Hybrid Inheritance
Hybrid inheritance provides flexibility by combining the benefits of multiple inheritance models within a single class hierarchy.
- Promotes maximum code reuse across related classes.
- Helps model complex real-world relationships effectively.
- Allows combining features from different inheritance structures.
Limitations of Hybrid Inheritance
Hybrid inheritance can increase complexity and requires careful class design.
- May introduce ambiguity such as the diamond problem.
- Makes class hierarchies harder to understand and maintain.
- Increases the need for virtual inheritance and careful planning.