Multiple Inheritance in C++

Multiple Inheritance in C++

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++?

要查看或添加评论,请登录

Lokesh Gupta的更多文章

社区洞察

其他会员也浏览了