PROGRAM USING VIRTUAL FUNCTION IN CPP
#include<conio.h>
#include<iostream.h>
class base
{
public:
void display()
{
cout<<"\n Display
base";
}
virtual void show()
{
cout<<"\n show
base";
}
};
class Derived: public base
{
public:
void display()
{
"\nDisplay
derived";
}
void show()
{
"\n show derived";
}
};
void main()
{
base b;
Derived d;
base *bptr;
clrscr();
cout<<" \nbptr
points to base \n";
bptr=&b;
bptr->display();
bptr->show();
cout<<"\n\nbptr
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
No comments: