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
course content

Зміст курсу

C++ OOP

C++ OOP

1. Fundamentals of OOP
2. Constructors and Destructors
3. Encapsulation Overview
4. Inheritance Overview
5. Polymorphism Overview

book
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.

h

Shape

h

Color

copy
1234567
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.

h

ColoredShape

copy
123456789
#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.

h

Vehicle

h

Car

copy
12345
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.

h

ElectricCar

copy
123456
#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:

  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

Which of the following statements about horizontal inheritance is correct?

Which of the following statements about horizontal inheritance is correct?

Виберіть правильну відповідь

Все було зрозуміло?

Як ми можемо покращити це?

Дякуємо за ваш відгук!

Секція 4. Розділ 3
We're sorry to hear that something went wrong. What happened?
some-alt