in Object Oriented Programming retagged by
954 views
1 vote
1 vote

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
in Object Oriented Programming retagged by
954 views

2 Answers

1 vote
1 vote

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.

1 comment

but why destructor is in reverse order. if we delete the object of the derived class. then how we are going to get the address of the base class to properly destroy the object for garbage collection?
0
0
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/

 

2 Comments

Destructor is called in reverse sequence of constructor invocation

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.

doesn't that make derived class destructor called first than base class in reverse order and therefore answer option (b). please correct me.

0
0
Yes, Answer should be B.
0
0
Answer:

Related questions

Quick search syntax
tags tag:apple
author user:martin
title title:apple
content content:apple
exclude -tag:apple
force match +apple
views views:100
score score:10
answers answers:2
is accepted isaccepted:true
is closed isclosed:true