Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Mulptiple Inheritance | Inheritance Overview
C++ OOP

Mulptiple InheritanceMulptiple 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 inheritance
vertical inheritance

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.

h

Shape.h

h

Color.h

Now, let's create a subclass called ColoredShape that inherits properties and methods from both Shape and Color classes.

h

ColoredShape.h

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.

h

Vehicle.h

h

Car.h

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.

h

ElectricCar.h

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:

  1. 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.
  2. 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.
  3. 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

1. Which of the following statements about horizontal inheritance is correct?
2. Why is multiple inheritance beneficial?

Which of the following statements about horizontal inheritance is correct?

Select the correct answer

question-icon

Why is multiple inheritance beneficial?

Select a few correct answers

Everything was clear?

Section 4. Chapter 3

Mulptiple InheritanceMulptiple 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 inheritance
vertical inheritance

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.

h

Shape.h

h

Color.h

Now, let's create a subclass called ColoredShape that inherits properties and methods from both Shape and Color classes.

h

ColoredShape.h

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.

h

Vehicle.h

h

Car.h

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.

h

ElectricCar.h

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:

  1. 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.
  2. 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.
  3. 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

1. Which of the following statements about horizontal inheritance is correct?
2. Why is multiple inheritance beneficial?

Which of the following statements about horizontal inheritance is correct?

Select the correct answer

question-icon

Why is multiple inheritance beneficial?

Select a few correct answers

Everything was clear?

Section 4. Chapter 3
some-alt