Hybrid Inheritance In C++
Last Updated :
20 Oct, 2023
Before jumping into Hybrid Inheritance, let us first know what is inheritance. Inheritance is a fundamental OOP concept in C++ that allows a new class, also known as a subclass or derived class, to inherit properties and methods from an already-existing class, also known as a superclass or base class.
C++ supports 5 types of inheritance:
- Single Inheritance: Subclass inherited from a single superclass.
- Multiple Inheritance: Subclass inherited from more than one superclass.
- Multilevel Inheritance: A class inherited from another class, which in turn inherited from another class, creating a chain of inheritance.
- Hierarchical Inheritance: Multiple subclasses inherited from a single superclass.
- Hybrid Inheritance: Combination of any of the above types of inheritance.
Hybrid Inheritance in C++
Hybrid inheritance is a complex form of inheritance in object-oriented programming (OOP). In Hybrid Inheritance, multiple types of inheritance are combined within a single class hierarchy, enabling a varied and flexible structure of classes. In hybrid inheritance, within the same class, we can have elements of single inheritance, multiple inheritance, multilevel inheritance, and hierarchical inheritance.
Hybrid Inheritance
The major goal of hybrid inheritance is to enhance code reusability by making it simpler for programmers to use the methods and attributes that are already present in other classes. Hybrid inheritance has several advantages over other inheritances as it increases the reusability of software elements, enables quicker code development lowers coding errors by avoiding code duplication across classes, and establishes a more clearly defined relationship between classes in object-oriented programming. It also provides a structured way to organize classes with shared attributes and behaviors.
However, Hybrid Inheritance also poses some difficulties, like the possibility of ambiguity or inconsistencies between inherited attributes and methods. While implementing hybrid inheritance, careful design and a thorough understanding of the principles of programming languages are essential to ensure that it achieves its goals successfully and without introducing any unnecessary complications.
Examples of Hybrid Inheritance
Example 1: Using Single Inheritance and Multiple Inheritance
Let us consider a scenario where we have a base class "Person", a derived class "Employee" that uses single inheritance, and another derived class "Student" that also uses single inheritance but combines with "Employee" to create a hybrid inheritance.
Example:
C++
// C++ program to illustrate the hybrid 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;
}
OutputMethods inside Derived Class StudentIntern :
Name: Riya
Employee ID: 67537
Method inside Derived Class Employee
Name: Riya
Student ID: 2215
Method inside Derived Class Student
It is important to note that the "StudentIntern" class exhibits the diamond problem of multiple inheritances as it inherits display() from both "Employee" and "Student" which in turn inherits from "Person".
Explanation
1. An instance of the "StudentIntern" class named "SI" is created in the main function with arguments:
- Name: Riya
- Employee ID: 67537
- Student ID: 2215
2. The "displayStudentIntern" method of the "SI" object is called.
3. Inside the "displayStudentIntern" method,
- Methods inside Derived Class "StudentIntern" is displayed as a header.
- The "displayEmployee" method of the "Employee" class is called.
- Inside "displayEmployee", the "display" method of the "Person" class (base class) is called to display the name along with the employee ID.
- The "displayStudent" method of the "Student" class is called.
Inside "displayStudent", the display method of the "Person" class (base class) is called to display the name again along with the student ID.
Example 2: Using Multilevel Inheritance and Hierarchical Inheritance
C++
// C++ program to illustrate the hybrid inheritance Using
// Multilevel Inheritance and Hierarchical 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;
}
OutputDifferent 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.
This code defines a set of classes and demonstrates multilevel inheritance and hierarchical inheritance creating a hierarchy of different types of meals served and their descriptions.
Explanation
1. "Meal" is the base class that has a single member function "print()" that displays a generic message about different types of meals.
a. "Breakfast" is derived from the class "Meal" which demonstrates Hierarchical Inheritance and overrides the function "print()" to provide information about breakfast.
- "Milk" is derived from "Breakfast" representing Multilevel Inheritance which specializes in information about milk served in breakfast.
- "Yoghurt" is derived from "Milk" representing Multilevel Inheritance.
b. "Dessert" is another derived class from "Meal" which demonstrates Hierarchical Inheritance.
- "Sweets" is derived from "Dessert" representing Multilevel Inheritance.
- "Pastry" is derived from "Sweets" representing Multilevel Inheritance.
2. In the "main()" function, objects of various classes are created, and "print()" method is called on each object to display information about the respective type of meals. Due to method overriding, the most specific type of "print()" function for each object's class is invoked.
Similar Reads
Inheritance in C++
The capability of a class to derive properties and characteristics from another class is called Inheritance. Inheritance is one of the most important features of Object-Oriented Programming in C++. In this article, we will learn about inheritance in C++, its modes and types along with the informatio
11 min read
What Is Hybrid Inheritance In Python?
Inheritance is a fundamental concept in object-oriented programming (OOP) where a class can inherit attributes and methods from another class. Hybrid inheritance is a combination of more than one type of inheritance. In this article, we will learn about hybrid inheritance in Python. Hybrid Inheritan
3 min read
C++ Multilevel Inheritance
Multilevel Inheritance in C++ is the process of deriving a class from another derived class. When one class inherits another class it is further inherited by another class. It is known as multi-level inheritance. For example, if we take Grandfather as a base class then Father is the derived class th
2 min read
What is a Hybrid Computer?
A hybrid computer is a merger of digital and analog computers. While the analog component frequently functions as a differential equation solver and other mathematically demanding problem solver, the digital component typically acts as the controller and offers logical and numerical operations. What
5 min read
Namespace in C++
Name conflicts in C++ happen when different parts of a program use the same name for variables, functions, or classes, causing confusion for the compiler. To avoid this, C++ introduce namespace. Namespace is a feature that provides a way to group related identifiers such as variables, functions, and
7 min read
is_signed template in C++
The std::is_signed template of C++ STL is used to check whether the type is a signed arithmetic type or not. Syntax: template <class T > struct is_signed; Parameters: This template contains single parameter T (Trait class) to check whether T is a signed arithmetic type or not. Return Value: Th
2 min read
Virtual Function in C++
A virtual function (also known as virtual methods) is a member function that is declared within a base class and is re-defined (overridden) by a derived class. When you refer to a derived class object using a pointer or a reference to the base class, you can call a virtual function for that object a
6 min read
std::set_intersection in C++
The intersection of two sets is formed only by the elements that are present in both sets. The elements copied by the function come always from the first range, in the same order. The elements in the both the ranges shall already be ordered.Examples: Input : 5 10 15 20 25 50 40 30 20 10 Output : The
5 min read
Strict Type Checking in C++
Strict type checking means the function prototype(function signature) must be known for each function that is called and the called function must match the function prototype. It is done at compile time. The "strictly typed language" refers to a very strongly typed language in which there are more s
4 min read
Writing OS Independent Code in C/C++
A program that can interact with the operating system irrespective of the OS on which it runs. In simple words, we can say that a program, which can run on a computer without depending on the operating system. How to write a utility program that lists all contents of the directory irrespective of th
4 min read