C++ Language



Admission Enquiry Form

  

Constructor and Destructor in Inheritance




Program of Default Constructor in Multiple Inheritance in C++

//Multiple Inheritance C++
#include<iostream>
using namespace std;
class Base1
{
public:
Base1()//default constructor
{
cout<<endl<<"I am default constructor of Base1";
}
void fun1()
{
cout<<endl<<"I am fun1 of Base1 class";
}
void fun2()
{
cout<<endl<<"I am fun2 of Base1 class";
}
};//end of Base class
class Base2
{
public:
Base2()//default constructor
{
cout<<endl<<"I am default constructor of Base2";
}
void fun3()
{
cout<<endl<<"I am fun3 of Base2";
}
void fun4()
{
cout<<endl<<"I am fun4 of Base2";
}
};//end of Base2
class Derived : public Base1,public Base2 //multiple inheritance
{
public:
Derived()//default constructor
{
cout<<endl<<"default constructor of Derived";
}
void fun5()
{
cout<<endl<<"I am fun5 of derived class";
}
void fun6()
{
cout<<endl<<"I am fun6 of derived class";
}

};//end of Derived class

int main()
{
Derived d;//object of Derived class

}//end of main


Output:

I am default constructor of Base1
I am default constructor of Base2
default constructor of Derived

We observe in above example the constructor of Derived class is invoked at the end and the default constructor of the Base1 and Base2 class is invoked by default.