Question: In this exercise, you are writing some source code files from scratch; two classes, one an abstract class Shape and the second a class Polygon

In this exercise, you are writing some source code files from scratch; two classes, one an abstract class Shape and the second a class Polygon which is a child of the Shape class. You will write a short main function in a main.cpp program file to demonstrate your Polygon class.
Developing the new code and then running the associated main program should demonstrate these learning outcomes:
The ability to understand redefinition of functions by a derived class, originally defined in a base class.
The ability to create virtual and pure virtual functions, understanding the difference between the two and what makes a class Abstract.
The ability to program constructors in a derived class, using their counterparts in the base class.
The ability to override a pure virtual function to create a non-abstract child class.
The ability to understand what makes a good virtual or pure virtual function.
Program Requirements
Activity 1)
This activity requires you to write a new class named Shape2D with the following features:
Private member variables:
xcoord: an integer x coordinate to help record position in a 2D space
ycoord: an integer y coordinate to help record position in a 2D space
Public member functions:
Default constructor setting xcoord and ycoord to 0
Constructor with 2 parameters for xcoord and ycoord
A pure virtual function for calculating Area
In the main function, you should attempt to create an object of type Shape2D and record the error message that you get from the compiler. Explain in a comment what is causing this error, as it relates to the pure virtual function for Area.
Activity 2)
This activity requires you to write a new class named Polygon which inherits (using public inheritance, which will be our default) from the Shape2D class, with the following features:
Private member variables:
width: an integer width
height: an integer height
Public member functions:
Default constructor setting width and height to 0.
Constructor with 4 parameter for width, height, xcoord and ycoord. Remember to use the constructor of Shape2D to assign its private members.
A virtual function for calculating Area, overriding the pure virtual function definition from Shape2D.
In the main function, you should attempt to create an object of type Polygon. Test the area function to ensure it is working properly.
Activity 3)
Suppose we wanted to check if two polygons are colliding based upon their position and their size. This might be a good case for:
a) A pure virtual function in Shape2D, which Polygon and other classes must override
b) A regular function in Shape2D, which we do not expect to need to redefine in other classes
c) A normal virtual function in Shape2D, which may be overriden by some children but not all
d) A normal function definition in main.cpp, written by the client
Consider these options and write a comment explaining which choice you would make and why. If you would like to implement your choice for fun, you may! But that is not required. I *do* have a correct answer in mind for which of these makes the most sense, but I can be persuaded by good reasoning!

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock

Lets proceed step by step to fulfill the requirements of the exercise Step 1 Creating the Shape2D Abstract Class Class Declaration Define an abstract ... View full answer

blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!