Multiple Inheritance in c++ example

What is Multiple Inheritance?

Multiple inheritance is a feature of some object-oriented computer programming languages in which an object or class can inherit characteristics and features from more than one parent object or parent class.

Deriving directly from more than one class is usually called multiple inheritance. Since it’s widely believed that this concept complicates the design and debuggers can have a hard time with it, multiple inheritance can be a controversial topic. However, multiple inheritance in C++ is very important and C++ programmers think of it as a very good structuring tool.

multiple inheritance in C++

Multiple Inheritance in C++ example:

#include<iostream>
using namespace std;

class A
{
public:
  A()  { cout << "A's constructor called" << endl; }
};

class B
{
public:
  B()  { cout << "B's constructor called" << endl; }
};

class C: public B, public A  // Note the order
{
public:
  C()  { cout << "C's constructor called" << endl; }
};

int main()
{
    C c;
    return 0;
}

OUTPUT

multiple inheritance in C++

Note: We see that when an object of class C is declared in main( ), firstly class B’s constructor is called then A’s and then finally C’s constructor is called as C inherits class B first then Class A.

Leave a Comment