retagged by
1,707 views
1 votes
1 votes

Give the output 

#include<iostream>
using namespace std;
class Base1{
public:
~Base1() {cout<<"Base1's destructor"<<endl;}
};
class Base2
{
public:
~Base2(){cout<<"Base2's destructor"<<endl;}
};
class Derived : public Base1,public Base2{
public:
~Derived(){cout<<"Derived's destructor"<<endl; }
};
int main()
{
Derived d;
return 0;
}
  1. Base$1$'s destructor
    Base$2$'s destructor
    Derived’s Destructor
  2. Derived’s Destructor
    Base$2$'s destructor
    Base$1$'s destructor
  3. Derived’s Destructor
  4. Compiler Dependent
retagged by

2 Answers

2 votes
2 votes

Answer: (B)

Base class constructors are called first and the derived class constructors are called next in single inheritance. Destructor is called in reverse sequence of constructor invocation, i.e., The destructor of the derived class is called first and the destructor of the base is called next.

0 votes
0 votes

Base class constructors are called first and the derived class constructors are called next in single inheritance. Destructor is called in reverse sequence of constructor invocation i.e. The destructor of the derived class is called first and the destructor of the base is called next.

Here its case of multiple inheritance

For multiple inheritance order of constructor call is, the base class’s constructors are called in the order of inheritance and then the derived class’s constructor.

So A is correct.

Ref: https://www.geeksforgeeks.org/order-constructor-destructor-call-c/

 

Answer:

Related questions