What will be the output of the following C++ code?

#include<iostream> 
using namespace std; 
class Base { 
public: 
    Base()	 
    { cout<<"Constructing Base \n"; } 
    ~Base() 
    { cout<<"Destructing Base \n"; }	 
}; 
class Derived: public Base { 
public: 
    Derived()	 
    { cout<<"Constructing Derived \n"; } 
    ~Derived() 
    { cout<<"Destructing Derived \n"; } 
}; 
 
int main(void) 
{ 
    Derived *d = new Derived(); 
    Base *b = d; 
    delete b; 
    return 0; 
}

 

1 thought on “What will be the output of the following C++ code?”

  1. b
    Explanation: In this case, we have made the destructor of base class virtual which will ensure that any derived class object which is pointed by a base class pointer object on deletion should call both base and derived class destructor.

Leave a Comment