Examples of Hybrid Inheritance in C++
What is Inheritance in C++?
Using C++, inheritance permits developers to build new classes from the ones that already exist. That establishes a relationship in which the derived class inherits the base class's properties and functionality. It encourages code reuse, extension, and specialisations. Derived classes can add new members or override those that were inherited.
- Inheritance generates new classes from existing ones.
- Derived classes inherit base classes' characteristics and behaviour.
- Code inheritance encourages code reuse and extensibility.
- Derived classes have the ability to add new members or override inherited ones.
- Polymorphism and dynamic binding are made possible by inheritance.
- The visibility and accessibility of inherited members are determined by access specifiers.
- Constructors and destructors are not inherited but can be explicitly invoked.
C++ Hybrid Inheritance
The term hybrid inheritance is a blend of multiple inheritance and hierarchical inheritance in C++. It happens when a class inherits from several base classes, some of which may be linked by a hierarchical relationship.
A derived class can inherit properties and behaviours from numerous base classes under hybrid inheritance, providing for a mix of features and capabilities. This can be beneficial for modelling complex relationships or reusing code from multiple sources.
- Multitude Inheritance: A derived class inherits from many base classes, incorporating properties and behaviours from other classes.
- Hierarchical Inheritance: A hierarchy is formed when multiple derived classes inherit from a single base class. Each derived class can contain extra or overridden members.
You can establish more complex class relationships and get greater code flexibility by combining multiple inheritance and hierarchical inheritance. To eliminate ambiguity and maintain code readability, these interactions must be carefully planned and managed.
Syntax for Hybrid Inheritance
The syntax for hybrid inheritance in C++, which mixes multiple inheritance and hierarchical inheritance, encompasses declaring numerous base classes, some of which are connected via a hierarchical relationship. The method of expression translates as listed below:
class BaseClass1 {
// Members of Base Class 1
};
class BaseClass2 {
// Members of Base Class 2
};
class DerivedClass : public BaseClass1, public BaseClass2 {
// Members of the derived class
};
The above DerivedClass in the preceding example is derived from both BaseClass1 and BaseClass2. It inherits attributes and behaviours from both base classes, enabling a mix of features and functionality. The public keyword denotes the base classes' access specifier, which determines the visibility of its members in the derived class.
Example 1
Following is an instance of hybrid inheritance in C++, accompanied with an explanation:
#include <iostream>
class BaseClass1 {
public:
void display1() {
std::cout << "Base Class 1\n";
}
};
class BaseClass2 {
public:
void display2() {
std::cout << "Base Class 2\n";
}
};
class DerivedClass : public BaseClass1, public BaseClass2 {
public:
void displayDerived() {
std::cout << "Derived Class\n";
}
};
int main() {
DerivedClass obj;
obj.display1();
obj.display2();
obj.displayDerived();
return 0;
}
Output
![Examples of Hybrid Inheritance in C++](https://static.tutorialandexample.com/cpp/examples-of-hybrid-inheritance-in-cpp1.png)
Explanation
We're dealing with three classes in our hybrid inheritance example: 'BaseClass1', 'BaseClass2', and 'DerivedClass'.
- The fundamental classes 'BaseClass1' and 'BaseClass2' have member functions.
- Using multiple inheritance, 'DerivedClass' derives from both 'BaseClass1' and 'BaseClass2'.
- The class that has been derived has its own member function as well.
Example 2
#include <iostream>
class Animal {
public:
void eat() {
std::cout << "Animal is eating." << std::endl;
}
};
class FlyingAnimal {
public:
void fly() {
std::cout << "FlyingAnimal is flying." << std::endl;
}
};
class Bird : public Animal, public FlyingAnimal {
public:
void chirp() {
std::cout << "Bird is chirping." << std::endl;
}
};
int main() {
Bird sparrow;
sparrow.eat();
sparrow.fly();
sparrow.chirp();
return 0;
}
Output
![Examples of Hybrid Inheritance in C++](https://static.tutorialandexample.com/cpp/examples-of-hybrid-inheritance-in-cpp2.png)
Explanation
We have three classes in this hybrid inheritance example: Animal, FlyingAnimal, and Bird.
- Animal is a general animal with an eat() member function.
- FlyingAnimal represents flying animals and has a fly() member function.
- Bird is a multiple inheritance descendant of both Animal and FlyingAnimal, and it has its own chirp() member function.
We may access the inherited member functions from both Animal and FlyingAnimal, as well as the Bird class's own member function, by constructing an object of the Bird class. This lets us to use Animal's eat(), FlyingAnimal's fly(), and Bird's chirp() functions.
Conclusion
To summarise, hybrid inheritance in C++ allows a derived class to inherit from multiple base classes while integrating their attributes and behaviours. It enables flexibility, code reuse, and specialisation. However, it necessitates meticulous management of potential complications such as name conflicts. Overall, it allows for the building of complicated class hierarchies while also improving code management and extensibility.