Program to illustrate virtual function in C++
#include<iostream.h>
#include<conio.h>
class Base
{
public:
virtual void display()
{
cout<<"\nIn Virtual Display of Base class";
}
void putdata()
{
cout<<"\nIn Putdata of base class";
}
};
class Derived:public Base
{
public:
void display()
{
cout<<"\nIn Display of Derived class";
}
};
void main()
{
Base *ptr;
Base B;
ptr=&B;
clrscr();
ptr->display();
Derived D;
ptr=&D;
ptr->display();
ptr->putdata();
getch();
}
OUTPUT
In Virtual Display Of Base Class
In Display Of Derived Class
In Putdata Of Base Class