CSE 333 Exercise 13

Out:   Friday, February 20
Due:   Monday, February 23 by 11 AM
Rating:   2 (note)
CSE 333 Exercise Rating Scale

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:

  1. In a file called Coordinate.h, define a purely virtual class (i.e., one with no implemented methods) called Coordinate that represents an abstract 2-dimensional coordinate. Your class must have a (pure virtual) method called ToString that returns a string representation, and may have a virtual (non-abstract) destructor, if needed.
  2. In files Cartesian.h and Cartesian.cc, create a subclass of Coordinate called Cartesian that represents a 2D cartesian coordinate (i.e., one with x and y values of type double). Implement ToString, and also add a method called Distance that calculates the distance between the object and a second Cartesian point object passed by reference as an argument.
  3. In files Polar.h and Polar.cc, create a subclass of Coordinate that represents a 2D polar coordinate (i.e., one with a radius and angle of type double). Implement ToString and also add a method called Distance that calculates the distance between the object and a second Polar object.

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::stringstream class can be useful for implementing the ToString methods. It supports writing values to a string with the << stream output operator.
  • A reasonable ToString function 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:

  1. By converting polar cordinates to Cartesian coordinates (x = r * cos(theta) and y = r * sin(theta)) and then using the Euclidean distance formula.
  2. 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.cc
  • ex13/Coordinate.h
  • ex13/Cartesian.h
  • ex13/Cartesian.cc
  • ex13/Polar.h
  • ex13/Polar.cc
  • ex13/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++ and valgrind).
  • 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 .cc and .h files 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.