VIRTUAL FUNCTION

VIRTUAL FUNCTION
#include
#include
class shape
{
protected:
double x,y,z;
public:
void get_area(double x1,double y1,double z1)
{
x=x1;
y=y1;
z=z1;
}
virtual void disp()=0;
};
class rect:public shape
{
void disp()
{
cout<<"area of rectangle="<< x*y <<"\n";
cout<<"\n";
}
};
class tria:public shape
{
void disp()
{
cout<<"area of triangle="<<(x*y)/2<<"\n";
cout<<"\n";
}
};
class cir:public shape
{
void disp()
{
cout<<"area of circle="<<(3.14*y*y)<<"\n";
cout<<"\n";
}
};
int main()
{
float x,y,z;
clrscr();
cout<<"enter the value of x,y,z\n";
cin>>x>>y>>z;
shape *p;
rect r;
p=&r;
p->get_area(x,y,z);
p->disp();
tria t;
p=&t;
p->get_area(x,y,z);
p->disp();
cir c;
p=&c;
p->get_area(x,y,z);
p->disp();
getch();
return 0;
}

Comments