Basic Example using Multiple Inheritance in C++


This program demonstrates how to create multiple base classes using multiple inheritance in C++. Here, we have three classes: A, B, and C. A and B are base classes and C is a derived class that inherits from both A and B. In the main() function, an object of C class is created using the default constructor. When the object is created, first the A class constructor is called followed by the B class constructor and then the C class constructor.

Source Code

#include<iostream>
using namespace std;
class A
{
   public:
    A()
    {
       cout<<"Constructor of A class"<<endl;
    }
};
class B
{
   public:
    B()
    {
       cout<<"Constructor of B class"<<endl;
    }
};
class C: public A, public B
{
   public:
    C()
    {
       cout<<"Constructor of C class"<<endl;
    }
};
int main()
{
  C obj;
  return 0;
}
To download raw file Click Here

Output

Constructor of A class
Constructor of B class
Constructor of C class

Program List


Flow Control

IF Statement Examples


Switch Case


Goto Statement


Break and Continue


While Loop


Do While Loop


For Loop


Friend Function in C++


String Examples


Array Examples


Structure Examples


Structure & Pointer Examples


Structure & Functions Examples


Enumeration Examples


Template Examples


Functions


Inheritance Examples

Hierarchical Inheritance


Hybrid Inheritance


Multilevel Inheritance


Multiple Inheritance


Single Level Inheritance


Class and Objects

Constructor Example


Destructor Example


Operator Overloading Example


Operator and Function Example


List of Programs


Pointer Examples


Memory Management Examples


Pointers and Arrays


Virtual Function Examples