In: Computer Science
Please do this in C++
Objective: Create an Inheritance Hierarchy to Demonstrate Polymorphic Behavior
1. Create a Rectangle
2. Class Derive a Square Class from the Rectangle Class
3. Derive a Right Triangle Class from the Rectangle Class
4. Create a Circle Class
5. Create a Sphere Class
6. Create a Prism Class
7. Define any other classes necessary to implement a solution
8. Define a Program Driver Class for Demonstration
(Create a Container to hold objects of the types described above Create and Add two(2) objects of each type described above Compute the area, perimeter, and volume in a uniform manner for each object in the Container. Display the results. Note: the perimeter is not defined for a 3D object and volume is not defined for a 2D object.)
#include<iostream>
#include<math.h>
using namespace std;
class Rectangle
{
public:
Rectangle(int length, int breadth)
{
this->length = length;
this->breadth = breadth;
}
int length;
int breadth;
void print()
{
cout<<"Details of Rectangle with sides
"<<length<<" and "<<breadth<<endl;
cout << "Area is : " <<
length*breadth << endl;
cout << "Perimeter : "
<< 2*(length+breadth) << endl;
cout << "Volume : Not
Defined"<<endl;
}
};
class Square : public Rectangle
{
public :
Square(int side,int side1):Rectangle(side,side1)
{
this->length = side;
this->breadth = side;
}
void print()
{
cout<<"Details of Square with side
"<<length<<" is : "<<endl;
cout << "Area is : " <<
length*breadth << endl;
cout << "Perimeter : "
<< 2*(length+breadth) << endl;
cout << "Volume : Not
Defined"<<endl;
}
};
class RightAngledTriangle : public Rectangle
{
public :
int hyp;
public:
RightAngledTriangle(int base, int
height):Rectangle(base,height)
{
this->length = base;
this->breadth = height;
this->hyp =
sqrt(pow(length,2)+pow(breadth,2));
}
void print()
{
cout<<"Details of Right Angled Triangle with
sides "<<length<<" and
"<<breadth<<endl;
cout << "Area is : " <<
length*breadth/2 << endl;
cout << "Perimeter : "
<< length+breadth+hyp << endl;
cout << "Volume : Not
Defined"<<endl;
}
};
class Circle
{
public:
Circle(int radius)
{
this->radius = radius;
}
int radius;
void print()
{
cout<<"Details of Circle with radius
"<<radius<<" is "<<endl;
cout << "Area is : " <<
3.14*pow(radius,2) << endl;
cout << "Perimeter : "
<< 2*3.14*radius << endl;
cout << "Volume : Not
Defined"<<endl;
}
};
class Sphere
{
public:
Sphere(int radius,int height)
{
this->radius = radius;
this->height = height;
}
int radius;
int height;
void print()
{
cout<<"Details of Sphere with radius and height
"<<radius<<", "<<height<<" is :
"<<endl;
cout << "Area is : " <<
4*3.14*pow(radius,2) << endl;
cout << "Perimeter : Not
Defined" << endl;
cout << "Volume : "<<
4*3.14*pow(radius,3)/3<<endl;
}
};
class Prism
{
public:
Prism(int length,int width,int height)
{
this->length = length;
this->width= width;
this->height = height;
}
int length;
int width;
int height;
void print()
{
cout<<"Details of Prism with length, width and
height "<<length<<", "<<width<<",
"<<height<<" is : "<<endl;
cout << "Area is : " <<
2*(length*width + width*height + height*length) <<
endl;
cout << "Perimeter : Not
Defined" << endl;
cout << "Volume : "<<
length * width* height<<endl;
}
};
int main(){
Rectangle r1(10,20);
r1.print();
Rectangle r2(1,2);
r2.print();
Square s1(2,2);
s1.print();
Square s2(5,5);
s2.print();
RightAngledTriangle rt1(3,4);
rt1.print();
RightAngledTriangle rt2(6,8);
rt2.print();
Circle c1(3);
c1.print();
Circle c2(10);
c2.print();
Sphere sp1(2,3);
sp1.print();
Sphere sp2(10,15);
sp2.print();
Prism p1(2,3,5);
p1.print();
Prism p2(10,12,13);
p2.print();
return 0;
}
Output
Details of Rectangle with sides 10 and 20
Area is : 200
Perimeter : 60
Volume : Not Defined
Details of Rectangle with sides 1 and 2
Area is : 2
Perimeter : 6
Volume : Not Defined
Details of Square with side 2 is :
Area is : 4
Perimeter : 8
Volume : Not Defined
Details of Square with side 5 is :
Area is : 25
Perimeter : 20
Volume : Not Defined
Details of Right Angled Triangle with sides 3 and 4
Area is : 6
Perimeter : 12
Volume : Not Defined
Details of Right Angled Triangle with sides 6 and 8
Area is : 24
Perimeter : 24
Volume : Not Defined
Details of Circle with radius 3 is
Area is : 28.26
Perimeter : 18.84
Volume : Not Defined
Details of Circle with radius 10 is
Area is : 314
Perimeter : 62.8
Volume : Not Defined
Details of Sphere with radius and height 2, 3 is :
Area is : 50.24
Perimeter : Not Defined
Volume : 33.4933
Details of Sphere with radius and height 10, 15 is :
Area is : 1256
Perimeter : Not Defined
Volume : 4186.67
Details of Prism with length, width and height 2, 3, 5 is :
Area is : 62
Perimeter : Not Defined
Volume : 30
Details of Prism with length, width and height 10, 12, 13 is
:
Area is : 812
Perimeter : Not Defined
Volume : 1560