Write a C++ program to demonstrate virtual function..
Code :-
/************************************************
TITLE:Programe for VIRTUAL FUNCTION
**************************************************/
# include<iostream.h>
# include<conio.h>
class base
{
public:
void disp()
{
cout<<"\nBase Class Display() call";
}
virtual void show()
{
cout<<"\nBase Class Show() call";
}
};
class derived:public base
{
public:
void disp()
{
cout<<"\nDerived class Display() Call";
}
void show()
{
cout<<"\nDerived Class Show() Call";
}
};
void main()
{
clrscr();
base b1;
derived d1;
cout<<"\nBase Pointer BPTR Points to base class\n";
base *bptr;
bptr=&b1;
bptr->disp();
bptr->show();
cout<<"\n\nBase Pinter BPTR Points to Derived class\n";
bptr=&d1;
bptr->disp();
bptr->show();
getch();
}
Output :-