Basic program for Multiple Inheritance with constructors

In this, we are going to see a basic program for Multiple Inheritance with constructors in C++ Programming Language. 


The Code given below can be used in TURBO C++ Compilers: -


#include<iostream.h>
#include<conio.h>

class parent1
{
    public:
    parent1()
    {
        cout << "Constructor of class parent1 (Base Class 1)" << endl;
    }
};

class parent2
{
    public:
    parent2()
    {
        cout << "Constructor of class parent2 (Base Class 2)" << endl;
    }
};

class child : public parent1, public parent2
{
    public:
    child()
    {
        cout << "Constructor of class child (Derived Class)" << endl;
    }
};

void main()
{
    clrscr();
    child obj1; //all constructors are called
    getch();
}

//...........Coded by Ananya Vidyadharan
 

The Code given below can be used in gcc/g++ Compilers: -


#include <iostream>
using namespace std;

class parent1
{
    public:
    parent1()
    {
        cout << "Constructor of class parent1 (Base Class 1)" << endl;
    }
};

class parent2
{
    public:
    parent2()
    {
        cout << "Constructor of class parent2 (Base Class 2)" << endl;
    }
};

class child : public parent1, public parent2
{
    public:
    child()
    {
        cout << "Constructor of class child (Derived Class)" << endl;
    }
};

int main()
{
    child obj1; //all constructors are called
    return 0;
}

//...........Coded by Ananya Vidyadharan
    

#ENJOY CODING

Post a Comment

FOR ANY DOUBTS AND ERRORS FEEL FREE TO ASK. YOUR DOUBTS WILL BE ADDRESSED ASAP

Previous Post Next Post