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

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

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

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

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

int main () {
        A *b = new B ();
        delete b;
        return 0;
}
Explanation
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.

Слідкуй за CodeGalaxy

Мобільний додаток Beta

Get it on Google Play
Зворотній Зв’язок
Cosmo
Зареєструйся Зараз
або Підпишись на майбутні тести