Virtual Function in OOPS

  1. * /program to illustrate virtual function./ *   
  2. #include < iostream > using namespace std;  
  3. class base  
  4. {  
  5.     publicvoid display()  
  6.     {  
  7.         cout << "\ndisplay base";  
  8.     }  
  9.     virtual void show()  
  10.     {  
  11.         cout << "\nshow derived";  
  12.     }  
  13. };  
  14. class derived: public base  
  15. {  
  16.     publicvoid display()  
  17.     {  
  18.         cout << "\nderived display";  
  19.     }  
  20.     void show()  
  21.     {  
  22.         cout << "\nshow derived";  
  23.     }  
  24. };  
  25. int main()  
  26. {  
  27.     base b;  
  28.     derived d;  
  29.     base * bptr;  
  30.     cout << "\nbptr points to base";  
  31.     bptr = & b;  
  32.     bptr - > display();  
  33.     bptr - > show();  
  34.     cout << "\nbptr points to derived";  
  35.     bptr = & d;  
  36.     bptr - > display();  
  37.     bptr - > show();  
  38.     return 0;  
  39. }