Inheritance. Why this output?

First of all you have no virtual functions and thus the functions from the corresponding class will be called. Thus you simply call a::f(3) and B::f(3). Second even if f was virtual you pass the arguments of g by value which means a upcast happens and thus in g you simply have an instance of A and instance of B, no polymorphism involved. Thus the output is:

3
4

Not very surprising.

Leave a Comment