his C++ question is about a grid writer and no code is needed to be written, questions just need to be answered. 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 is the image attached in the assignment: 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: http://fog.ccsf.edu/~mluttrel/cs110b/GridWriter.zip file and examine the classes. Carefully read through the code. 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.
This C++ question is about a grid writer and no code is needed to be written, questions just need to be answered.
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 is the image attached in the assignment:
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: http://fog.ccsf.edu/~mluttrel/cs110b/GridWriter.zip
file and examine the classes. Carefully read through the code. 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. 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.
Trending now
This is a popular solution!
Step by step
Solved in 2 steps