C++ examples for Class:Virtual Function
Virtual functions accessed from pointer
#include <iostream> using namespace std; class Base/*from ww w. ja v a 2 s .com*/ { public: virtual void show() //virtual function { cout << "Base\n"; } }; class Derv1 : public Base //derived class 1 { public: void show() { cout << "Derv1\n"; } }; class Derv2 : public Base //derived class 2 { public: void show() { cout << "Derv2\n"; } }; int main() { Derv1 dv1; //object of derived class 1 Derv2 dv2; //object of derived class 2 Base* ptr; //pointer to base class ptr = &dv1; //put address of dv1 in pointer ptr->show(); //execute show() ptr = &dv2; //put address of dv2 in pointer ptr->show(); //execute show() return 0; }