Which answer is more correct? (C++) | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 5

Which answer is more correct? (C++)

I got the following C++ question: //----- In case of inheritance where both base and derived class are having constructors, when an object of derived class is created... 1. constructor of derived class will be invoked first. 2. constructor of base class will be invoked first. 3. constructor of derived class will be executed first followed by base class. 4. constructor of base class will be executed first followed by derived class. //----- I chose the 2nd answer, as the word "first" implies that there will be another invocatios and the execution of derived class constructor may not immediately follow after execution of base class constructor. My answer was not accepted as correct. The correct answer is #4. Which answer is more correct (#2 or #4)?

11th Feb 2020, 12:53 PM
andriy kan
andriy kan - avatar
5 Answers
+ 5
I did not write that the derived class constructor will not be called. I wrote that a derived class constructor call may not immediately(!!!) follow a base class constructor call (what the #4 answer says). derived class constructor call can follow the call of one of the derived class member constructors.
11th Feb 2020, 1:28 PM
andriy kan
andriy kan - avatar
+ 5
For example, I can write the following code: #include <iostream> using namespace std; class Other{ public: Other(){ cout << "Other "; } }; class Base{ public: Base(){ cout << "Base "; } }; class Derived: public Base{ public: Derived(){ cout <<"Derived "; } private: Base b; Other o; }; int main() { Derived d; cout << endl; } The output will be: Base Base Other Derived Derived class constructor call follows Other class constructor call, but not Base class constructor call (what answer #4 says)
11th Feb 2020, 1:38 PM
andriy kan
andriy kan - avatar
+ 5
Thank you for your answer, but I see it differently: Specification " in case, both Base and derived classes are having the constructors.." doesn’t matter, since both classes have constructors in any case, even if you do not explicitly specify them (unless you explicitly delete them, but in this case you can not create an object). Answer #2 is not missing the derived class constructor invocation behavior: the word “first” in answer #2 implies that derived class constructor will be invoked second (or third, or fourth...), otherwise answer #2 would be like this: only(!) base class costructor will be invoked. Answer #4: English is not my native language, and for me the word "followed" means called immediately after. But as I wrote, derived class constructor may be called not immediately after calling base class constructor (Constructors of derived class members can be called before it). Or my understanding of the answers #2, #4 is not correct?
11th Feb 2020, 5:45 PM
andriy kan
andriy kan - avatar
+ 1
Obviously answer #4 is correct. The whole concept of inheritance will collapse if the child class constructor which is responsible for object creation does not call the parent class constructor. The call is generally a implicit one which is taken care of by the compiler.
11th Feb 2020, 1:15 PM
Avinesh
Avinesh - avatar
+ 1
#4 option will fit accurately, because there specified as " in case, both Base and derived classes are having the constructors.." And #2 missing the derived class invokation behavior...
11th Feb 2020, 3:34 PM
Jayakrishna 🇮🇳