Question

C++ ONLY PLEASE Problem Description: Objective: Create Inheritance Hierarchy to Demonstrate Polymorphic Behavior Create a Rectangle...

C++ ONLY PLEASE

Problem Description:

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

a) Create a Container to hold objects of the types described above

b) Create and Add two(2) objects of each type described above

c) 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.

Document your code, tell me what you are doing, and make it readable. Creativeness and Quality Craftsmanship do count.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include <iostream>
#include <cmath> //for M_PI, the value of PI

using namespace std;
class Rectangle
{
protected:
float length;
float breadth;
public:
Rectangle() //default constructor
{
length=breadth=0;
}
Rectangle(float l,float b)
{
length=l;
breadth=b;
}
float Area() //calculate area
{
return length*breadth;
}
float perimeter() //calculate Perimeter
{
return 2*(length+breadth);
}
void getDetails() //get data
{
cout<<"Length of Rectangle : "<<length<<endl;
cout<<"Breadth of Rectangle : "<<breadth<<endl;
}
void setDetails(float l,float b) //set data
{
length=l;
breadth=b;
}
};
class Square: public Rectangle
{
public:
void getDetails()
{
cout<<"Each Side of the Square = "<<length<<endl;
}
void setDetails(float l)
{
length=breadth=l;
}
Square() //default constructor
{
length=breadth=0;
}
Square(float l) //constructor
{
length=breadth=l;
}
};
class RightTriangle:public Rectangle
{
public:
RightTriangle() //default constructor
{
length=breadth=0; //for a triangle, breadth=base and length=height
}
RightTriangle(float b,float h) //constructor
{
length=h;
breadth=b;
}
void getDetails() //get data
{
cout<<"Base of Right-Triangle : "<<breadth<<endl;
cout<<"Height of Right-Triangle : "<<length<<endl;
}
float Area()
{
return (breadth*length)/2; //area of a triangle = 1/2*base*height
}
float perimeter() //perimeter of the right-triangle
{
float hyp=sqrt(length*length + breadth*breadth); //pythagoras theorem to find hypotenuse
return hyp+length+breadth;
}
};
class Prism:public RightTriangle //we're only considering triangular prisms
{
float height;
public:
Prism()
{
height=length=breadth=0;
}
Prism(float l,float b,float h)
{
length=l;
breadth=b;
height=h;
}
void setDetails(float l,float b,float h)
{
length=l;
breadth=b;
height=h;
}
void getDetails()
{
cout<<"Height of the Prism : "<<height<<endl;
cout<<"Length of its base : "<<length<<endl;
cout<<"Breadth of its base : "<<breadth<<endl;
}
float surfaceArea()
{
return 2*this->Area()+this->perimeter()*height;
}
float volume()
{
return this->Area()*height;
}
};
class Circle
{
protected:
float radius;
public:
Circle()
{
radius=0;
}
Circle(float r)
{
radius=r;
}
void setDetails(float r)
{
radius=r;
}
void getDetails()
{
cout<<"Radius of the Circle : "<<radius<<endl;
}
float Area()
{
return M_PI*radius*radius;
}
float perimeter()
{
return 2*M_PI*radius; //circumference of a circle=its perimeter
}
};
class Sphere:public Circle
{
public:
void getDetails()
{
cout<<"Radius of the Sphere : "<<radius<<endl;
}
float volume()
{
return (4*M_PI*radius*radius*radius)/3;
}
float surfaceArea()
{
return 4*M_PI*radius*radius;
}
};

