#include “stdio.h”
class A {
public:
A() {
this->myPrint();
}
virtual void myPrint() {
printf(“A:%s()\n”,__FUNCTION__);
}
};
class B: public A {
public:
B() {
}
void myPrint() {
printf(“B:%s()\n”,__FUNCTION__);
}
};
main()
{
B b1; // prints A:myPrint
b1.myPrint(); // prints B:myPrint
}
The reason is when A:A() executes, the object is still of type A. It only becomes the obect of type B, when code in B:B() is entered.