What will be printed out as a result of the following code execution?
#include <iostream>
using namespace std;

class D {
        D () { cout << "D"; }
        virtual ~D () { cout << "~D"; }

class A : public D{
        A () { cout << "A";}
        ~A() { cout << "~A";}

class C : public D{
        C () { cout << "C";}
        virtual ~C () { cout << "~C";} 

class B : public A, C {
        B () {cout << "B";}
        ~B () { cout << "~B"; }

int main () {
        A *b = new B ();
        delete b;
        return 0;
It's example of rhomboid inheritance. It means that in order to create object of final class from two that are derivative to some base class we need to create 2 objects of base (constructors are placed in order base obj1, der1 obj.,base obj2, der2.obj) class one for each derivative and then create object of final class, so we have DADCB (note: class A constructor appears earlier then class C one, because it's defined earlier then C), destructors appear in reversed order, so we have DADCB~B~C~D~A~D.

Follow CodeGalaxy

Mobile Beta

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