PROGRAM USING VIRTUAL FUNCTIONS
/*WRITE A PROGRAM USING VIRTUAL FUNCTIONS*/
#include<iostream.h>
#include<conio.h>
class base
{
public:
void display()
{
cout<<"\nDisplay base";
}
virtual void show()
{
cout<<"\nShow base";
}
};
class derived:public base
{
public:
void display()
{
cout<<"\nDisplay derived";
}
void show()
{
cout<<"\nShow derived";
}
};
void main()
{
base b;
derived d;
base*bptr;
cout<<"\nbptr points to base";
bptr=&b;
bptr->display();
bptr->show();
cout<<"\nbptr points to derived";
bptr=&d;
bptr->display();
bptr->show();
return0;
}
----------------------------------------------------------
OUTPUT:-
#include<iostream.h>
#include<conio.h>
class base
{
public:
void display()
{
cout<<"\nDisplay base";
}
virtual void show()
{
cout<<"\nShow base";
}
};
class derived:public base
{
public:
void display()
{
cout<<"\nDisplay derived";
}
void show()
{
cout<<"\nShow derived";
}
};
void main()
{
base b;
derived d;
base*bptr;
cout<<"\nbptr points to base";
bptr=&b;
bptr->display();
bptr->show();
cout<<"\nbptr points to derived";
bptr=&d;
bptr->display();
bptr->show();
return0;
}
----------------------------------------------------------
OUTPUT:-
0 comments