What will print the following code?
#include <iostream>
using namespace std;

struct A {
    virtual ~A() = 0;
A::~A() { cout << "~A"; }

struct B : A {
    ~B() { cout << "~B"; }

int main() {
  A *a = new B;
  delete a;
In C++ destructors are called recursively for each ancestor class from which this class extends, beginning from the destructor of this class. Since destructor of class A - virtual, then during a deletion, it will call a destructor B, so that C ++ will call a destructor A. Although destructor ~A() is declared as pure virtual, it must be defined.
See the details on http://stackoverflow.com/questions/999340/question-about-pure-virtual-destructor.

Follow CodeGalaxy

Mobile Beta

Get it on Google Play
Send Feedback
Sign Up Now
or Subscribe for future quizzes