C++ challenge from quizes | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 3

C++ challenge from quizes

Whats the output and how did you get it. class A{ public: A(){cout<<1;} ~A(){cout<<2;} }; class B:public A{ public: B(){cout<<3;} }; int main(){ B obj1; A obj2; }

5th Aug 2017, 1:46 PM
Marko Majstorovic
Marko Majstorovic - avatar
2 Answers
+ 10
Remember that base class constructors get called before the derived class constructors, and all destructors are called when the program ends. The first object is from class B. // prints 1 from class A constructor and 3 from class B constructor The second object is from class A. // prints 1 from class A constructor Program ends // prints 2 twice when destructor for class A and class B object is called. 13122
5th Aug 2017, 1:58 PM
Hatsy Rei
Hatsy Rei - avatar
+ 3
Thank you so much i have more questions :D so stick around my profile.
5th Aug 2017, 2:19 PM
Marko Majstorovic
Marko Majstorovic - avatar