Answer to Question #247386 in C++ for kanha

Question #247386

Write a program to demonstrate the order of call of constructors and destructors in case of multiple inheritance where one or more base classes are virtual.


1
Expert's answer
2021-10-06T06:17:42-0400
#include <iostream>
using namespace std;


class Base_A
{
	
	public:
    	Base_A()
        	{
        		cout <<"First base class constructor"<<"\n";
        	}
        ~Base_A()
        	{
        		cout <<"First base class destructor"<<"\n";
        	}
};


class Base_B
{
	public:
    	Base_B()
    	{
    		cout << "Second base class constructor\n";
    	}
    	~Base_B()
    	{
    		cout << "Second base class destructor\n";
    	}
};


class Derived : public Base_A, public Base_B
{
	public:
    	Derived()
    	{
    		cout << "Derived class constructor" << "\n";
    	}
    	~Derived()
    	{
    		cout << "Derived class destructor" << "\n";
    	}
    	
};


int main() {
    cout<<"Order of call of constructors:\n\n";
	Derived obj;
	cout<<"\nOrder of call of destructors:\n\n";
	return 0;
}


Output





Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS