// A1c.cpp -- variation c on Question #1 #include class B { public: virtual void mf(int n); protected: int state; }; void B::mf(int n) { cout << " B::mf(" << n << ") invoked\n"; state = n; } class D : public B { public: void mf(int n); }; void D::mf(int n) { cout << " D::mf(" << n << ") invoked\n"; state = -n; } void main() { D d1; // compiler gen'd void ctor for D called // which calls compiler gen'd void ctor for B B *bptr = new D(d1); // memory allocated to hold a D object // on heap, initialized with Compiler // generated copy ctor for D which calls // compiler generated base void ctor bptr->mf(3); // D:mf(3) is invoked polymorphically; // since we added virtual qualifier to B::mf }