Consider a graphics system that has classes for various figures - rectangles, circles, squares, triangles, and so on. For example, a rectangle might have data members for height, width; a circle might have a radius, while a square and triangle might have only an edge length or three sides of the triangle, respectively. In a well-designed system, these would derive from a common class, GeometricObject.

You are to implement such a system. Square is a rectangle. You will create a derived class (Square) from the Rectangle class.

Figure: see image.

Requirement

1. (The GeometricObject class) - Save two files individually: GeometricObject's specification file (header file) (GeometricObject.h) and implementation file (GeometricObject.cpp).

The based class has pure virtual functions for member functions getArea() and getPerimeter(). You will create all members and functions based on the above figure.

  • Two data fields.
  • A no-arg constructor that creates a default values of color: "white" and filled: false Two constructors with arguments
  • toString() method will return "Geometric Object"
  • pure virtual functions for getArea and getPerimeter
  • The mutators functions for all data fields.
  • The constant accessor functions for all data fields.

2. (The Rectangle class: is-a GeometricObject) - Based on the above figure to modify and create the Rectangle class.Save two files individually: Rectangle's specification file (header file) (Rectangle.h) and implementation file (Rectangle.cpp)

  • Two double data fields named: width and height
  • A no-arg constructor that creates a default rectangle with width 1.0 and height 1.0.
  • A constructor that creates a rectangle with the specified width and height.
  • A constructor with the specified width and height and also accept the color and filled variables (same as the above figure for the Rectangle class)
  • A destructor
  • The mutators functions for all data fields.
  • The constant accessor functions for all data fields.
  • A constant function named getArea() that returns the area. area = width * height
  • A constant function named getPerimeter() that returns the perimeter Perimeter = 2*(width+ height)
  • Exception handling for negative numbers of width and height

3. (The Circle class: is-a GeometricObject) - Based on the above figure to modify and create the Circle class. Save two files individually: Circle's specification file (header file) (Circle.h) and implementation file (Circle.cpp)

  • One double data field named: radius
  • A no-arg constructor that creates a default circle with radius 1.0.
  • A constructor that creates a rectangle with the specified radius.
  • A constructor with the specified radius and also accept the color and filled variables (same as the above figure for the Circle class)
  • A destructor
  • The mutators functions for all data field.
  • The constant accessor functions for all data field.
  • A constant function named getArea() that returns the area. area = PI * 2
  • A constant function named getPerimeter() that returns the perimeter Perimeter =2* PI * r
  • Exception handling for negative number of radius

4. (The Triangle class: is-a GeometricObject) Design a class named Triangle that is derived from the GeometricObject. Save two files individually: Triangle's specification file (header file) (Triangle.h) and implementation file (Triangle.cpp)

The class is based on the following figure: see image.

  • Three double data fields named side1, side2, and side3 to denote three sides of the triangle.
  • A no-arg constructor that creates a default triangle with each side 1.0.
  • A constructor that creates a triangle with the specified side1, side2, and side3.
  • A constructor that creates three sides and also accept the color and filled variables (same as the above figure for the Circle class)
  • A destructor
  • The mutators functions for all three data fields.
  • The constant accessor functions for all three data fields.
  • A constant function named getArea() that returns the area of this triangle.
    • s = (side1 + side2 + side3)/2;
    • area = sqrt(s* (s - side1) * (s - side2) * (s - side3));
  • A constant function named getPerimeter() that returns the perimeter of this triangle. Perimeter = side1+side2+side3
  • Exception handling for negative numbers of side1, side2, side3

5. (The Square class: is-a Rectangle) Design a class named Square that is derived from the Rectangle.

Save two files individually: Square's specification file (header file) (Square.h) and implementation file (Square.cpp)

The class contains the following:

  • One double data fields named length to denote each sides of the square.
  • A no-arg constructor that creates a default square with length 1.0.
  • A constructor that creates a square with the specified length.
  • A constructor that also accept the length, color and filled variables (same as the above figure for the Rectangle class)
  • A destructor
  • The mutators function for one data field.
  • The constant accessor function for one data field.
  • A constant function named getArea() that returns the area of this square. Area = length *length
  • A constant function named getPerimeter() that returns the perimeter of this triangle. Perimeter = 4* length
  • Exception handling for negative number of length

After you have created these classes, create a driver program that demonstrates by asking the user to enter the information for a Circle object, a Square object, a Triangle object and a Rectangle object. Demonstrate that each object properly calculates and reports its area and perimeter.

The program that prompts the user to enter the choice from the menu. If the user chooses 1 for the Circle, then the program will also prompt for radius, color, and enter 1 or 0 to indicate whether the color is filled or not. The program should display the result with radius, diameter, area, and perimeter, color, true or false to indicate whether filled or not. All information will display with two decimal places. The program will be continuing to asking the user to quit or not. The program will continue run as long as until the user enters "5" for quitting. If not, it will be displayed the menu for the choice.

The program should include "Input Validation: Decide how the program should handle any illegal inputs". Display an error message if the user enters a number outside the range of 1 through 5 when selecting an item from the menu.

Exception handling for negative values of the circle's radius, the rectangle's width or height, the triangle's side, or any shape of geometric object.

The program should divide the program onto the different functions.

For example,

showMenu() will display the menu for the user to choose.

//Display the menu

void showMenu(); //see the below figure

1. Circle
2. Rectangle
3. Square
4. Triangle
5. Quit the program
Enter your choice: 1

A function for displaying a geometric object for polymorph:

void displayGeometricObject(const GeometricObject& g)

This function will perform polymorph for creating individual object and pass reference variable. This function is responsible for displaying all the information about the chosen GeometricObject on the console. It does polymorphically.

Hint: For the definition of the function:

GeometricObject* p = &g;
Circle* p1 = dynamic_cast< Circle* >(p);
cout << "The radius is << p1->getRadius() << endl;

The program's result is similar to the following figure: see image.

Academic Honesty!
It is not our intention to break the school's academic policy. Posted solutions are meant to be used as a reference and should not be submitted as is. We are not held liable for any misuse of the solutions. Please see the frequently asked questions page for further questions and inquiries.
Kindly complete the form. Please provide a valid email address and we will get back to you within 24 hours. Payment is through PayPal, Buy me a Coffee or Cryptocurrency. We are a nonprofit organization however we need funds to keep this organization operating and to be able to complete our research and development projects.