Multiple Inheritance in C++
Lokesh Gupta
AWS Certified Solutions Architect – Professional | Full Stack Developer | Technical Lead | Masters in Information Systems | Salesforce Trailblazer | Google Scholar
Unlike many other object-oriented programming languages, C++ allows multiple inheritance.
Multiple inheritance allows a child class to inherit from more than one parent class.
At the outset, it seems like a very useful feature. But a user needs to be mindful of a few gotchas while implementing this feature.
In the examples below, we will cover a few scenarios that one needs to be mindful about.
We’ll start with a simple example to explain this concept in C++.
The output of this code is as follows:
I'm breathing as a snake.
I’m crawling as a snake.
In the example above, we have a base class named as LivingThing. The Animal and Reptile classes inherit from it. Only the Animal class overrides the method breathe(). The Snake class inherits from the Animal and Reptile classes. It overrides their methods. In the example above, there is no problem. Our code works well.
What is C++?