Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

help me solve this in C++ Goals: Learn how to use the member functions of classes. Learn why a class should be split into two

help me solve this in C++

Goals:

Learn how to use the member functions of classes.

Learn why a class should be split into two parts: the header file (.h or ,hpp) and the source file (.cpp)

General requirement:

Complete the program and answer the three questions at the bottom of this file.

Zip the three files and submit the ZIP file.

Project requirement:

Use the program in the attached document Sample_Rectangle_Class.txt to complete this assignment. The questions in the file are just for our practice in class. You do NOT have to answer them for this homework.

Modify the attached Rectangle program based on the following requirements:

  1. There should be a constructor which takes two parameters, i.e. width and height.
  2. In main(), there should be an array of size 21. The content of each element in the array must be a pointer to a Rectangle. (You can declare SIZE as a global const, since it will be used in Display().) The array must be initialized to nullptr, NULL or 0, depending on which version of C++ which you are using.
  3. In main(), there should be a for loop which populate the content of the array with random int numbers in the range from 1 to 7. The widths and heights of the 21 Rectangle objects are all randomly generated. Each Rectangle has a label like Rx, where x is a 4-digit serial number. That is, the first Rectangle is R0001, the second R0002, the third R0003, and so on. No matter what the serial number is, it must be displayed in 4 digits with a prefixed character R.
  4. There should be a global function named Display(), which takes only one parameter, i.e. the name of the array, and display three types of Rectangles, i.e. squares, horizontal rectangles and horizontal rectangles. At the end of main(), the function Display() is called to display a report like the following:

Square

================

Label Width Height

R0003 5 5

R0005 2 2

============================

Total 2

Horizontal Rectangle

================

Label Width Height

R0001 7 5

R0010 2 1

..

============================

Total 13

Vertical Rectangle

================

Label Width Height

R0013 5 7

R0015 2 3

..

============================

Total 6

  1. Three static variables must be used to keep track of the counts of the three types of rectangles.
  2. Finally the pointers must be deleted in main().
  3. The entire project should consists of three separated files, i.e. main.cpp, rectangle.h and rectangle.cpp files. Zip them together and submit the zip file. Do remember to add

#ifndef RECTANGLE_H

#define RECTANGLE_H

.

#endif

in the rectangle.h file.

  1. Answer the following three questions in the comment area on the top of the program.
  2. (5%) Why ifndef RECTANGLE_H, define RECTANGLE_H and endif have to be added to the .h file?
  3. (5%) Why the size of the array, i.e. 21, must be a const?
  4. (5%) Why we prefer to let the array hold pointers to Rectangle, instead of Rectangle objects?

here is a sample program

////// Rectangle.h

#include

#include

#include

using namespace std;

class Rectangle

{

private:

int serialNumber;

double width;

double height;

public:

Rectangle(double, double);

~Rectangle();

void setWidth(double);

void setHeight(double);

void setSerialNumber(int);

double getWidth() const;

double getHeight() const;

int getSerialNumber() const;

};

////// Rectangle.cpp

//#include "Rectangle.h"

Rectangle::Rectangle(double w, double h)

{

width= w;

height = h;

}

Rectangle::~Rectangle()

{

}

void Rectangle::setWidth(double w)

{

width = w;

}

void Rectangle::setHeight(double h)

{

height = h;

}

double Rectangle::getWidth() const

{

return width;

}

double Rectangle::getHeight() const

{

return height;

}

void Rectangle::setSerialNumber(int s)

{

serialNumber = s;

}

int Rectangle::getSerialNumber() const

{

return serialNumber;

}

////// main.cpp

/*#include

#include

#include

using namespace std;*/

//#include "Rectangle.h"

const int SIZE = 21;

void display(Rectangle *[]);

int main()

{

Rectangle * arrRect[SIZE];

for (int i=0; i < SIZE; i++)

{

arrRect[i] = new Rectangle(2.0, 4.0);

arrRect[i]->setSerialNumber(1000 + i);

}

display(arrRect);

/// ADD YOUR CODE HERE TO DELETE ALL OBJECTS

}

void display(Rectangle * arr[])

{

for (int i=0; i < SIZE; i++)

{

cout << arr[i]->getSerialNumber() << endl; /// Display the

serial number of the Rectangle object

}

}

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

Moving Objects Databases

Authors: Ralf Hartmut Güting, Markus Schneider

1st Edition

0120887991, 978-0120887996

More Books

Students also viewed these Databases questions