For this project, you will implement a Geometric Shapes area and perimeter calculator, using the object-oriented design, diagramed below. The following class diagram shows class attributes (members)...

1 answer below »

For this project, you will implement a Geometric Shapes area and perimeter calculator, using the object-oriented design, diagramed below.



  • The following class diagram shows class attributes (members) and methods (operations). Operations are denoted with a plus. The dotted line with an open diamond indicates a realization, indicating that an abstract class is implemented (realized) by a concrete class type. Realization is a type of inheritance. The functions are indicated as virtual and set to 0, indicating the derived class must implement these functions.

  • All class attributes are private unless denoted otherwise, and if access is needed outside the class must be accessed with getter, setter methods, or constructors. Private attributes if access is needed outside the class, assume get methods accessors.

  • Virtual methods are abstract and must be implemented by the derived class. Classes that only contain virtual methods are called pure virtual classes in C++, and these classes are abstract by definition and cannot be instantiated. This type of class in C++ is functionally equivalent to an interface in Java.

  • If you think about it, it makes sense that we call the Shape class abstract, since you cannot measure or visualize a Shape unless you know its type. But you can measure a
    type
    of Shapes, such as a Rectangle, Circle, or Triangle, but an unspecified Shape is a concept, that is a generalization. An unspecified Shape is an abstraction, and you can measure the area of a Shape only if you know what kind it is. The following diagram models this exacts situation:



Shapes



Shape























Method




Function



virtual float area()



declaration of virtual function to calculate and return area of shape (not implemented).


virtual float perimeter()

declaration of virtual function to calculate and return perimeterof shape (not implemented).



toName()



virtual function to return name of Class.







Circle, Rectangle, Triangle Classes























Method




Function



float area()


Calculates and returns area for Shape

float perimeter()



Calculates and returns perimeter for Shape



string toName()



Returns className for the derived class. Returns "Circle", "Rectangle", "Triangle", etc.





Implementation Instructions:



  • Create a new Project in CLion called Shapes. Modify the existing class or add a new CPP class to the project. The name of your class file must match the cpp file name in your project make file (CMakeLists.txt).

  • Add necessary headers files to the top of your main class file:

  • #include
    #include
    #include
    #include
    #include// std::setprecision - allows printing decimals to given precision

  • Create a pure virtual (abstract) class called Shape as shown in the class diagram. This class contains the three methods defined in Shape. Virtual methods are just declarations in the pure virtual class, that is no body is defined for these functions in the Shape class.

  • Create three classes for Circle, Rectangle, and Triangle and implement the area(), perimeter() and toName() methods() for these classes. The implementations of these methods will be different depending on the class:

    • Rectangle:

      • Area of a rectangle is:width*length;

      • Perimeter of rectangle is:2* (width+length);

      • toName() returns:return "Rectangle";



    • Circle: ( Requires header at top of file:#include)

      • area() :returnM_PI* (radius*radius);

      • perimeter:return2*M_PI*radius;

      • toName() returns:return "Circle";



    • Triangle: (For this exercise we define 3 sides instead of just base and height):




        • // Use Heron's formula to calculate area:

          // A = SquareRoot(s * (s - a) * (s - b) * (s - c))
          // where s = (a + b + c) / 2, or 1/2 of the perimeter of the triangle
          float area() {
          float s = (a + b + c) / 2;




          return sqrt(s * (s - a) * (s - b) * (s - c));



          }



        • perimeter:return a + b + c;

        • toName() returns:
          return "Triangle";



      • Create another Shape class of your choice and implement area and perimeter methods for your new class as above. For example, you could choose Rhombus (a rhombus has equivalent side lengths):

      • Area of a Rombus:




















        • Various Formulas to Calculate Area of Rhombus
          Using DiagonalsA = ½ × d1× d2
          Using Base and HeightA = b × h
          Using TrigonometryA = b2× Sin(a)


        • Perimeter: P = 4a









  • Implement constructors for each of your shapes as follows:

  • // constructor
    Rectangle(floatw,floatl) {
    width= w;
    length= l;
    }

  • // constructor
    Triangle(floata1,floatb1,floatc1) {
    a= a1
    b= b1;
    c= c1;
    }

  • // constructor
    Circle(intr) {
    radius= r;
    }

  • Add a main method to your class or modify the existing one. The main method will instantiate a Rectangle, Triangle, and Circle, plus a fourth Shape of your choice.

  • In your main function, declare a vector of type Shape* pointer. Then add the address of each Shape object that you have created to the vector.

  • Loop through the vector elements and print out the className, area, and perimeter of your Shape objects. Area and perimeter value should be formatted to two decimal points.

  • Your main class and print (write) method (write) is provided for you below:

  • voidwrite(Shape* s) {
    // fixed

    couttoName()area()
    perimeter()}
    intmain(void) {
    // Choose any size elements
    Rectanglerect(5.5,7.3);
    Triangletri(4.3,4,3);
    Circlecir(5.3);
    // Vector is like an ArrayList to hold our shapes
    vectorshapes;// vector of pointers to shapes
    // Adds the shapes to the vector, using address reference of shape (review lab on references)
    shapes.push_back(&rect);
    shapes.push_back(&cir);
    shapes.push_back(&tri);
    // iterator - print contents
    for_each(shapes.begin(),shapes.end(), write);
    return0;
    }


  • If you are confused at this point, you can, if you must,
    download a partially implemented starter file


    Preview the document
    and fill in the missing code where indicated. You can create a new project and copy the starter file and replace the contents of the default main.cpp that CLion gives you.

  • Test and debug your project.

Answered Same DayNov 09, 2021

Answer To: For this project, you will implement a Geometric Shapes area and perimeter calculator, using the...

Arun Shankar answered on Nov 16 2021
139 Votes
#include
#include
#include
#include
#include
#
include
using namespace std;
class Shape
{
public:
Shape(){}
virtual float area() = 0;
virtual float perimeter() = 0;
virtual string toName() = 0;
virtual ~Shape(){};
};
class Triangle: public Shape
{
private:
double sideA;
double sideB;
double sideC;
public:
Triangle(double a, double b, double c)
{
sideA = a;
sideB = b;
sideC = c;
}
float area(){
double s = (sideA + sideB + sideC) / 2.0;
return sqrt(s * (s - sideA) * (s -...
SOLUTION.PDF

Answer To This Question Is Available To Download

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here