All mock tests are FREE till 31 December — register to save your progress

What is the output of the code given below?
#include<iostream>
using namespace std;
class Base {
public:
  Base() { cout << "Base Const"; }
  virtual ~Base() { cout << "Base dest"; }
};
class Derived : public Base {
public:
  Derived() { cout << "Derived Const"; }
  ~Derived() { cout << "Derived dest"; }
};
int main() {
  Base *b = new Derived();
  delete b;
  return 0;
}

Pick an option to check yourself, or open the answer below.

Show answer & explanation

Correct answer: A. Base Const Derived Const Derived dest Base dest

Explanation

Constructors run base first, then derived. Because ~Base is virtual, delete through a Base* calls ~Derived first and then ~Base.

More Programming Languages and Computer Graphics questions