Question discussion

What is the output of the following C++ program?#include using namespace std; class B { public: virtual ~B() { cout

1171Medium DSSSB TGT Computer Science 0 replies 1 views
0

What is the output of the following C++ program?

#include <iostream>
using namespace std;
class B { public: virtual ~B() { cout << "B"; } };
class D : public B { public: ~D() { cout << "D"; } };
int main() { B *p = new D(); delete p; }
  • ADB
  • BD
  • CBD
  • DB
Correct answer: A

Explanation

The destructor is virtual, so deleting through the base pointer runs ~D() first and then ~B(), printing DB.

Discussion 0

Nobody has replied yet. Start the discussion!