#include<iostream.h>
#include<conio.h>

class Roundshape
{
protected:
float radius;

public:
static float PI;
Roundshape(float r)//default argument
{
radius=r;
}
virtual float area()=0;//pure virtual function
};

float Roundshape::PI=3.142;

class Circle:public Roundshape
{
public:
Circle(float r=2):Roundshape(r) {}
float area()
{
return (PI*radius*radius);
}
};

class Sphere:public Roundshape
{
public:
Sphere(float r=3):Roundshape(r) {}
float area()
{
return (4*PI*radius*radius);
}
};

class Cylinder:public Roundshape
{
float height;

public:
Cylinder(float h,float r=4):Roundshape(r)
{
height=h;
}
float area()
{
return (2*PI*radius*(radius+height));
}
};

int main()
{
float r,h;
clrscr();
Circle c1();
cout<<endl<<endl<<"Area of circle is "<<c1.area()<<" sq.units.";
cout<<endl<<"Enter radius: ";
cin>>r;
Circle c2(r);
cout<<endl<<endl<<"Area of circle having radius="<<r<<"units is "<<c2.area()<<" sq.units.";
Sphere s1();
cout<<endl<<endl<<"Area of sphere is "<<s1.area()<<" sq.units.";
Sphere s2(r);
cout<<endl<<endl<<"Area of sphere having radius="<<r<<"units is "<<s2.area()<<" sq.units.";
cout<<endl<<endl<<"Enter height for cylinder: ";
cin>>h;
Cylinder cy1(h);
cout<<endl<<"Area of cylinder having height="<<h<<" is "<<cy1.area()<<" sq.units.";
cout<<endl<<endl<<"Enter height for cylinder: ";
cin>>h;
Cylinder cy2(h,r);
cout<<endl<<"Area of cylinder having radius="<<r<<"units and height="<<h<<" is "<<cy2.area()<<" sq.units.";
getch();
return 0;
}









Post a Comment

Previous Post Next Post