From the course: Object-Oriented Programming with C++

Unlock this course with a free trial

Join today to access over 24,400 courses taught by industry experts.

Multiple inheritance

Multiple inheritance

- [Instructor] So far, our classes have inherited from a single base class. However, C++ allows a class to inherit from multiple base classes. Let me show you why this might be useful, and also, why it needs to be used carefully. Let's extend our review system. Imagine we want our product review to also have social media features, likes, shares, comments. We should create a sociable class like the following. The class exposes public methods that allow users to like, share, comment, and display social media stats. Now, we can make product review inherit from both review and sociable. The syntax for multiple inheritance is simple. Just add a comma, followed by the inheritance access specifier, and the new class from which we want to inherit. But first, we need to include its header. Next, we'll call the displaySocialStats method of the sociable-based class from displayDetails. In the main function, we can use the new features. Let's add some social interactions. We'll call the like…

Contents