Type in the code examples on pp. 37-45 of the Weiss book. For anything that doesn't compile, make sure you understand why. For anything that produces unexpected results, make sure you understand why.
Type in a bunch of the code examples on pp. 53-83. Again, make sure you understand why things don't compile or don't work.
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. Conveniently enough, Eclipse will do a lot of this for you automatically.
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:
<<
operator so you can print out a
Date
easily (see Weiss chap. 5). 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:
=
operator does a "deep copy". Make sure you
test this fact.<<
operator so you can print out a
RunnerLog
easily (see Weiss chap. 5)avgSpeed
, 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.
Define a class LogBook
which implements a linked list of
RunnerLog
objects, using a null pointer internally to
indicate the end of the list (although users of the
LogBook
class should never see a null pointer).
Write the following member functions:
LogBook
.isEmpty
function which operates on a
LogBook
and returns a boolean telling whether it's
empty.<<
operator so you can print out a
LogBook
easily (see Weiss chap. 5)addFront
function which operates on a
LogBook
, takes in a RunnerLog
, and adds the
new log to the front of the list.getFirst
function which operates on a
LogBook
and returns its first log entry.pop
function which operates on a
LogBook
, removes its first log entry, and returns
the removed entry.entries
function which operates on a
LogBook
and returns how many RunnerLog
s are in
the list.totalMiles
function which operates on a
LogBook
and returns the total number of miles run in all
the log entries.reverse
function which operates on a
LogBook
and reverses the order of all its entries,
so the one that was at the front of the list is now at the back,
and vice versa.For entries
, totalMiles
,
reverse
, the destructor, and the overridden operators,
you can reasonably write the function either
with a loop or recursively. You should know how to do both, so make
sure at least two are written with a loop, and at least
two are written using recursion instead.