C++ | Inheritance | Question 2

Output?

filter_none

edit
close

play_arrow

link
brightness_4
code

#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;
}

chevron_right


(A)

Base1's destructor
Base2's destructor
Derived's destructor

(B)

Derived's destructor
Base2's destructor
Base1's destructor

(C)

Derived's destructor

(D) Compiler Dependent


Answer: (B)

Explanation: Destructors are always called in reverse order of constructors.

Quiz of this Question

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up
Article Tags :
Practice Tags :


1


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.