Hybrid Inheritance in C++
What is inheritance?
A key component of OOP in C++ is inheritance, which lets a newly created class—also referred to as a subdivision or generated class—borrow attributes and techniques from an existing class—also referred to as the superclass or root class.
The Need for Inheritance:
In the case of object-oriented programming, inheritance is necessary for a variety of reasons:
- To make code more easily reusable, extensible, and adaptable.
- Simplifies the maintenance and porting of an object.
- To create practical and affordable outstanding houses.
C++ supports five forms of inheritance:
- A subclass that has only one inheritance form only one superclass.
- A subclass that has several inheritances from different superclasses.
- Multilevel Inheritance: In an ordered sequence of inheritance, a particular class inherited through a different class, that in turn acquired through still an additional class.
- Many subclasses that derive form just one superclass are said to exhibit hierarchy inheritance.
- Combining either of the above categories results in a form of hybrid inheritance.
Hybrid Inheritance in C++:
![Hybrid Inheritance in C++](https://static.tutorialandexample.com/cpp/hybrid-inheritance-in-cpp1.png)
In object-oriented programming (OOP), hybrid inheritance is a sophisticated kind of inheritance. In hybrid inheritance, several kinds of inheritance are merged into just one class hierarchy in hybrid inheritance, allowing for an adaptable and diversified structure for classes. We can combine components from single inheritance, multiple lines of inheritance, multilayer inheritance, and inheritance according to hierarchy in the form of hybrid inheritance throughout the same type.
By rendering it easier for developers to use the attributes and methods that already exist in other courses, the use of hybrid inheritance aims to improve the reuse of code. Compared to other inheritances, hybrid inheritance possesses the following benefits: it makes software parts more reusable, speeds up the creation of code, reduces coding mistakes by preventing duplicate code between courses, and helps object-oriented programming by defining connections among classes with greater precision. Additionally, it offers a systematic approach to group courses with similar characteristics and activities.
Hybrid inheritance does, however, provide several challenges, such as the potential for ambiguity or contradictions among inherited procedures and properties. To guarantee that the combination of inheritance accomplishes its objectives properly while avoiding creating needless difficulties, rigorous design and an adequate grasp of programming languages fundamentals are crucial.
Hybrid Inheritance Examples:
Example 1:
Here we've got a base class named "Person," a class that derives from it called "Employee" that employs individual inheritance, then a third derivative class known as "student" that mixes single inheritance alongside "Employee" to produce a hybrid inheritance.
Example Code: To demonstrate the hybrid inheritance
#include <bits/stdc++.h>
using namespace std;
class Person {
protected:
string name;
public:
Person(const string& name)
: name(name)
{
}
void display() { cout << "\nName: " << name << endl; }
};
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;
}
};
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;
}
};
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();
}
};
int main()
{
StudentIntern SI("lara", 23147, 22189);
SI.displayStudentIntern();
return 0;
}
Output:
![Hybrid Inheritance in C++](https://static.tutorialandexample.com/cpp/hybrid-inheritance-in-cpp2.png)
Description
- The primary function creates a version of the "StudentIntern" class called "SI" with the following justifications:
Call name: Lara
23147 is the employee ID.
ID of the student: 22189
- The "displayStudentIntern" method is invoked on the "SI" object.
- Within the procedure "displayStudentIntern,"
- Techniques within Origin Class "StudentIntern" appears as the header.
- The "Employee" class's "displayEmployee" technique is invoked.
- The "display" function of the "Person" as class (base class) is utilized within the "displayEmployee" object in order to present the employee ID and name.
- The "Student" class's "displayStudent" technique is invoked.
- For displaying the student's name and student ID once more, the "displayStudent" function calls the displaying action of the "Person" type (base class).
![Hybrid Inheritance in C++](https://static.tutorialandexample.com/cpp/hybrid-inheritance-in-cpp3.png)
Example 2:
Expand on the conceptual instance from before by turning them into a practical situation. Class D is for cows, Class B is for mammals, Class C is for herbivores, and Class A is for animals. Cows are a hybrid of mammals and herbivores, and mammals can be descended from the Animal class. The mixture of many Inheritances with Single Inheritance is nicely defined by this connection.
Example Code:
#include <iostream>
using namespace std;
class Animals
{
public:
Animals()
{
cout<< "This is an animal\n";
}
};
class Mammals: public Animals
{
public:
Mammals()
{
cout<< "This is a mammal\n";
}
};
class Herbivores
{
public:
Herbivores()
{
cout<< "This is a herbivore\n";
}
};
class Cow: public Mammals, public Herbivores
{
public:
Cow()
{
cout<< "A cow is a herbivore mammal\n";
}
};
int main() {
Cow c;
return 0;
}
Output:
![Hybrid Inheritance in C++](https://static.tutorialandexample.com/cpp/hybrid-inheritance-in-cpp4.png)
this tutorial and starting to make visually appealing graphics. So, jump in and let your inner creativity shine and make cool graphics.ce more for cache memory loading following bucket sorting.language..