Having the following class hierarchy, what will be printed out as a result of the program execution?
#include <iostream>

using namespace std;

class Aggregated{

  public:

      Aggregated(){
        cout<<"Aggregated construct"<<endl;
      }

      ~Aggregated(){
        cout<<"Aggregated destruct"<<endl;
      }
};

class Base{

  protected:

      Aggregated * ptr;

  public:

      Base():ptr(NULL){
       cout<<"Base construct"<<endl;
       f();
      }

      virtual ~Base()=0;

      virtual void f()=0;
};

Base::~Base(){
        if(ptr)
            delete ptr;
        cout<<"Base destruct"<<endl;
}

void Base::f(){
   cout<<"Base f"<<endl;
}

class Derived: public Base{

  public:

      Derived(){
        if(!ptr)
            ptr = new Aggregated;

        cout<<"Derived construct"<<endl;
      }

      ~Derived(){
        cout<<"Derived destruct"<<endl; 
      }

      void f(){}
};

int main(){

    Base* b = new Derived();

    delete b;

    cin.get();

    return 0;
}

Follow CodeGalaxy

Mobile Beta

Get it on Google Play
Send Feedback
Keep exploring
C++ quizzes
Cosmo
Sign Up Now
or Subscribe for future quizzes