Question

Hi I need help doing this problem *The program must re-prompt as long as invalid input...

Hi I need help doing this problem

*The program must re-prompt as long as invalid input is inserted

Implement a program that manages shapes. Implement a class named Shape with a virtual function area()which returns the double value. Implement three derived classes named Diamond, Oval, and Pentagon. Declare necessary properties in each including getter and setter function and a constructor that sets the values of these properties. Override the area() function in each by calculating the area using the defined properties in that class. Write a program that repeatedly shows the user a menu to create one of the three main shapes or to print the shapes created so far. If the user selects to create a new shape, the program prompts the user to enter the values for the size of the selected shape. The shape is then stored in an array*. If the user selects to print the current shapes, print the name and the total area of each shape to the console. Creating objects for testing all classes in the main function.

*The array must hold pointers to Shape objects. You may limit the size of the array to 10.

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

Note: Could you plz go through this code and let me know if u need any changes in this.Thank You
_________________

#include <iostream>
#include <iomanip>
#include <ctime>
#include <cstdlib>
using namespace std;

class Shape
{
   private :
       string name;
       public :
           Shape(string n)
           {
               this->name=n;
           }
           virtual double area()=0;
               string getShape()
           {
               return name;
           }
};
class Diamond:public Shape
{
   private :
       double base;
       double height;
       public :
           Diamond(double b,double h,string name):Shape(name)
           {
               this->base=b;
               this->height=h;
           }
          
           double area()
           {
               return (base*height)/2;
           }
      
};

class Oval:public Shape
{
private :
   double a;
   double b;
  
public :  
Oval(double a,double b,string name):Shape(name)
{
this->a=a;
this->b=b;
   }
   double area()
   {
       return a*b*3.14159;
   }
  
};
class Pentagon:public Shape
{
   private :
       double s;
       double a;
      
       public :
           Pentagon(double s,double a,string name):Shape(name)
           {
               this->s=s;
               this->a=a;
           }
           double area()
           {
               return (5*(s*a))/2;
           }
};
int main()
{
int choice,cnt=0;
const int SIZE=10;
Shape* shapes[SIZE];
  
while(true)
{
cout<<"\n1.Oval"<<endl;
cout<<"2.Pentagon"<<endl;
cout<<"3.Diamond"<<endl;
cout<<"4.Display all Shapes"<<endl;
cout<<"5.Exit"<<endl;
cout<<"Enter Choice :";
cin>>choice;
if(cnt>=SIZE)
{
   cout<<"** Array is full **"<<endl;
   continue;
}
switch(choice)
{
   case 1:{
  
       double a,b;
       cout<<"Enter Minor Radius (a):";
       cin>>a;
       cout<<"Enter Major Radius (a):";
       cin>>b;
       Oval* o=new Oval(a,b,"Oval");
       shapes[cnt]=o;
       cnt++;
  
       continue;
   }
   case 2:{
       double s,a;
       cout<<"Enter the side length (s):";
       cin>>s;
       cout<<"Enter the length of apothem (a):";
       cin>>a;
       Pentagon* pen=new Pentagon(s,a,"Pentagon");
       shapes[cnt]=pen;
       cnt++;
       continue;
   }
   case 3:{
       double b,h;
       cout<<"Enter the length of base (b):";
       cin>>b;
       cout<<"Enter the height (h):";
       cin>>h;
      
       Diamond* d=new Diamond(b,h,"Diamond");
       shapes[cnt]=d;
       cnt++;
       continue;
   }
   case 4:{
       for(int i=0;i<cnt;i++)
       {
           cout<<"Shape Name :"<<shapes[i]->getShape()<<endl;
           cout<<"Area :"<<shapes[i]->area()<<endl;
           cout<<"------------------------------"<<endl;
       }
       continue;
   }
   case 5:{
      
       break;
   }
   default:{
       cout<<"** Invalid Choice **"<<endl;
       continue;
   }

}
   break;
}
  
return 0;
}

_____________________________

Output:

_______________Could you plz rate me well.Thank You

