Monday, 22 December 2014

C++ PROGRAM: implement the following class hierarchy and implement the methods to input dimensions of shape and also calculate area of each shape.


SOLUTION:
#include<iostream.h>
#include<conio.h>
#include<math.h>
class shape
{
public:
int a;
};
class two_dimensional:public shape
{
public:
int d1,d2;
};
class three_dimensional:public shape
{
public:
int d1,d2;
};
class triangle:public two_dimensional
{
public:
void area()
{
cout<<"\n\nEnter the Triangle dimensions (Base,Vertical Height):";
cin>>d1>>d2;
a=0.5*(d1*d2);
cout<<"The area of Triangle is="<<a;
}
};
class rectangle:public two_dimensional
{
public:
void area()
{
cout<<"\n\n Enter the Rectangle dimenssions (Width,Height):";
cin>>d1>>d2;
a=d1*d2;
cout<<"The area of Rectangle is="<<a;
}
};

class box:public three_dimensional
{
public:
void area()
{
cout<<"\n\nEnter the Box dimenssion of any side:";
cin>>d1;
a=d1*d1*d1;
cout<<"The area of Box is="<<a;
}
};

class cone:public three_dimensional
{
public:
void area()
{
cout<<"\n\nEnter the dimenssions of Cone (Radius,Height):";
cin>>d1>>d2;
a=M_PI*d1*(d1+sqrt(d1*d1+d2*d2));
cout<<"The area of the Cone is="<<a;
}
};

class sphere:public three_dimensional
{
public:
void area()
{
cout<<"\n\nEnter the dimensssion of Sphere (Radius):";
cin>>d1;
a=4*M_PI*d1*d1;
cout<<"The area of Sphere is="<<a;
}
};

void main()
{
int ch;
triangle t;
rectangle r;
box b;
cone c;
sphere s;
clrscr();
cout<<"Press:\n1.Area of Triangle\n2.Area of Rectangle\n3.Area of Box\n4.Area of Cone\n5.Area of Sphere";
cout<<"\n\nEnter your choice:";
cin>>ch;
switch (ch)
{
case 1:
t.area();
break;
case 2:
r.area();
break;
case 3:
b.area();
break;
case 4:
c.area();
break;
case 5:
s.area();
break;
default:
cout<<"\nYou didnot enter the correct choice.";
break;
}
getch();
}
OUTPUT:




No comments:

Post a Comment