Question

In: Computer Science

Please do this in C++ Objective: Create an Inheritance Hierarchy to Demonstrate Polymorphic Behavior 1. Create...

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

Solutions

Expert Solution

#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


Related Solutions

Objective: Create an Inheritance Hierarchy to Demonstrate Polymorphic Behavior 1. Create a Rectangle 2. Class Derive...
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...
c++ using polymorphyism , create an inheritance hierarchy for the following types of insurance : Home,...
c++ using polymorphyism , create an inheritance hierarchy for the following types of insurance : Home, vehicle , life 2 member functions should be included: - calcpremium    - Home- 0.5% of the value of the home    - Life- 1% of the value of the policy    - Vehicle - 0.5% of the value of the policy calcCommission Home- 0.2% of the value of the home Life- 1% of the value of the policy Vehicle - 0.5% of the...
In C++ Demonstrate inheritance. Create an Airplane class with the following attributes: • manufacturer : string...
In C++ Demonstrate inheritance. Create an Airplane class with the following attributes: • manufacturer : string • speed : float Create a FigherPlane class that inherits from the Airplane class and adds the following attributes: • numberOfMissiles : short
Java Code: Problem #1: Create an inheritance hierarchy of Rodent: mouse, gerbil, hamster, guinea pig. In...
Java Code: Problem #1: Create an inheritance hierarchy of Rodent: mouse, gerbil, hamster, guinea pig. In the base class, provide methods that are common to all rodents based on behaviours you find with a quick Internet search. Be sure to document the behaviours you implement (e.g., eat, sleep, groom, move, etc.). Each behaviour should print its action to standard output (e.g., rodent eating). Next, refine these behaviours in the child classes to perform different behaviours, depending on the specific type...
In c++, when dealing with inheritance in a class hierarchy, a derived class often has the...
In c++, when dealing with inheritance in a class hierarchy, a derived class often has the opportunity to overload or override an inherited member function. What is the difference? and which one is the better?
The objective of this homework is to give you experience using inheritance with C++. You will...
The objective of this homework is to give you experience using inheritance with C++. You will use the provided employee base class and implement two derived classes, salaried employee and hourly employee. For this assignment, you will use the base class Employee, which is implemented with the files Employee.h and Employee.cpp. A test file, test.cpp, is also provided for your use, as you choose to use it. Each employee object has a user id; a first and last name; a...
The solution has to be written on C++ Visual Studio Thank you (Package Inheritance Hierarchy) Package-delivery...
The solution has to be written on C++ Visual Studio Thank you (Package Inheritance Hierarchy) Package-delivery services, such as FedEx®, DHL® and UPS®, offer a number of different shipping options, each with specific costs associated. Create an inheritance hierarchy to represent various types of packages. Use class Package as the base class of the hierarchy, then include classes TwoDayPackage and OvernightPackage that derive from Package. Base class Package should include data members representing the name, address, city, state and ZIP...
in C++ (Please do steps 1-11) 1 ) Create a file that contains your grades and...
in C++ (Please do steps 1-11) 1 ) Create a file that contains your grades and the type of assement (you can just create the file or write the file in the program) A = Assignment E = Extra Credit L - Lab Q = Qui M = Mid Example: L 20 L 20 L 20 Q 10 A 18 E 9 Q 10 L 20 Q 10 L 20 Q 10 L 20 M 85 2 ) create 3...
Using C#, modify the codes below to do the following: Develop a polymorphic banking application using...
Using C#, modify the codes below to do the following: Develop a polymorphic banking application using the Account hierarchy created in the codes below. Create an array of Account references to SavingsAccount and CheckingAccount objects. For each Account in the array, allow the user to specify an amount of money to withdraw from the Account using method Debit and an amount of money to deposit into the Account using method Credit. As you process each Account, determine its type. If...
Section C: Performance Activity Objective: To provide you with an opportunity to demonstrate the required performance...
Section C: Performance Activity Objective: To provide you with an opportunity to demonstrate the required performance elements for this unit. This activity will enable you to demonstrate the following performance evidence: Scope and develop organisational policies and procedures that comply with legislative requirements and support the organisation’s sustainability goals covering at a minimum: minimising resource use resource efficiency reducing toxic material and hazardous chemical use employing life cycle management approaches continuous improvement Plan and implement sustainability policy and procedures including:...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT