## Using Object-oriented Approach in Software Development

Software development projects using the object-oriented approach involve breaking the problem down into multiple classes that can be tied together into a single solution. In this project, you are given the task of writing some classes that would work together for providing a solution to a problem involving some basic computations.

##### Learning Objectives

The focus of this assignment is on the following learning objectives:

• Be able to identify the contents of class declaration header and class definition files and to segregate class functionality based on class description

• Be able to write object creation and initialization code in the form of constructors

• Be able to implement an encapsulated approach while dealing with data members for the classes involved through accessors and mutators

• Be able to make the program code spread across multiple files work together for the solution to the computing problem

##### Prerequisites

To complete this project, you need to make sure that you have the following:

• C++ and the g++ compiler

• A C++ IDE or text editor (multiple editors exist for developers)

• An understanding of the material presented in class.

• An understanding of the material covered in zyBooks.

##### Problem Description

You are to implement a program for creating a menu-driven program for reporting important parameters of some two-dimensional geometric shapes. For this project, you will need to utilize the concepts of classes and top-down design that has been discussed in class. The list of requirements and constraints for the system are as follows:

1. At the heart of the solution will be a test program file shapestest.cpp (using all the classes specified below) that will test the full functionality of the classes required as part of this project. The shapes that the program would be considering will be a circle, a triangle, and a rectangle.

The program will call all functionality for each shape and print out the resultant object state to allow for visual inspection that the class is implemented correctly. For example, the test class

will check that a Circle should be properly created when invoking the default constructor,

then its area, diameter, and circumference printed to the screen for verification that it has the correct state, and those respective methods work correctly.

After this, output of the state should be repeated after calling each mutator to ensure that the object’s new state reflects the change (note, there is only one mutator for the circle).

2. A circle is a closed shape in which every point of the enclosure is equidistant from a central point. This common distance between the center and the periphery is known as its radius. Important mathematical parameters associated with a circle can be calculated using the radius of the circle, as follows.

Circumference of the circle is the linear distance along the circle boundary and is given by (2 x π x radius) units.

Diameter of the circle is the length of a line passing through the center that has its end points on the boundary of the circle and is given by (2 x radius) units.

Area of the interior of the circle can be calculated using the formula (π x radius x radius) square units.

Create a Circle class (using files circle.cpp and circle.hpp files) with

● private data member for basic parameter (double): ‘radius’

● a default constructor which has no parameters

● a parametrized, overloaded constructor, which takes in a single double parameter for ‘radius’

● accessor and mutator for ‘radius’ named getRadius(…) and setRadius(…)

● member functions to return double values for calculated parameters: area( ), diameter( ) and circumference( )

● PI should be assumed to be exactly 3.141592. You may define this as a static const double at the class scope, as a const double inside any method that uses it, or as a literal in the expression in which it is used.

a member function that accepts a Rectangle (see below) and returns true if the area of the rectangle is the same as the area of this circle. It returns false otherwise.

3. A triangle is a closed shape bound within three lines. For an acute angled triangle, the base and the height of the triangle are the important parameters, as seen in the following figure.

Area inside the triangle can be calculated using the formula (1/2 x base x height) square units.

Create a Triangle class (using triangle.cpp and triangle.hpp files) with

● private data members for type double basic parameters: ‘base’, ‘height’

● a default constructor with no parameters

● a parametrized, overloaded constructor, which takes in a double parameter for ‘base’ followed by a double parameter for ‘height’

● accessors and mutators for ‘base’ named getBase(…) and setBase(…), and ‘height’ named getHeight(…) and setHeight(…)

● member function to return a double for calculated parameter: area( )

● a member function that accepts a Circle and returns true if the area of the circle is the same as the area of this triangle. It returns false otherwise.

4. A rectangle is a closed shape bounded by parallel sides that meet at right angles, with the other condition that opposite sides are parallel and equal, but all sides are not equal. The longer side in a rectangle is known as its length and the shorter side is known as width. Important mathematical parameters associated with a e rectangle can be calculated using the length and width values of the rectangle, as follows.

Perimeter of the rectangle is the linear distance along the rectangle boundary and is given by

[2 x (length + width)] units.

Diagonal of the rectangle is the length of a line joining opposite corners and is given by √(length2 + width2) units.

Area of the rectangle can be calculated using the formula (length x width) square units.

Create a Rectangle class (using rectangle.cpp and rectangle.hpp files) with

● private data members for basic parameters (double): ‘length’, ‘width’

● a default constructor which has no parameters which takes in a double parameter for ‘length’ followed by a double parameter for ‘width’

● a parametrized, overloaded constructor,

● accessors and mutators for ‘length’, named getLength(…) and setLength(…), and ‘width’ named getWidth(…) and setWidth(…)

● member functions to return double values for calculated parameters: area( ), diagonal( ) and perimeter( )

