For the following programming problems, be sure your classes have
exactly the interface I specify; I'll be testing them by
linking them together with my own main
function, and if
they don't match up, it'll be extra work for me, which makes me cranky
just at the time that I'm grading your homework :-)
Every class should have a static test
function which runs
a bunch of test cases on the other functions and reports whether they all
produced the expected results. You'll presumably test these things by
writing a main
function that calls the various
test
functions.
Follow the usual C++ conventions with regard to header files, getter names, etc.
Keep efficiency and memory safety in mind. For example, if you're returning a large object, decide whether to return a pointer, return a whole object, or return a const reference. Make sure accessor functions are marked as such, so the compiler can optimize them and they can be called on const objects. Watch out for memory leaks and dangling pointers/references. And so on.
Define a class Date
with integer fields for
day-of-month, month, and year.
Write the following member functions:
=
(assignment) operator<<
operator so you can print out a
Date
easily. It would be nice if this
printed out month names rather than numbers.dayInYear
, which operates on a Date
and
returns an int
telling how many days it has been since the
last New Year's Eve. For example, Jan. 1 would be day 1; Feb. 4 would
be day 35; etc. Assume there are no leap years. (Extra credit: handle
leap years correctly.)==
operator which tells whether two
Date
objects represent the same date (i.e. the
same day, the same month, the same year).!=
operator which produces the opposite
result of ==
. (This doesn't come automatically: if you
override one, you really should override the other!)daysLater
, which operates on a Date
, takes
in an int
, and returns a pointer to a newly-allocated
Date
that many days later.<
operator which tells whether one
Date
object represents an earlier date than another.-
operator that
takes two Date
s and returns the difference between them in
days+
operator that takes a
Date
and an integer, and returns a Date
(I'm
not sure how to do this without memory leaks....)+=
operator that takes a
Date
and an integer, and modifies the Date
to
be that many days laterDefine a class RunnerLog
to represent an entry in a
runner's log book. It should have fields for the date of the
run (a Date
), how many miles (double), how many hours
(double), and a free-form comment field (string). Write the following
member functions:
=
(assignment) operator. Make sure
the =
operator does a "deep copy". Make sure you
test this fact.<<
operator so you can print out a
RunnerLog
easilyavgSpeed
, which given a RunnerLog
, returns
the average running speed in miles per hour for that run.==
operator (and !=
doing
the opposite)addComment
, which given a RunnerLog
and a
string, modifies the RunnerLog
by adding the string onto
the end of whatever comments were already there.