Frank C.

asked • 12/05/20

C++ Grid Writer

C++: In this part of the assignment you will not need to write any code. Instead you will download a software project and submit your answers to questions about the code.



The project uses a class named GridWriter to display circles and rectangles in a text grid. The GridWriter class is a collection type class similar to an IntCollection. It stores Circle and Rectangle objects that inherit from a base class named Shape.


The GridWriter class has a member function named display that accepts a row count and column count as arguments, and prints out a text grid that shows the shapes that it stores.

Consider the main function below. It creates a GridWriter, adds some shapes, and then displays a grid:



int main() {


GridWriter gw;


// Add some circles...

gw.add(new Circle(10, 10, 9));

gw.add(new Circle(25, 20, 12));

gw.add(new Circle(25, 20, 5));


// Add a rectangle...

gw.add(new Rectangle(40, 0, 10, 10));


// Display the grid with 40 rows and 50 columns

gw.display(40, 50);

}

The console output will look like this:


https://gyazo.com/9215d24228975a628d119ee6aaa060a3




The code in the main function creates three Circle objects and one Rectangle object to the GridWriter. See if you can find the shapes in the output.

The Shape class defines x and y. Both the circle and the rectangle class inherit these member variables. Similarly, the Shape class defines the getArea and containsPoint member functions, and the circle and rectangle inherit them. The circle and rectangle classes extend the Shape class by adding radius or height and width, respectively.


Download the GridWriter.zip (Links to an external site.) file and examine the classes. Carefully read through the code and note any statements that you do not understand. When you are comfortable with the code, answer the following questions. Submit your answers in a text file.


http://fog.ccsf.edu/~mluttrel/cs110b/GridWriter.zip


1. The two argument constructor of the Shape class contains this code:

this->x = x;

this->y = y;

Why is it necessary to put “this->” in front of x and y? If you are not sure then remove these lines and see if it changes the output, which may provide you a hint why it is necessary. Note: the answer to this question is not what happens if you remove "this->" but why do you need it.


2. In the containsPoint member function of Circle, why are this->x and this->y in scope, even though they are not defined as member variables in Circle.h?


3. What is the purpose of the keyword “virtual” in front of Shape::containsPoint and Shape::getArea? What is the purpose of the “ = 0”?


4. In Circle.cpp the three argument Circle constructor is defined like this:

Circle:: Circle(int x, int y, int radius) : Shape(x, y) {

this->radius = radius;

}

What is the purpose of the “: Shape(x, y)” (shown in bold in the code above). What happens if you remove it?


5. List an example of polymorphism that you can find in the code base. In other words, list a statement where the datatype of a reference or pointer is not an exact match with the datatype of the object that it refers to.




1 Expert Answer

By:

Hoan T. answered • 12/07/20

Tutor
5 (21)

Ex-Teaching Assistant in Computer Science, with 2 years of experience

Still looking for help? Get the right answer, fast.

Ask a question for free

Get a free answer to a quick problem.
Most questions answered within 4 hours.

OR

Find an Online Tutor Now

Choose an expert and meet online. No packages or subscriptions, pay only for the time you need.