Question
C++ QUESTIONS PLEASE HELP ME ANSWER THIS ASAP :(( !! ( I ONLY HAVE THREE HOURS ) THIS IS THE CODE GIVEN : - ////////////////////////////////////////////////////////////////////////////////
C++ QUESTIONS PLEASE HELP ME ANSWER THIS ASAP :(( !! ( I ONLY HAVE THREE HOURS )
THIS IS THE CODE GIVEN : -
//////////////////////////////////////////////////////////////////////////////// // Faculty of Computing, Universiti Teknologi Malaysia // SCSJ1023- Programming Technique II // Semester 2, 2017/2018 // Final Exam, Paper 2 (Practical), Question 1 // Template file // // program1.cpp ////////////////////////////////////////////////////////////////////////////////
#include
using namespace std;
// Task 1: class Polygon class Polygon { protected: int w, h; public: Polygon(int w=0, int h=0){ this->w = w; this->h = h; } // Define the method display //____________________________
// Define the method calcArea //____________________________ };
// Task 2: class Rectangle
// Specify the class as a child of class Polygon
class Rectangle { public: // Complete the definition of the constructor Rectangle(int width=0, int length=0) { } // Complete the definition of the method display void display() const { cout
// Task 3: class Triangle
// Specify the class as a child of class Polygon
class Triangle { public:
// Complete the definition of the constructor Triangle(int base=0, int height=0) { }
// Complete the definition of the method display void display() const { cout
// Complete the definition of the method calcArea int calcArea() const { } };
int main() { // Task 4: An array of pointers to polygons. const int SIZE = 4; // The size of the array // Declare the array and fill in it with the polygons. //____________________________
// Task 5: Display the polygons and calculate the total area. cout
return 0; }
Question Rectangles and triangles are two common types of polygons. They can be represented by their dimensions, w and h. As for a rectangle, w represents the width and h is the length of the rectangle. Whereas for a triangle, w and h are the base and height of the triangle, respectively (Figure 1). length=h height=h width=w base=w (a) Rectangle (b) Triangle Figure 1: Representation of a rectangle and triangle. Based on the above case, answer the following questions: 1. Explain three ways to model a rectangle and the advantages as well disadvantages of each approach. 2. Can a rectangle and a triangle be generalized? Explain your answer. 3. How does the concept of specialization work on the above case? 4. Finally, write a program for the above case. Here, you are given an incomplete program source code, main.cpp which contains a base or parent class named Polygon and two derived or child classes named Rectangle and Triangle, respectively. Complete the program based on the tasks 1 to 5 below. Task 1: In class Polygon, define two member functions or methods and allow them to be polymorphic. The methods and their purpose are as follows: a) display: to display a message "Generic polygon" onto the screen. b) calcArea: to return a value of zero. Task 2: Declare the class Rectangle as a child of the class Polygon and complete the definitions for the following methods. a) the constructor: to initialize the width and length of the rectangle. b) display: to display the width and length of the rectangle. c) calcArea: to calculate the area of the rectangle. Formula: area = width * length. Task 3: Declare the class Triangle as another child of the class Polygon and complete the definitions for the following methods: a) the constructor: to initialize the base and height of the triangle. b) display: to display the base and height of the triangle. c) calcArea : to calculate the area of the triangle. Formula: area = 1 x base x 2 height Task 4: Declare an array of pointers to polygons and fill in the array with different types of polygons that are allocated dynamically. The polygons are: a triangle with the base is 10 and height is 20, a rectangle with the width is 20 and length is 20, a generic polygon, and another rectangle with the width and length are 15 and 10, respectively. Task 5: Display the information of all polygons along with their area and calculate the total area of the polygons. The program should produce screen output as shown in Figure 2. Polygon #1 Triangle: Base = 10, Height = 20 Area = 100 Polygon #2 Rectangle: Width = 20, Length = 20 Area = 400 Polygon #3 Generic polygon: Area = 0 Polygon #4 Rectangle: Width = 15, Length = 10 Area - 150 The total area of all polygons = 650 Figure 2: Program outputStep by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started