out: Wednesday, May 7, 2025
due: Monday, May 12, 2025 by 10:00 am,
No late exercises accepted.
Goals: Write a program using C++ inheritance to explore how to create and use C++ classes and subclasses.
Description: Write a C++ program with the following classes:
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.
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.
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.
ex13.cc
,
write a main
function that tries out your classes.
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 ~
.Hint: The std::stringstream
class can be useful for
implementing the ToString
methods. It supports writing
values to a string with the <<
stream output operator.
Hint: A reasonable ToString
function should return a string
identifying the type of the object and values of its instance variables.
Your code must:
Makefile
as described above that compiles the code
with the g++
options -Wall -g -std=c++17
const
and so on must make us
smile rather than cry. (Hints: Google C++ Style Guide, cpplint
)You should submit your exercise using the Gradescope dropbox linked on the course resources web page.