CSE 333 Exercise 13
Due: Monday, February 23 by 11 AM
Rating: 2 (note)
Each exercise this quarter is rated on a integer scale of 1 – 5, inclusive, with 1 being the "least time-consuming" and 5 being the "most time-consuming".
This difficulty scale is meant as a rough guide for you in predicting the amount of time to set aside for each exercise as you balance the work required for 333 with your other obligations. However, it is necessarily imperfect as everyone's set of circumstances and experiences with the exercises differ. If your experience with an exercise does not align with its rating, that is not a reflection of you or your abilities.
Goals
- Utilize C++ inheritance by defining a class that inherits from and implements an abstract class.
Problem Description
Write a C++ program with the following classes:
- In a file called
Coordinate.h, define a purely virtual class (i.e., one with no implemented methods) calledCoordinatethat represents an abstract 2-dimensional coordinate. Your class must have a (pure virtual) method calledToStringthat returns a string representation, and may have a virtual (non-abstract) destructor, if needed. - In files
Cartesian.handCartesian.cc, create a subclass ofCoordinatecalledCartesianthat represents a 2D cartesian coordinate (i.e., one with x and y values of typedouble). ImplementToString, and also add a method calledDistancethat calculates the distance between the object and a secondCartesianpoint object passed by reference as an argument. - In files
Polar.handPolar.cc, create a subclass ofCoordinatethat represents a 2D polar coordinate (i.e., one with a radius and angle of typedouble). ImplementToStringand also add a method calledDistancethat calculates the distance between the object and a secondPolarobject.
Then, in file ex13.cc, write a main
function that tries out your classes (i.e., create
instances of the classes you defined and use the methods you wrote).
Finally, create a suitable Makefile.
The command make should build an executable program
ex13, recompiling individual files only when needed.
The command make clean should remove the
ex13 executable, all .o files, and any
editor or other backup files whose names end in ~.
Implementation Notes
ToString Methods
Consider the following hints when implementing your
ToString methods:
- The
std::stringstreamclass can be useful for implementing theToStringmethods. It supports writing values to a string with the<<stream output operator. - A reasonable
ToStringfunction should return a string identifying the type of the object and values of its instance variables.
Distance Methods
Polar distance may be unfamiliar or old, so as a quick refresher, we can find polar distance in two ways:
- By converting polar cordinates to Cartesian coordinates
(
x = r * cos(theta)andy = r * sin(theta)) and then using the Euclidean distance formula. - Using the polar distance formula:
distance = sqrt(r1^2 + r2^2 - (2 * r1 * r2 * cos(theta1 - theta2)))
Style Focus
Inheritance Keywords
Remember that you'll need to tell the compiler (1) when a method
will be inherited by a derived class with the virtual
keyword, and (2) when a virtual method gets overridden with the
override keyword.
Refer to the lecture material if you need to see examples.
Inheritance Principles
We only require that you have a (pure virtual)
ToString method in Coordinate.h, but it's
up to you to add anything else that you may want your subclasses to
inherit.
As a starting point, consider this question: is there behavior that
is shared by all of your subclasses?
Also recall good practices for inheritance.
Your design should ensure that your classes have good encapsulation
of their variables and methods, and that inheritance relationships
between your classes allow only what is necessary to be shared
between them.
Makefile
Recall the style focus from Exercise 10, as it still applies here! This time you have even more files to manage, so it may be helpful to draw out a dependency graph before you start coding.
Submission
Submit the following file(s) by creating an ex13-submit tag in your exercise repo before the assignment deadline. The file(s) should be located in the exact directory listed below, including capitalization:
ex13/ex13.ccex13/Coordinate.hex13/Cartesian.hex13/Cartesian.ccex13/Polar.hex13/Polar.ccex13/Makefile
Your code must:
- Compile without errors or warnings on CSE Linux machines
(lab workstations,
attu, or CSE home VM). - Have no runtime errors, memory leaks, or memory errors
(
g++andvalgrind). - Be contained in the files listed above with your Makefile compiling your code with the
g++options-Wall -g -std=c++17. - Have a comment at the top of your
.ccand.hfiles with your name(s) and CSE or UW email address(es). - Be pretty: the formatting, modularization, variable and
function names, commenting, and so on should be consistent with
class style guidelines.
Additionally, the linter shouldn't have any complaints about your
code (
cpplint.py). - Be robust: your code should deal with hard-to-handle/edge cases and bogus user input (if there are any) gracefully.