Course Content
C++ OOP
C++ OOP
Mulptiple Inheritance
Multiple inheritance allows a derived class to inherit from multiple base classes. This means that a single derived class can have access to the members (data and functions) of multiple base classes, effectively combining their attributes and behaviors. To understand this concept better, let's start with a simple example.
Horizontal Multiple Inheritance
In this type of multiple a class inherits properties and methods from multiple superclasses at the same level in the inheritance hierarchy. Consider classes: Shape and Color, each with distinct properties and methods. The Shape defines the shape of an object, and the Color defines its color.
Shape
Color
class Shape { public: void setShape(const std::string& value) { shape = value; } std::string getShape() { return shape; } private: std::string shape; };
Now, let's create a subclass called ColoredShape that inherits properties and methods from both Shape and Color classes.
ColoredShape
#include "Shape.h" #include "Color.h" class ColoredShape : public Shape, public Color { public: void describe() { std::cout << "This object is a " << getShape() << " shape and is " << setColor() << '\n'; } };
Vertical Inheritance
In depth inheritance, a class inherits properties and methods from its direct parent and its ancestors, forming an inheritance chain. For instance, consider the class Vehicle, which can serve as a basis for inheriting for car, truck, and others.In our example, we will utilize the Car to illustrate the concept.
Vehicle
Car
class Vehicle { public: void start() { std::cout << "Vehicle started"; } void stop() { std::cout << "Vehicle stopped"; } };
Now, to achieve vertical inheritance you have to set up a hierarchy where one class inherits from another, and then a subsequent class inherits from the first one, and so on. We can create an ElectricCar that inherits all properties and functionalities from the Car, which itself inherits from the Vehicle, establishing a complex multiple inheritance structure.
ElectricCar
#include "Car.h" class ElectricCar : public Car { public: void charge() { std::cout << "Electric car is charging"; } };
Why Multiple Inheritance is Needed
Multiple inheritance provides flexibility and code reuse in situations where a class needs to exhibit behaviors or characteristics of more than one parent class. Here are some scenarios where multiple inheritance is beneficial:
- Modeling Real-World Relationships: objects often have multiple aspects or roles. For example, a FlyingBird class might inherit from both Flying and Bird, capturing both the characteristics of a bird and the ability to fly. And you still can use class Flying for other objects that might fly, like a plane.
- Code Reusability: Multiple inheritance allows the reuse of existing classes without code duplication. By inheriting from multiple base classes, a derived class can inherit functionalities from each base class independently.
- Interface Segregation: It enables the creation of interfaces tailored to specific needs. Instead of creating a monolithic interface, multiple inheritance allows the creation of smaller, more focused interfaces that can be combined as needed.
Note
Split a complex object into simpler ones and utilize multiple inheritance to create flexible and maintainable software
Thanks for your feedback!