Predict the output of following C++ programs.
Constructor called Constructor called
The first line of output is printed by statement “String s1(“Geeks”);” and the second line is printed by statement “s1 = name;”. The reason for the second call is, a single parameter constructor also works as a conversion operator (See this and this for details).
A base class pointer can point to objects of children classes. A base class pointer can also point to objects of grandchildren classes. Therefore, the line “A *a = new C;” is valid. The line “a->fun();” prints “C” because the object pointed is of class C and fun() is declared virtual in both A and B (See this for details). The second line of output is printed by statement “b->fun();”.
Please write comments if you find any of the answers/explanations incorrect, or want to share more information about the topics discussed above.