#include class myOldClass { public: int i1; virtual void func1(int i) { cout << "Calling myOldClass.func1(int)...\n"; i1 = i1 + i; } }; class myNewClass : public myOldClass { public: int i2; virtual void func1(int i) { cout << "Calling myNewClass.func1(int)...\n"; i2 = i2 + 1000*i; } }; int main(int argc, char *argv[]) { myOldClass A; myOldClass *ptr1; myNewClass B; myNewClass *ptr2; A.i1 = 0; B.i1 = B.i2 = 0; cout << "Invoking A.funct1(4)" << endl; A.func1(4); cout << endl; cout << "Invoking B.funct1(4)" << endl; B.func1(4); cout << endl << endl; // This is illegal: // ptr2 = &A; // ************************************************************************* // This is the interesting part: which function would ptr1->func1() pick ? // // A is a myOldClass variable that has it's own func1() // B is a myNewClass variable that has it's own func1() // ptr1 can point to a myOldClass or a myNewClass variable // Which function will ptr1->func1() invoke ??? // // ************************************************************************* ptr1 = &A; cout << "ptr1 = &A; Invoking ptr1->func1(4)" << endl; ptr1->func1(4); cout << endl; ptr1 = &B; cout << "ptr1 = &B; Invoking ptr1->func1(4)" << endl; ptr1->func1(4); }