Due: Monday, July 29th, 2024 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.
As usual, your code must:
Makefile
, as described above, that compiles the code
with the g++
options -Wall -g -std=c++17
cpplint
may be helpful in flagging potential style
problems. (You can ignore cpplint
complaints about
using namespace
directives)
You should submit your exercise to the course Gradescope.