I wonder why static objects call parent method and dynamic objects child method in this example.
#include <string>
#include <iostream>
using namespace std;
class Father {
public:
virtual string Info() {
return "I am father";
}
};
class Son : public Father {
public:
string Info() {
return "I am son";
}
};
int main() {
Father f = Son();
cout << f.Info(); // I am father
Father* pf = new Son();
cout << pf->Info(); // I am son
return 0;
}
Because,
Father f = Son()create object of type Father and copy attributes from object Son (at this case no attributes to copy), soFather * pf = new Son()receives pointer to object Son, it actually may be any Father-derived object.Finally, in your example you have 3 objects.
Father f,unnamed object Son()which copy toFather f, andFather * pfthat points to object of type Son.