// test_vir.cc // show how a virtual function in a base class gets called // demonstrate polymorphism // compare this with testpvir.cc using a pure function to make an abstract class class Base // terminology: a base class is any class that { // does not inherit another class public: void f(void); virtual void v(void); }; class D1 : public Base // note both f and v inherited { // then defined again public: void f(void); virtual void v(void); }; class D2 : public Base // note both f and v inherited { // only v defined again public: virtual void v(void); }; class D3 : public Base // note both f and v inherited { // only f defined again public: void f(void); }; class DD1 : public D1 // a class derived from the a derived class { // now have potentially three f's and three v's public: void f(void); virtual void v(void); }; int main(void) { Base b, *bp; // declare an object and a pointer for each class D1 d1, *d1p; D2 d2, *d2p; D3 d3, *d3p; DD1 dd1, *dd1p; b.f(); // calls Base::f() b.v(); // Base::v() bp=&b; bp->f(); // Base::f() bp->v(); // Base::v() no difference with virtual yet d1.f(); // D1::f() d1.v(); // D1::v() d1p=&d1; d1p->f(); // D1::f() d1p->v(); // D1::v() no difference with virtual yet bp=&d1; // now, make a pointer to the base type, point to an // object of a derived type of the base type bp->f(); // Base::f() choose function belonging to pointer type bp->v(); // D1::v() now difference with virtual! // run time polymorphism d2.f(); // Base::f() d2.v(); // D2::v() d2p=&d2; d2p->f(); // Base::f() D2 has no f(), get from base type d2p->v(); // D2::v() no difference with virtual yet bp=&d2; // now, make a pointer to the base type, point to an // object of a derived type of the base type bp->f(); // Base::f() choose function belonging to pointer type bp->v(); // D2::v() now difference with virtual! // run time polymorphism d3.f(); // D3::f() d3.v(); // Base::v() D3 has no v(), get from base type d3p=&d3; d3p->f(); // D3::f() d3p->v(); // Base::v() D3 has no v(), get from base type bp=&d3; // now, make a pointer to the base type, point to an // object of a derived type of the base type bp->f(); // Base::f() choose function belonging to pointer type bp->v(); // Base::v() no local function, choose from pointer type dd1.f(); // DD1::f() dd1.v(); // DD1::v() dd1p=&dd1; dd1p->f(); // DD1::f() dd1p->v(); // DD1::v() no difference with virtual yet bp=&dd1; // now, make a pointer to the base type, point to an // object of a derived derived type of the base type bp->f(); // Base::f() choose function belonging to pointer type bp->v(); // DD1::v() now difference with virtual! // this is run time polymorphism return 0; } #include using namespace std; void Base::f(void) // implementation of each function { // each function just outputs its name cout << "Base::f()" << endl; } void Base::v(void) { cout << "Base::v()" << endl; } void D1::f(void) { cout << "D1::f()" << endl; } void D1::v(void) { cout << "D1::v()" << endl; } void D2::v(void) { cout << "D2::v()" << endl; } void D3::f(void) { cout << "D3::f()" << endl; } void DD1::f(void) { cout << "DD1::f()" << endl; } void DD1::v(void) { cout << "DD1::v()" << endl; } /* result of running above file: Base::f() Base::v() Base::f() Base::v() D1::f() D1::v() D1::f() D1::v() Base::f() D1::v() Base::f() D2::v() Base::f() D2::v() Base::f() D2::v() D3::f() Base::v() D3::f() Base::v() Base::f() Base::v() DD1::f() DD1::v() DD1::f() DD1::v() Base::f() DD1::v() */