Wednesday, September 29, 2010

(OOPS) Implementation of run time polymorphism

#include<iostream.h>
#include<conio.h>
class shapes
{
public:
virtual void area()
{
cout<<"\n program for calculating shapes";
}
};
class circle:public shapes
{
public:
int a,r;
void area()
{
cout<<"\n enter the radius";
cin>>r;
a=3.14*r*r;
cout<<"\n the area of the circle is"<<a;
}
};
class rectangle:public shapes
{
public:
float l,b,a;
void area()
{
cout<<"\n rectangle";
cout<<"\n enter the length";
cin>>l;
cout<<"\n enter the breadth";
cin>>b;
a=l*b;
cout<<"\n the area of the rectangle"<<a;
}
};
class triangle:public shapes
{
public:
float b,h,a;
void area()
{
cout<<"\n triangle";
cout<<"\n enter the breadth";
cin>>b;
cout<<"\n enter the height";
cin>>h;
a=0.5*b*h;
cout<<"\n area of the triangle is"<<a;
}
};
class square:public shapes
{
public:
float s,a;
void area()
{
cout<<"\n square";
cout<<"\n enter the side of the square";
cin>>s;
a=s*s;
cout<<"\n the area of the square is"<<a;
}
};
void main()
{
int ch=0;
shapes *p;
circle a1;
rectangle b1;
triangle c1;
square d1;
do{
cout<<"\n ....Menu....";
cout<<"\n1.circle";
cout<<"\n2.Rectangle";
cout<<"\n3.Triangle";
cout<<"\n4.Square";
cout<<"\n Enter your choice";
cin>>ch;
switch(ch)
{
case 1:
p=&a1;
p->area();
break;
case 2:
p=&b1;
p->area();
break;
case 3:
p=&c1;
p->area();
break;
case 4:
p=&d1;
p->area();
break;
}
}while(ch!=5);
getch();
}