Question

Create a program that calculates the area of various shapes.

Console

The Area Calculator Circle, square, or rectangle? (c/s/r): c Enter radius: 15 Area: 706.858 Continue? (y/n): y circle, square, or rectangle? (c/s/r):r Enter width 15 Enter height: 20 Area: 300 continue? (y/n): n Bye!

Specifications

  • Create an abstract class named Shape. This class should contain virtual member function named get_area() that returns a double type.
  • Create a class named Circle that inherits the Shape class and contains these constructors and member functions:
    • Circle(double radius)
    • double get_radius()
    • void set_radius(double radius)
    • double get_area()
  • Create a class named Square that inherits the Shape class and contains these constructors and member functions:
    • Square(double width)
    • double get_width()
    • void set_width(double width)
    • double get_area()
  • Create a class named Rectangle that inherits the Square class and contains these constructors and member functions:
    • Rectangle(double width, double height)
    • double get_height()
    • void set_height(double height)
    • double get_area()
  • Use the following formulas to calculate area:
    • Circle: Area = radius2 * pi
    • Square: Area = width2
    • Rectangle: Area = width * height
  • In the main.cpp class, use a function named display_area() to display the area of each shape.
  • Use the console namespace presented in chapter 7, p.274~277, to get and validate the user’s entries.
1 0
Add a comment Improve this question Transcribed image text
Answer #1

#include<iostream>
using namespace std;
//abstract class
class Shape
{
  
   virtual double get_area()=0;//returns double type
};

class Circle:Shape
{
  
   double radius;
   //constructor
   public:
   Circle(double r)
   {
       radius=r;
   }
   double get_radius()
   {
       return radius;
   }
   void set_radius(double r)
   {
           radius=r;
   }
  
   double get_area()
   {
       return 3.14*radius*radius;//returning area  
   }
};

class Square:Shape
{
   double width;
   //constructor
   public:
   Square(double w)
   {
       width=w;
   }
   double get_width()
   {
       return width;
   }
   void set_width(double w)
   {
           width=w;
   }
  
   double get_area()
   {
       return width*width;//returning area  
   }      
  
};

class Rectangle:Shape
{
   double width,height;
   //constructor
   public:
   Rectangle(double w,double h)
   {
       width=w;
       height =h;
   }
   double get_width()
   {
       return width;
   }
   void set_width(double w)
   {
           width=w;
   }
   double get_height()
   {
       return height;
   }
   void set_height(double h)
   {
           height=h;
   }
  
   double get_area()
   {
       return width*height;//returning area  
   }      
  
};
//method to display area of each shape
void display_area()
{
   double r;
   double w,h;
  
   cout<<"Enter circle radius :";
   cin>>r;
   Circle *c= new Circle(r);
   cout<<"Area of circle :"<<c->get_area()<<endl;
  
   cout<<"Enter Square width :";
   cin>>w;
   Square *s= new Square(w);
   cout<<"Area of Square :"<<s->get_area()<<endl;
  
   cout<<"Enter Rectangel width :";
   cin>>w;
   cout<<"Enter Rectangel height :";
   cin>>h;
   Rectangle *R= new Rectangle(w,h);
   cout<<"Area of Rectangle :"<<R->get_area()<<endl;
  
  
  
  
  
}
int main()
{
  
   display_area();
   return 0;
}


//PLS give a thumbs up if you find this helpful, its helps me alot, thanks.

Add a comment
Know the answer?
Add Answer to:
Create a program that calculates the area of various shapes. Console Specifications Create an abstract class...
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
  • Define an interface named Shape with a single method named area that calculates the area of...

    Define an interface named Shape with a single method named area that calculates the area of the geometric shape:        public double area(); Next, define a class named Circle that implements Shape. The Circle class should have an instance variable for the radius, a constructor that sets the radius, an accessor and a mutator method for the radius, and an implementation of the area method. Define another class named Rectangle that implements Shape. The Rectangle class should have instance variables...

  • java language Problem 3: Shape (10 points) (Software Design) Create an abstract class that represents a Shape and contains abstract method: area and perimeter. Create concrete classes: Circle, Rectang...

    java language Problem 3: Shape (10 points) (Software Design) Create an abstract class that represents a Shape and contains abstract method: area and perimeter. Create concrete classes: Circle, Rectangle, Triangle which extend the Shape class and implements the abstract methods. A circle has a radius, a rectangle has width and height, and a triangle has three sides. Software Architecture: The Shape class is the abstract super class and must be instantiated by one of its concrete subclasses: Circle, Rectangle, or...

  • Pure Abstract Base Class Project. Define a class called BasicShape which will be a pure abstract class. The clas...

    Pure Abstract Base Class Project. Define a class called BasicShape which will be a pure abstract class. The class will have one protected data member that will be a double called area. It will provide a function called getArea which should return the value of the data member area. It will also provide a function called calcArea which must be a pure virtual function. Define a class called Circle. It should be a derived class of the BasicShape class. This...

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

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

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

  • Area Program – Build a Java Class to Calculate the area of a Figure(call it “Area.java”....

    Area Program – Build a Java Class to Calculate the area of a Figure(call it “Area.java”. The figure may be a circle, or a square or a rectangle. The user will enter the type of figure they want through a Scanner. If they enter a “C”, proceed to calculate the area of the Circle using values read in for radius. If they enter an “S”, then calculate the area of a Square using values read in for side. If they...

  • Q2) Interface Create a program that calculates the perimeter and the area of any given 2D...

    Q2) Interface Create a program that calculates the perimeter and the area of any given 2D shape. The program should dynamically assign the appropriate calculation for the given shape. The type of shapes are the following: • Quadrilateral 0 Square . Perimeter: 4xL • Area:LXL O Rectangle • Perimeter: 2(L+W) • Area:LxW Circle Circumference: I x Diameter (TT = 3.14) Area: (TT xD')/4 Triangle (assume right triangle) o Perimeter: a+b+c O Area: 0.5 x base x height (hint: the base...

  • create a program shape.cpp that uses classes point.h and shape.h. The program should compile using the...

    create a program shape.cpp that uses classes point.h and shape.h. The program should compile using the provided main program testShape.cpp. the provided programs should not be modified. Instructions ar egiven below. Shape class The Shape class is an abstract base class from which Rectangle and Circle are derived. bool fits_in(const Rectangle& r) is a pure virtual function that should return true if the Shape fits in the Rectangle r. void draw(void) is a pure virtual function that writes the svg...

  • I Need UML Design for the following Java code: Colorable interface: public interface Colorable { //a...

    I Need UML Design for the following Java code: Colorable interface: public interface Colorable { //a void method named howToColor(). void howToColor(); } GeometricObject class: public class GeometricObject { } Sqaure class: public class Square extends GeometricObject implements Colorable{ //side variable of Square double side;    //Implementing howToColor() @Override public void howToColor() { System.out.println("Color all four sides."); }    //setter and getter methods for Square public void setSide(double side) { this.side = side; }    public double getSide() { return...

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