VIRTUAL FUNCTION
Program:
#include “iostream.h”
#include “conio.h”
class Base
{
public:
void display()
{
cout << "display base"; } virtual void show() { cout << "\n show base"; } }; class Derived:public Base { public: void display() { cout << "\n display derived"; } void show() { cout << "\n show derived"; } }; void main() { clrscr(); Base B; Derived D; Base *bptr; cout << "\n bptr points to base \n"; bptr=&B; bptr->display();
bptr->show();
cout << "\n bptr points to derived \n"; bptr=&D; bptr->display();
bptr->show();
getch();
}
Output:
bptr points to base
display base
show base
bptr points to derived
display base
show derived