Add a comment
Know the answer?
Add Answer to:
Hi I need help doing this problem *The program must re-prompt as long as invalid input...
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
  • Write a C++ program for the instructions below. Please read the instructions carefully and make sure they are followed correctly.   please put comment with code! and please do not just copy other solu...

    Write a C++ program for the instructions below. Please read the instructions carefully and make sure they are followed correctly.   please put comment with code! and please do not just copy other solutions. Instructions 1. Read instructions carefully! 2. Use C++ syntax only, C syntax will not be accepted. 3. Always use braces to define blocks. 4. Indent all lines within a block. Each block requires one more tab. 5. Organize your code well with proper formatting and a single...

  • JAVA Problem:  Coffee    Design and implement a program that manages coffees. First, implement an abstract class...

    JAVA Problem:  Coffee    Design and implement a program that manages coffees. First, implement an abstract class named Coffee. The Coffee class has three member variables: coffee type, price, and store name. The Coffee class has an abstract method name ingredient. Make sure to add all necessary setters and getters as well as other methods. The class Coffee implements an interface HowToMakeDrink. The interface has the following method: public interface HowToMakeDrink { public void prepare (); } The prepare method will...

  • JAVA Problem:  Coffee do not use ArrayList or Case Design and implement a program that manages coffees....

    JAVA Problem:  Coffee do not use ArrayList or Case Design and implement a program that manages coffees. First, implement an abstract class named Coffee. The Coffee class has three member variables: coffee type, price, and store name. The Coffee class has an abstract method name ingredient. Make sure to add all necessary setters and getters as well as other methods. The class Coffee implements an interface HowToMakeDrink. The interface has the following method: public interface HowToMakeDrink { public void prepare ();...

  • Problem: Coffee(Java) Design and implement a program that manages coffees. First, implement an abstract class named...

    Problem: Coffee(Java) Design and implement a program that manages coffees. First, implement an abstract class named Coffee. The Coffee class has three member variables: coffee type(does not mention the data type of the variable coffeeType), price, and store name. The Coffee class has an abstract method name ingredient. Make sure to add all necessary setters and getters as well as other methods. The class Coffee implements an interface HowToMakeDrink. The interface has the following method: public interface HowToMakeDrink { public...

  • Write a program in C++ with comments! Create an abstract class Property, containing the data items...

    Write a program in C++ with comments! Create an abstract class Property, containing the data items owner, address and price. Add an abstract method showOwner(). Also provide getter/setters for all the data items. Make validations if necessary in the setter so that no invalid values are entered. Create a class House, to inherit the Property class. This class should contain additional data item – yardSize – an integer – the size of the yard of the house, getter and setter...

  • QUESTION 5 (15 Marks) Inheritance and Polymorphism Design a Ship class that has the following members:...

    QUESTION 5 (15 Marks) Inheritance and Polymorphism Design a Ship class that has the following members: • A private String data field named name for the name of the ship. • A private String data field named yearBuilt for the year that the ship was built. • A constructor that creates a ship with the specified name and the specified year that the ship was built. • Appropriate getter and setter methods. A toString method that overrides the toString method...

  • c++ driver and car are independed classes 1. Create a class Car, which has a color, engine, horsepower, fuel, FuelLevel, year of manufacturing and driver which can be defined by name, age, licen...

    c++ driver and car are independed classes 1. Create a class Car, which has a color, engine, horsepower, fuel, FuelLevel, year of manufacturing and driver which can be defined by name, age, licenseNumber. Create the corresponding OOP in For each class (Driver, Car) write a header file and the class implementation -In the main function do the following: from that class in main function. L.1 Write a function that will print the total number of objects created 12 Define an...

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

  • Help with this coding assignment for C++! Add any comments for better understanding. Create a class...

    Help with this coding assignment for C++! Add any comments for better understanding. Create a class named CupCake. It contains: • Has private instance variables 1. egg 2. butter 3. baking powder 4. sugar 5. flour 6. milk • All members must have public methods: getter() and setter(). • A constructor - a default constructor with no arguments. The constructor will initial all member variable to a default value 0. • One public pure virtual function showingredients() that returns void....

  • Using Python Write the following well-documented (commented) program. Create a class called Shape that has a...

    Using Python Write the following well-documented (commented) program. Create a class called Shape that has a method for printing the area and the perimeter. Create three classes (Square, Rectangle, and Circle) which inherit from it. Square will have one instance variable for the length. Rectangle will have two instance variables for the width and height. Circle will have one instance variable for the radius. The three classes will have methods for computing the area and perimeter of its corresponding shape....

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