c++ Objective: Create Inheritance Hierarchy to Demonstrate Polymorphic Behavior
ID: 3817303 • Letter: C
Question
c++
Objective: Create Inheritance Hierarchy to Demonstrate Polymorphic Behavior
Create a Rectangle Class
Derive a Square Class from the Rectangle Class
Derive a Right Triangle Class from the Rectangle Class
Create a Circle Class
Create a Sphere Class
Create a Prism Class
Define any other classes necessary to implement a solution
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: perimeter is not defined for a 3D object and volume is not defined for a 2D object
Explanation / Answer
#include <iostream>
using namespace std;
class Rectangle // Class Rectangle
{
public:
Rectangle(){} // Constructor
float getLength(){
return length;
}
void setLength(float L){
length = L;
}
float getWidth(){
return width;
}
void setWidth(float W){
width = W;
}
double perimeter(void){
return (length*2 + width*2);
} // Perimeter function
double area(void) {
return (length*width);
} // Area funcion
private:
float length;
float width;
}; // End of class Rectangle
class Square : public Rectangle // Class Rectangle
{
public:
Square(){} // Constructor
float getLength(){
return length;
}
void setLength(float L){
length = L;
}
double perimeter(void){
return (length*4);
} // Perimeter function
double area(void) {
return (length*length);
} // Area funcion
private:
float length;
float width;
}; // End of class Rectangle
class Triangle : public Rectangle// Class Rectangle
{
public:
Triangle(){} // Constructor
float getLength(){
return length;
}
void setLength(float L){
length = L;
}
float getBreadth(){
return breadth;
}
void setBreadth(float W){
breadth = W;
}
float getHeight(){
return Height;
}
void setHeight(float W){
Height = W;
}
double perimeter(void){
return ((length + breadth + Height)/2);
} // Perimeter function
double area(void) {
float p=((length + breadth + Height)/2);
return (sqrt(p*(p - length)*(p - breadth)*(p - Height)));
} // Area funcion
private:
float length;
float breadth;
float Height;
}; // End of class Rectangle
class circleClass
{
private:
double radius;
public:
circleClass();
circleClass(double r){
radius = r;
}
void setradius(double r){
radius = r;
}
double getradius(){
return radius;
}
double area(){
return (radius*radius*3.14159);
}
double circum(){
return (2*3.14159*radius);
}
};//end of class definition
class sphere{
private:
float radius;
public:
sphere(float Radius){
radius = Radius;
}
float get_radius(){
return radius;
}
void set_radius(float r){
radius = r;
}
float get_volume(){
return 4.0/3.0 * 3.14159 *pow(radius,3);
}
float get_surface_area(){
return 4.0 * 3.14159 * pow(radius,2);
}
};
int main(){
Rectangle r1, r2;
Square s1, s2;
Triangle t1, t2;
circleClass c1, c2;
r1.setLength(5);
r1.setWidth(9);
cout<<" The length of Rectangle1 : "<<r1.getLength();
cout<<" The width of Rectangle1 : "<<r1.getWidth();
cout<<" The area of Rectangle1 : "<<r1.area();
cout<<" The perimeter of Rectangle1 : "<<r1.perimeter();
r2.setLength(140);
r2.setWidth(50);
cout<<" The length of Rectangle2 : "<<r2.getLength();
cout<<" The width of Rectangle2 : "<<r2.getWidth();
cout<<" The area of Rectangle2 : "<<r2.area();
cout<<" The perimeter of Rectangle2 : "<<r2.perimeter();
s1.setLength(21);
cout<<" The length of Square1 : "<<s1.getLength();
cout<<" The area of Square1 : "<<s1.area();
cout<<" The perimeter of Square1 : "<<s1.perimeter();
s2.setLength(55);
cout<<" The length of Square2 : "<<s2.getLength();
cout<<" The area of Square2 : "<<s2.area();
cout<<" The perimeter of Square2 : "<<s2.perimeter();
t1.setBreadth(5);
t1.setHeight(6);
t1.setLength(7);
cout<<" The area of Triangle1 : "<<t1.area();
cout<<" The perimeter of Triangle1 : "<<t1.perimeter();
t2.setBreadth(15);
t2.setHeight(61);
t2.setLength(27);
cout<<" The area of Triangle2 : "<<t2.area();
cout<<" The perimeter of Triangle2 : "<<t2.perimeter();
c1.setradius(5.5);
cout<<" The area of Circle1 : "<<c1.area();
cout<<" The circumference of Circle1 : "<<c1.circum();
c2.setradius(52.25);
cout<<" The area of Circle2 : "<<c2.area();
cout<<" The circumference of Circle2 : "<<c2.circum();
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.