#include using namespace std; struct Base { void f() { cout << "B::f "; } // non-virtual void g() { cout << "B::g "; } // non-virtual virtual void h() { cout << "B::h "; } // virtual }; struct Derived : public Base { void g() { cout << "D::g "; } // replace, non-virtual void h() { cout << "D::h "; } // replace, virtual void e() { cout << "D::e "; } // extension }; int rtn( Base &bp ) { // inheritance polymorphism bp.f(); // pointer type (non-virtual) bp.g(); // pointer type (non-virtual) ((Derived &)bp).g(); // pointer type (coercion!) bp.Base::h(); // explicit selection bp.h(); // object type (virtual) } int main() { Base b; Derived d; Base &b1 = b; Base &b2 = *new Base(); // same as b1 Base &b3 = d; Base &b4 = *new Derived(); // same as b3 rtn( b1 ); cout << endl; rtn( b2 ); cout << endl; rtn( b3 ); cout << endl; rtn( b4 ); cout << endl; d.e(); cout << endl; }