Hybrid inheritance is a combination of more than one type of inheritance, such as single, multiple, multilevel, or hierarchical. It allows a class to inherit properties from different inheritance models, providing flexibility in class design.
Example Structure
A class can be derived from multiple base classes, where each base class itself may have been derived through different inheritance models.
#include <iostream>
using namespace std;
class Animal {
public:
void eat() {
cout << "Eating..." << endl;
}
};
class Mammal : public Animal {
public:
void breathe() {
cout << "Breathing..." << endl;
}
};
class Bird : public Animal {
public:
void fly() {
cout << "Flying..." << endl;
}
};
class Bat : public Mammal, public Bird {
public:
void sound() {
cout << "Screeching..." << endl;
}
};
int main() {
Bat myBat;
myBat.eat(); // Inherited from Animal
myBat.breathe(); // Inherited from Mammal
myBat.fly(); // Inherited from Bird
myBat.sound(); // Specific to Bat
return 0;
}