C++ Multiple, Multilevel and Hierarchical Inheritance
Inheritance in C++ is a mechanism that allows one class to inherit the properties and behaviors (attributes and methods) of another class. In C++, there are various types of inheritance, including multiple inheritance, multilevel inheritance, and hierarchical inheritance. These inheritance types define how a derived class can inherit from one or more base classes and how the relationship between classes is structured.
Understanding the differences between multiple, multilevel, and hierarchical inheritance is essential for building robust object-oriented applications in C++. Let's explore these types of inheritance in detail.
Multiple inheritance occurs when a derived class inherits from more than one base class. In other words, a class can have multiple parent classes, and it inherits the members (attributes and methods) from all the base classes.
This type of inheritance can be useful in situations where a class needs to combine the functionality of two or more classes.
class DerivedClass : public BaseClass1, public BaseClass2 {
// Derived class members
};
Example of Multiple Inheritance:
#include <iostream>
using namespace std;
// Base class 1
class Animal {
public:
void eat() {
cout << "Animal eats!" << endl;
}
};
// Base class 2
class Vehicle {
public:
void drive() {
cout << "Vehicle drives!" << endl;
}
};
// Derived class inheriting from both Animal and Vehicle
class Car : public Animal, public Vehicle {
public:
void honk() {
cout << "Car honks!" << endl;
}
};
int main() {
Car car;
car.eat(); // Inherited from Animal class
car.drive(); // Inherited from Vehicle class
car.honk(); // Defined in Car class
return 0;
}
Explanation:
Car
class inherits from both Animal
and Vehicle
.eat()
from Animal
and drive()
from Vehicle
.Car
class also has its own method honk()
.We can solve this using virtual inheritance, but we will cover that in a later section.
Multilevel inheritance occurs when a class is derived from another derived class, creating a chain of inheritance. In other words, a class can inherit from a derived class, which in turn inherits from another base class.
class DerivedClass1 : public BaseClass {
// Derived class members
};
class DerivedClass2 : public DerivedClass1 {
// Derived class members
};
Example of Multilevel Inheritance:
#include <iostream>
using namespace std;
// Base class
class Animal {
public:
void eat() {
cout << "Animal eats!" << endl;
}
};
// Derived class from Animal
class Mammal : public Animal {
public:
void giveBirth() {
cout << "Mammal gives birth!" << endl;
}
};
// Further derived class from Mammal
class Dog : public Mammal {
public:
void bark() {
cout << "Dog barks!" << endl;
}
};
int main() {
Dog dog;
dog.eat(); // Inherited from Animal
dog.giveBirth(); // Inherited from Mammal
dog.bark(); // Defined in Dog
return 0;
}
Explanation:
Dog
class is derived from Mammal
, and Mammal
is derived from Animal
.Dog
class can access methods from both the Mammal
and Animal
classes.Hierarchical inheritance occurs when multiple classes are derived from a single base class. In this case, one base class serves as a parent for multiple derived classes.
class DerivedClass1 : public BaseClass {
// Derived class members
};
class DerivedClass2 : public BaseClass {
// Derived class members
};
Example of Hierarchical Inheritance:
#include <iostream>
using namespace std;
// Base class
class Animal {
public:
void eat() {
cout << "Animal eats!" << endl;
}
};
// Derived class 1 from Animal
class Dog : public Animal {
public:
void bark() {
cout << "Dog barks!" << endl;
}
};
// Derived class 2 from Animal
class Cat : public Animal {
public:
void meow() {
cout << "Cat meows!" << endl;
}
};
int main() {
Dog dog;
dog.eat(); // Inherited from Animal class
dog.bark(); // Defined in Dog class
Cat cat;
cat.eat(); // Inherited from Animal class
cat.meow(); // Defined in Cat class
return 0;
}
Explanation:
Dog
and Cat
classes are derived from the Animal
base class.eat()
method from the Animal
class.Dog
class defines the method bark()
, and the Cat
class defines meow()
.Multiple Inheritance: Use this when a class needs to inherit characteristics from multiple base classes. However, it can lead to complications (such as the diamond problem), and you should handle it carefully.
Multilevel Inheritance: Use when you need to create a hierarchy where one class is derived from another, and further derived classes continue the chain. This type of inheritance is often used to represent a clear hierarchical relationship.
Hierarchical Inheritance: Use when multiple derived classes share a common base class. This is appropriate when you have different subclasses with similar behaviors that can inherit common functionality from a single parent class.