Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Example: #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 transcribedimage text in transcribedimage text in transcribedimage text in transcribed

Example:

#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

Do It In C++ Language

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 (x1, yl). h = y2-yl area = w x h w = x2-xl (x2, y2) Bottom right Figure 2.1: Formula to obtain the width (w), height (h) and area of a rectangle. area = ner2 Figure 2.2: Formula to obtain the area of a circle with radius r. Note that, use 7=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 corners. 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 corners, 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() +getArea(): virtual double tread(): virtual void print(): virtual void Circle Rectangle -radius: double Point top Left -get Width(): double -getHeight(): double center +Circle tgetArea(): double +read 0 : void print(): void -x : double -y: double +Point() tset(x:double, y:double): void +getX(): double +getY(): double +Rectangle() +getArea(): double +read(): void bottomRight +print(): void 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 1: The user chooses option 1 to add a circle. ========== [MENU) ============ 1. Add a shape 2. Print all shapes 3. Calculate total area 4. Exit Enter your choice => 1 What type of shape you want to enter? 1. circle 2. Rectangle Your choice => 1 Enter the center coordinates of the circle (x y) => O O Enter the circle's radius=> 10 Interaction 2: The user chooses option 1 to add a rectangle. (MENU) 1. Add a shape 2. Print all shapes 3. Calculate total area 4. Exit Enter your choice => 1 What type of shape you want to enter? 1. Circle 2. Rectangle Your choice => 2 1 Enter the top left corner coordinates of the rectangle (x y) => 20 30 Enter the bottom right corner coordinates of the rectangle (* y) => 30 20 7 Interaction 3: The user chooses option 1 to add another circle. ==========[MENU] ====== 1. Add a shape 2. Print all shapes 3. Calculate total area 4. Exit Enter your choice => 1 What type of shape you want to enter? 1. Circle 2. Rectangle Your choice -> 1 Enter the center coordinates of the circle (x y)-> 50 50 Enter the circle's radius-> 50 Interaction 4: The user chooses option 2 to print the information of all the shapes that he/she has entered. [MENU) 1. Add a shape 2. Print all shapes 3. Calculate total area 4. Exit Enter your choice => 2 Shape #1 Circle's center: x=0 YO Circle's radius 10 Circle's area =314.15 Shape #2 Rectangle's top left corner: X-20 Y=30 Rectangle's bottom right corner: X=30 Y=20 Rectangle's width - 10 Rectangle's height - 10 Rectangle's area 100 Shape #3 Circle's center: X-50 Y-50 Circle's radius -50 Circle's area 7853.75 Interaction 5: The user chooses option 3 to obtain the total area of all the shapes. =(MENU) 1. Add a shape 2. Print all shapes 3. Calculate total area 4. Exit Enter your choice => 3 Total Area= 8267.9 Interaction 6: The user chooses option 4 to end the program. (MENU) 1. Add a shape 2. Print all shapes 3. Calculate total area 4. Exit Enter your choice => 4 Figure 2.4: An example run 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

Transactions On Large Scale Data And Knowledge Centered Systems X Special Issue On Database And Expert Systems Applications Lncs 8220

Authors: Abdelkader Hameurlain ,Josef Kung ,Roland Wagner ,Stephen W. Liddle ,Klaus-Dieter Schewe ,Xiaofang Zhou

2013th Edition

3642412203, 978-3642412202

More Books

Students also viewed these Databases questions

Question

Design a training session to maximize learning. page 296

Answered: 1 week ago

Question

Design a cross-cultural preparation program. page 300

Answered: 1 week ago