class Driver
{
Rectangle R1,R2;
Square S1,S2;
RightTriangle RT1,RT2;
Circle C1,C2;
Sphere SP1,SP2;
Prism P1,P2;
public: //create different objects by calling the polymorphic method setDetails() for different shapes
void createRectangles()
{
float l,b;
cout<<"Enter Length of the 1st Rectangle : ";
cin>>l;
cout<<"Enter Breadth of the 1st Rectangle : ";
cin>>b;
R1.setDetails(l,b);
cout<<"Enter Length of the 2nd Rectangle : ";
cin>>l;
cout<<"Enter Breadth of the 2nd Rectangle : ";
cin>>b;
R2.setDetails(l,b);
}
void createSquares()
{
float l;
cout<<"Enter Length of the 1st Square : ";
cin>>l;
S1.setDetails(l);
cout<<"Enter Length of the 2nd Square : ";
cin>>l;
S2.setDetails(l);
}
void createRightTriangles()
{
float l,b;
cout<<"Enter Length of the 1st Right Triangle : ";
cin>>l;
cout<<"Enter Breadth of the 1st Right Triangle : ";
cin>>b;
RT1.setDetails(l,b);
cout<<"Enter Length of the 2nd Right Triangle : ";
cin>>l;
cout<<"Enter Breadth of the 2nd Right Triangle : ";
cin>>b;
RT2.setDetails(l,b);
}
void createCircles()
{
float r;
cout<<"Enter the radius of the 1st circle : ";
cin>>r;
C1.setDetails(r);
cout<<"Enter the radius of the 2nd circle : ";
cin>>r;
C2.setDetails(r);
}
void createSpheres()
{
float r;
cout<<"Enter the radius of the 1st Sphere : ";
cin>>r;
SP1.setDetails(r);
cout<<"Enter the radius of the 2nd Sphere : ";
cin>>r;
SP2.setDetails(r);
}
void createPrisms()
{
float l,b,h;
cout<<"Enter Height of the 1st Prism : ";
cin>>h;
cout<<"Enter Length of the 1st Prism : ";
cin>>l;
cout<<"Enter Breadth of the 1st Prism : ";
cin>>b;
P1.setDetails(l,b,h);
cout<<"Enter Height of the 1st Prism : ";
cin>>h;
cout<<"Enter Length of the 1st Prism : ";
cin>>l;
cout<<"Enter Breadth of the 1st Prism : ";
cin>>b;
P2.setDetails(l,b,h);
}
void ShowData() /*show the details of different objects using getdetails() and calculate thier area, perimeter,
volume & surface area using polymorphic methods; methods with same names for different objects*/
{
cout<<"1st Rectangle :- ";
R1.getDetails();
cout<<"Area = "<<R1.Area()<<endl;
cout<<"Perimeter = "<<R1.perimeter()<<endl;
cout<<"2nd Rectangle :- ";
R2.getDetails();
cout<<"Area = "<<R2.Area()<<endl;
cout<<"Perimeter = "<<R2.perimeter()<<endl;
cout<<" 1st Square :- ";
S1.getDetails();
cout<<"Area = "<<S1.Area()<<endl;
cout<<"Perimeter = "<<S1.perimeter()<<endl;
cout<<"2nd Square :- ";
S2.getDetails();
cout<<"Area = "<<S2.Area()<<endl;
cout<<"Perimeter = "<<S2.perimeter()<<endl;
cout<<"1st Right Triangle :- ";
RT1.getDetails();
cout<<"Area = "<<RT1.Area()<<endl;
cout<<"Perimeter = "<<RT1.perimeter()<<endl;
cout<<"2nd Right Triangle :- ";
RT2.getDetails();
cout<<"Area = "<<RT2.Area()<<endl;
cout<<"Perimeter = "<<RT2.perimeter()<<endl;
cout<<" 1st Circle :- ";
C1.getDetails();
cout<<"Area = "<<C1.Area()<<endl;
cout<<"Perimeter = "<<C1.perimeter()<<endl;
cout<<"2nd Circle :- ";
C2.getDetails();
cout<<"Area = "<<C2.Area()<<endl;
cout<<"Perimeter = "<<C2.perimeter()<<endl;
cout<<" 1st Sphere :- ";
SP1.getDetails();
cout<<"Area = "<<SP1.Area()<<endl;
cout<<"Perimeter = "<<SP1.perimeter()<<endl;
cout<<"Volume = "<<SP1.volume()<<endl;
cout<<"Surface Area = "<<SP1.surfaceArea()<<endl;
cout<<"2nd Sphere :- ";
SP2.getDetails();
cout<<"Area = "<<SP2.Area()<<endl;
cout<<"Perimeter = "<<SP2.perimeter()<<endl;
cout<<"Volume = "<<SP2.volume()<<endl;
cout<<"Surface Area = "<<SP2.surfaceArea()<<endl;
cout<<" 1st Prism :- ";
P1.getDetails();
cout<<"Area = "<<P1.Area()<<endl;
cout<<"Perimeter = "<<P1.perimeter()<<endl;
cout<<"Volume = "<<P1.volume()<<endl;
cout<<"Surface Area = "<<P1.surfaceArea()<<endl;
cout<<"2nd Prism :- ";
SP2.getDetails();
cout<<"Area = "<<P2.Area()<<endl;
cout<<"Perimeter = "<<P2.perimeter()<<endl;
cout<<"Volume = "<<P2.volume()<<endl;
cout<<"Surface Area = "<<P2.surfaceArea()<<endl;
}
void CreateObjects() //call different methods which will accept user input and create objects
{
createRectangles();
createSquares();
createCircles();
createSpheres();
createRightTriangles();
createPrisms();
}
};
int main()
{
Driver D;
D.CreateObjects(); //create objects
D.ShowData(); //show information about these created objects

return 0;
}

