Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

CODE #include using namespace std; #define PI 3.1415 // Task 1 // Declare class Point with the required attributes // and define the member functions

image text in transcribedimage text in transcribedimage text in transcribedimage text in transcribed

CODE

#include using namespace std;

#define PI 3.1415

// Task 1 // Declare class Point with the required attributes // and define the member functions

// Task 2 // Declare class Shape with the required attributes // and define the member functions // define getArea() as pure virtual function // define read() and print() as virtual function

// Task 3 // Declare class Circle that inherit Shape class // Define the required attributes and member functions

// Task 4 // Declare class Rectangle that inherit Shape class // Define the required attributes and member functions

// Task 5 // Complete the following int menu() function that will display this menu // and get the user choice

//==========[MENU]============ // 1. Add a shape // 2. Print all shapes // 3. Calculate total area // 4. Exit // // Enter your choice => 1

int menu() { int choice; // Please complete return choice; }

// Given main() program int main() { // to count the number of shapes instance int shapeCount=0; // declarations of pointer array named shapes // that able to store address for various types of shape Shape *shapes[20]; // get user choice from the menu int command = menu(); int shapeType; Shape *newShape; while (command!=4){ switch (command){ case 1: cout "; cin >> shapeType; // user choice from the menu cout read(); // polymorphism shapes[shapeCount]=newShape; // 'using array to store various shape shapeCount++; // count number of shapes in the array

break; case 2: for (int i=0; iprint(); // 1m cout

case 3: // Calculating the total area of all shapes in the array for (int i=0; igetArea(); // 1m cout Question: Rectangles and circles are most common geometrical shapes. A rectangle can be defined by two points, i.e., the top left and bottom right vertices. Whereas, a circle is defined by its center point and radius. Further, the area of a rectangle is obtained from its width and height, and the area of a circle is based only on its radius. Figure 2.1 and Figure 2.2 show how to determine the area of a rectangle and a circle, respectively. Top left (x,y) h = y2-yi area wh w x2-xl (x2, y2) Bottom right Figure 2.1: Formula to obtain the width (w), height (h) and area of a rectangle. area = ar Figure 2.2: Formula to obtain the area of a circle with radius r. Note that, ise R=3.1415. Given a class diagram consisting of three main classes, Shape, Circle and Rectangle, and their relationship, in Figure 2.3. Below are some important notes regarding the classes: The methods getArea() in class Rectangle and Circle are meant to calculate the area of the circle and rectangle, respectively. The formula to calculate the area are given in Figure 2.1 and 2.2. . The methods read() in the classes will be used to read the data of the shape from the keyboard. As for the class Rectangle the data to read will be the coordinates of the top left and bottom right comers. Whereas, for the circle, the data to read are the coordinates of the center and the radius. . The methods print) in the classes are meant to print shape's data onto the screen. As for the class Rectangle this method will be printing the top left and bottom right comers, the width and height, as well as the area of the rectangle. Whereas, for the circle the data to print are the coordinates of the center and the radius, as well as the area of the circle. Shape Shape gerai double read: vita void ri: virtual Point Circle radius: double Circle Aradable read : void print:void double Point double. y.double):void XO): double YO) double Rectangle -W): double plan geheight duble Rectangle double road) void bottomligheid Figure 2.3: The class diagram You are given a C++ program with 5 tasks to be completed and main() program to implement all the classes given in Figure 2.3. Your implementation needs to apply several object-oriented programming concepts including compositions, inheritances and polymorphisms The main () program given use the classes to create a program that calculates areas of a list of shapes consisting of rectangles and circles. To show the polymorphism concept, the program use only a single array to hold the shapes. Also, the program needs to provide the user a menu-driven interaction with the following options: Menu Options 1. Add a shape 2. Print all shapes 3. Calculate total area 4. Exit Description To insert a new shape (a circle or rectangle) into the array To print the information of all the shapes To calculate and print the total area of all the shapes To end the program Figure 2.4 shows the expected result of your program. Note that, all the interactions shown in the figure are continuous in a single run. Note also that, the bold texts indicate input entered by the user. Interaction : The user chooses option to add a circle 2. Print all shapes 3. Calculate to a inter your choice - 1 what type of shape you want to enter? 1. Cicle 2. Rectangle Your choice => 1 Enter the center codinates of the site OO the late 10 Interaction 2: The schooses option to add a rectangle 2. Pin all shapes Calculate total are ter your choice - 1 what type of shape you want to enter Enter the top left corner ordinates of the rectangley - 20 30 to the bottom right one cardinates of the retangle - 30 20 Interaction 3: The user chooses option I wadd another circle --- 2. Pelle Calculate total se was type ar 1. Cute Enter the center codes of the selen 50 50 aces the distes a 50 Interaction : The wchoones option to promotion of all these har hashe has Pin all shapes 3. Calease cal Cele 300 Rectangle bottom Rectangle with = 10 Rectangle height - 20 Shape Cuscle' EXO Career Interaction : The war chooses prin) wamecres of all these 2. Pin all shapes 3. Calculate total Tee your home 3 Total A 1262 Interaction is the ver chooses aperien die and the program a. Print all the 3. Calculate total area ter your choice Figure 2.4: An example of the program

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Essential Data Protection For Estate Agencies In Singapore 2024

Authors: Yang Yen Thaw Yt

1st Edition

B0CQK79WD3, 979-8872095392

More Books

Students also viewed these Databases questions

Question

Briefly explain the various types of leadership ?

Answered: 1 week ago

Question

Explain the need for and importance of co-ordination?

Answered: 1 week ago

Question

Explain the contribution of Peter F. Drucker to Management .

Answered: 1 week ago

Question

What is meant by organisational theory ?

Answered: 1 week ago

Question

Design a job advertisement.

Answered: 1 week ago