Part 2 - Grid Writer In this part of the assignment you will not need to write a
ID: 3779162 • Letter: P
Question
Part 2 - Grid Writer 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 written questions regarding 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: . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 1 1 1 1 1 1 1 1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 1 1 1 1 1 1 1 1 1 1 1 1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 1 1 1 1 1 1 1 1 1 2 1 1 1 1 1 1 1 1 1 1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 1 1 1 1 1 1 1 1 . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 1 1 1 1 1 1 1 . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 1 1 1 1 1 1 1 . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 1 1 1 1 1 1 1 . . . . . . . . . . . . . . . . . . . . . . . . . . 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 2 1 1 1 1 1 1 1 . . . . . . . . . . . . . . . . . . . . . . 1 . . . 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 1 1 1 1 1 1 1 . . . . . . . . . . . . . . . . . . . 1 1 1 1 1 1 1 1 2 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 1 1 1 1 1 1 1 . . . . . . . . . . . . . . . . . . 1 1 1 1 1 1 1 1 1 2 2 1 1 1 1 1 2 2 2 2 2 2 2 2 2 1 1 1 1 1 1 1 . . . . . . . . . . . . . . . . . 1 1 1 1 1 1 1 1 1 1 2 2 2 1 1 1 1 1 2 2 2 2 2 2 2 1 1 1 1 1 1 1 1 . . . . . . . . . . . . . . . . 1 1 1 1 1 1 1 1 1 1 1 1 2 2 2 1 1 1 1 1 1 1 2 1 1 1 1 1 1 1 1 1 1 . . . . . . . . . . . . . . . . 1 1 1 1 1 1 1 1 1 1 1 1 1 2 2 2 2 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 . . . . . . . . . . . . . . . . 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 2 2 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 . . . . . . . . . . . . . . . . . 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 2 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 . . . . . . . . . . . . . . . . . . 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 1 1 1 1 1 1 1 1 1 1 1 1 1 1 . . . . . . . . . . . . . . . . . . 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 1 1 1 1 1 1 1 1 1 1 1 1 . . . . . . . . 1 1 1 1 1 1 1 1 1 1 . . 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 . . 1 1 1 1 1 1 1 1 1 . . . . . . . . . . 1 1 1 1 1 1 1 1 1 1 . . 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 . . . . . . 1 . . . . . . . . . . . . . . 1 1 1 1 1 1 1 1 1 1 . . 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 . . . . . . . . . . . . . . . . . . . . . 1 1 1 1 1 1 1 1 1 1 . . 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 . . . . . . . . . . . . . . . . . . . . . 1 1 1 1 1 1 1 1 1 1 . . . 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 . . . . . . . . . . . . . . . . . . . . . . 1 1 1 1 1 1 1 1 1 1 . . . . 1 1 1 1 1 1 1 1 1 1 1 1 1 . . . . . . . . . . . . . . . . . . . . . . . 1 1 1 1 1 1 1 1 1 1 . . . . . 1 1 1 1 1 1 1 1 1 1 1 . . . . . . . . . . . . . . . . . . . . . . . . 1 1 1 1 1 1 1 1 1 1 . . . . . . 1 1 1 1 1 1 1 1 1 . . . . . . . . . . . . . . . . . . . . . . . . . 1 1 1 1 1 1 1 1 1 1 . . . . . . . . . . 1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 1 1 1 1 1 1 1 1 1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 1 1 1 1 1 1 1 1 1
The code in the main function adds 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 instance 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.
Download the GridWriter.zip 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.
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. That is to say, 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.
Part 2 - Grid Writer
Submit your answers to the GridWriter questions in a plain text file, e.g. GridWriter.txt.
Explanation / Answer
1) "this -> " is used to distinguish local variables with instance variables.
Shape(int x, int y) and variables declared in the object are also x, y we use this -> operator to distinguish them. Otherwise we don't need it.
2) Circle has been derived from class Shape. Shape class the x and y variables. Circle has inherited them from Shape class. As usual we used the "this" keyword to access them.
3) "virtual" in front of Shape::containsPoint - indicates that there should not be any static linkage created for that function, Instead dynamically determined by the instance at which Shape class pointer is referring to. This what we call overriding(polymorphism).
eg: Shape *p = new Circle(); p->containsPoint() executes shape.containsPoint() if we don't use "virtual" keyword as opposed to circle.containsPoint().
Purpose of 0: says that Shape class is abstract(You can't create an object for it) and you must implement it in derived class.
4) We Circle class constructor is calling Shape class constructor to initialize x and y instance variables
5) GridWriter has Vector object of type Shape. But we are still able to store Circle and Rectangle class objects. It's an example of polymorphism where where the datatype of a reference or pointer is not an exact match with the datatype of the object that it refers to.