0

I have the following code:

class A { public: A() { cout << "A-C" << endl; } ~A(){ cout << "A-D" << endl; } }; class B { public: B() { cout << "B-C" << endl; } ~B() { cout << "B-D" << endl; } }; class C { protected: A* a; B b; public: C() { a = new A; cout << "C-C" << endl; } ~C() { delete a; cout << "C-D" << endl; } }; class D : public C { protected: A a; B b; public: D(){ cout << "D-C" << endl; } ~D(){ cout << "D-D" << endl; } }; int main() { D* d = new D; B* b = new B; delete b; delete d; system("pause"); return 0; } 

My initial thoughts on the output were:

A-C C-C A-C B-C D-C B-C B-D D-D B-D A-D A-D C-D 

But it's wrong. The output is actually:

B-C A-C C-C A-C B-C D-C B-C B-D D-D B-D A-D A-D C-D B-D 

I don't know why the program calls the B's constructor firstly and its destructor lastly. I think the order of constructor call should like this: C's constructor-> A's constructor -> B's constructor -> D's constructor.

And the order of destructor call is the reverse order of constructor call

Anyone can tell me the reason why the B's constructor is called at the beginning and B's destructor is called at last?

5
  • 1
    Wait, why should the output be B-C first? new D calls the C constructor which calls the A constructor. Commented Apr 22, 2017 at 16:45
  • 1
    @GillBates nuh-huh. C has a B data member that's initialized before the constructor's body. Commented Apr 22, 2017 at 16:47
  • @Quentin I thought that B data member in C was a pointer, my bad. Commented Apr 22, 2017 at 16:47
  • @aschepler Yeah, op meant that he's getting the 2nd part as actual output, just wasn't worded correctly. Commented Apr 22, 2017 at 16:56
  • it seems that my lecturer is wrong lol, he said that when the class inherit from another class, only the base class constructor will be called before the derived class constructor...He hasn't mentioned about the initialization of base class data member. Commented Apr 22, 2017 at 17:22

1 Answer 1

1
B-C A-C C-C A-C B-C D-C 

All of these are caused by D* d = new D;.

The D constructor calls the C constructor because C is a base of D.

Then, data members a and b of class C are initialized, a is a pointer so no constructor calls for that data member yet. b is an object of type B so the B parameterless constructor gets called, giving you the first B-C.


Then in the C constructor you say new A which calls the constructor of A which gives you A-C followed by C-C.

Then, data members a and b of class D are initialized, both are objects so both constructors are called, giving you A-C and B-C.

And finally, the D constructor is called, ending it with D-C.

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you very much, do you mean that when the class inherit from another class, let say D inherit from C, it will initialize the data member of the base class first and the constructor will be called after the initialization of the data member?
@KelvinNg Yes, exactly.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.