PROGRAM FOR VIRTUAL FUNCTION FOR RUNTIME POLYMORPHISM
//program for run time polymorphism using virtual function
#include<iostream.h>
#include<conio.h>
class base
{
public:
void display()
{
cout<<"\n display base:";
}
virtual void show()
{
cout<<"\nshow base:";
}
};
class derived:public base
{
public:
void display()
{
cout<<"\n display derived:";
}
void show()
{
cout<<"\nshow derived:";
}
};
void main()
{
base b;
derived d;
base *bptr;
clrscr();
cout<<"\n bptr points to base\n";
bptr=&b;
bptr->display();
bptr->show();
cout<<"\n\n bptr points to derived\n";
bptr=&d;
bptr->display();
bptr->show();
getch();
}
#include<iostream.h>
#include<conio.h>
class base
{
public:
void display()
{
cout<<"\n display base:";
}
virtual void show()
{
cout<<"\nshow base:";
}
};
class derived:public base
{
public:
void display()
{
cout<<"\n display derived:";
}
void show()
{
cout<<"\nshow derived:";
}
};
void main()
{
base b;
derived d;
base *bptr;
clrscr();
cout<<"\n bptr points to base\n";
bptr=&b;
bptr->display();
bptr->show();
cout<<"\n\n bptr points to derived\n";
bptr=&d;
bptr->display();
bptr->show();
getch();
}

Comments
Post a Comment