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 nonabstract child class.
The ability to understand what makes a good virtual or pure virtual function.
Program Requirements
Activity
This activity requires you to write a new class named ShapeD with the following features:
Private member variables:
xcoord: an integer x coordinate to help record position in a D space
ycoord: an integer y coordinate to help record position in a D space
Public member functions:
Default constructor setting xcoord and ycoord to
Constructor with 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 ShapeD 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
This activity requires you to write a new class named Polygon which inherits using public inheritance, which will be our default from the ShapeD 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
Constructor with parameter for width, height, xcoord and ycoord. Remember to use the constructor of ShapeD to assign its private members.
A virtual function for calculating Area, overriding the pure virtual function definition from ShapeD
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
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 ShapeD which Polygon and other classes must override
b A regular function in ShapeD which we do not expect to need to redefine in other classes
c A normal virtual function in ShapeD 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!