● member function to return a bool for the state of the rectangle being square (its length is equal to its width) named isSquare(…)

● member function that accepts a Triangle and returns true if the area of the triangle is the same as the area of this rectangle. It returns false otherwise.

5. Default constructors for all shapes should initialize the private data members to a value of unity (i.e. 1.0).

6. Integrate the circle, rectangle, and triangle classes to the shapes code to make them available inside the shapes program. The menu in the shapes program should ask the user to choose one of the three figures for which parameters are to be calculated, along with another option to quit the program. If the user chooses one of the shapes, the program should ask the user if the user wishes to enter the values of the basic parameters for calculation. If not, the basic parameters are all fixed at unity values (1.0) for all basic parameter/s by default for the shape chosen by the user. The program then creates an object of the user-specified type and calls relevant functions for the object to find out and report the calculated parameter/s of the user-specified shape. The code keeps looping back to the menu until the user chooses the option to quit.

##### Development Tasks

For this project, you must complete the following tasks in the order that they are described:

1. From the problem description, create a list of all classes that you can identify. For each class, list the associated member variables and identify an initial set of member functions.

2. List out a set of steps that you will take to implement your solution to the problem. Each

step refers to an increment of the program that you will be creating. It is recommended to complete the implementation of a single logical action per step.

3. Keep track of your work as you implement the program, logging the tasks you completed, and issues encountered along the way. For example - Friday 02/26: completed class X, encountered some issues implementing Y feature

4. Once you have finished implementing your solution, reflect on the process that you followed. Did you wind up with the same classes as you initially identified? Did you need to change any of the functionality or add unexpected details? Did you have to deviate from your plan? Write a description of any details that needed to change as you worked on your solution.

Your responses to these tasks should be submitted as a PDF document called lastname_firstname_ project02.pdf.

##### Classes

The classes identified in this problem are:

a) Circle

• radius: double

The member functions are:

• getRadius(): double – Returns the radius of the circle

• setRadius(double): void – Changes the circle’s radius

• area(): double – Returns the area of the circle

• diameter(): double – Returns the diameter of the circle

• circumference(): double – Returns the circumference of the circle

• compareWithRectangle(Rectangle): bool – Returns true if the area of the circle is equal to the area of the rectangle passed as argument

b) Rectangle

• length: double

• width: double

The member functions are:

• getLength(): double – Returns the length of the rectangle

• getWidth(): double – Returns the width of the rectangle

• setLength(double): void – Changes the length of the rectangle

• setWidth(double): void – Changes the width of the rectangle

• area(): double – Returns the area of the rectangle

• diagonal(): double – Returns the length of the diagonal of the rectangle

• perimeter(): double – Returns the perimeter of the rectangle

• isSquare(): bool – Returns true if the length of the rectangle is equal to its width

• comparWithTriangle(Triangle): bool – Returns true of the area of the rectangle is equal to the area of the triangle passed as argument.

c) Triangle

• Base: double

• Height: double

The member functions are:

• getBase(): double – Returns the base of the triangle

• getHeight(): double – Returns the height of the triangle

• setBase(double): void – Changes the base of the triangle

• setHeight(double): void – Changes the height of the triangle

• area(): double – Returns the area of the triangle

• compareWithCircle(Circle): bool – Returns true of the area of the triangle is equal to the area of the circle passed as argument.

##### Steps to complete program

The following steps are specified in order to complete the program

a. Create the class Circle

b. Create the class Triangle

c. Create the class Rectangle

d. Create the main class which is used to test all other classes

e. Define empty variables to hold inputs from user. There variables are used to hold information about all shapes, so we need to create variables for radius, base, width, length and height.

f. Create empty Circle, Triangle and Rectangle objects. These objects will later be edited (using member functions) if the user wants to specify its parameters.

g. Create a loop that will display menu to user and keep asking for a menu option until user selects “Exit” option. This is done using a while-loop

h. Test all classes.

##### Software Development Problems Encountered

Monday 02/03/2021: I have found a problem known as "Circular Dependency", which refers to the dependence of one class on another, which at the same time depends on the first. That is, it had a circular dependency as follows:

The Circle class includes the Rectangle class, which includes the Triangle class, which includes the Circle class.

This problem was solved after investigation, and by using the method known as "Forward Declaration", I was able to fix this problem.

2. Once you have finished implementing your solution, reflect on the process that you followed. Did you wind up with the same classes as you initially identified? Did you need to change any of the functionality or add unexpected details? Did you have to deviate from your plan? Write a description of any details that needed to change as you worked on your solution.

As I mentioned in the previous part, I had to resort to "Forward Declaration". In my case, I had to modify the Triangle class to include a forward declaration of the Circle class, and in the compareWithCircle method, define that the Circle argument is of type pointer.