Output:-

Add a comment
Know the answer?
Add Answer to:
C++ ONLY PLEASE Problem Description: Objective: Create Inheritance Hierarchy to Demonstrate Polymorphic Behavior Create a Rectangle...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Description Create an object-oriented program that uses inheritance to perform calculations on a rectangle or a...

    Description Create an object-oriented program that uses inheritance to perform calculations on a rectangle or a square. Sample Output (Your output should be similar to the text in the following box) Rectangle Calculator Rectangle or square? (r/s): r Height: 5 Width: 10 Perimeter: 30 Area: 50 Continue? (y/n): y Rectangle or square? (r/s): s Length: 5 Perimeter: 20 Area: 25 Continue? (y/n): n Thank you for using my app Specifications Use a Rectangle class that provides attributes to store the...

  • Java code for the following inheritance hierarchy figure.. 1. Create class Point, with two private instance...

    Java code for the following inheritance hierarchy figure.. 1. Create class Point, with two private instance variables x and y that represented for the coordinates for a point. Provide constructor for initialising two instance variables. Provide set and get methods for each instance variable, Provide toString method to return formatted string for a point coordinates. 2. Create class Circle, its inheritance from Point. Provide a integer private radius instance variable. Provide constructor to initialise the center coordinates and radius for...

  • in C++ use Inheritance for the following Shape Circle Sphere Cylinder Rectangle Square Cube You must define one class for each shape. And, use public inheritance when deriving from base cla...

    in C++ use Inheritance for the following Shape Circle Sphere Cylinder Rectangle Square Cube You must define one class for each shape. And, use public inheritance when deriving from base classes. Circle int r; void area(); void perimeter(); void volume(); //No volume for circle Sphere int r; void area();//Sphere surface area= 4 × pi × radius2 void perimeter(); //No perimeter for Sphere void volume();//Sphere volume= 4/3 × pi × radius2 Cylinder int r, height; void area();// Cylinder surface area=perimeter of...

  • Please use C++ and Please do all file separate separately. And also I need output too....

    Please use C++ and Please do all file separate separately. And also I need output too. thanks. Please do it complete work. ShapeNodePoly.cpp Avaliable from: Wednesday, July 27, 2016, 10:20 AM Requested files: Point.h, Point.cpp, Shape.h, Shape.cpp, Polygon.h, Polygon.cpp, Ellipse.h, Ellipse.cpp, ShapeNodePoly.cpp, ShapeNodePoly_test.cpp (Download) Type of work: Individual work In this assignment, you will create a class that can behave as any of the shapes listed below. You will use object inheritance to enable all shapes to be managed using...

  • Please show it in C++. Thank you! Problem Definition Create an inheritance hierarchy containing base class...

    Please show it in C++. Thank you! Problem Definition Create an inheritance hierarchy containing base class Account and derived class Savings-Account. Base class Account should include one data member of type double to represent the account balance. The class should provide a constructor that receives an initial baiance and uses it to initialize the data member. The class should provide three member functions. Member function credit should add an amount to the current balance. Member function debit should withdraw money...

  • Assignment Requirements I have also attached a Class Diagram that describes the hierarchy of the inheritance...

    Assignment Requirements I have also attached a Class Diagram that describes the hierarchy of the inheritance and interface behaviors . The link to the PDF of the diagram is below MotorVehical.pdf Minimize File Preview User Define Object Assignment: Create a Intellij Project. The Intellij project will contain three user defined classes. The project will test two of the User Define Classes by using the invoking each of their methods and printing the results. You are required to create three UML...

  • In C++ Create an inheritance hierarchy that a bank might use to represent customers’ bank accounts. All customers at this bank can deposit (i.e., credit) money into their accounts and withdraw (i.e.,...

    In C++ Create an inheritance hierarchy that a bank might use to represent customers’ bank accounts. All customers at this bank can deposit (i.e., credit) money into their accounts and withdraw (i.e., debit) money from their accounts. More specific types of accounts also exist. Savings accounts, for instance, earn interest on the money they hold. Checking accounts, on the other hand, charge a fee per transaction (i.e., credit or debit). Create an inheritance hierarchy containing base class Account and derived...

  • Please show all work and answer all parts using python, thanks! Instructions You will need to...

    Please show all work and answer all parts using python, thanks! Instructions You will need to create four files: • Shape2D.py - file containing a class definition containing properties all Shapes could possibly have. • Circle.py - file containing a class definition of a Circle that inherits from the Shape2D class. Square.py - file containing a class definition of a Square that inherits from the Shape2D class. • testFile.py - file containing pytest functions testing the Shape2D, Circle, and Square...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT