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

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:



Shape



















Method



virtual float area()



virtual float perimeter()



toName()




Circle, Rectangle, Triangle Classes



















Method



float area()



float perimeter()



string toName()




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 Diagonals



Using Base and Height



Using Trigonometry



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

May 18, 2022
SOLUTION.PDF

Get Answer To This Question

Submit New Assignment

Copy and Paste Your Assignment Here