Programming in C++

Exercise Sheet 3

These exercises provide practice with defining and using overloaded operators. In each case, you should add const annotations as appropriate.

  1. Fetch the file sdate.cc, containing the following simple Date class, as discussed in the second lecture. Add a << (output) operator for Dates, and write a small main method to test it. You will have to define operator<< as an independent function, and thus use only the public methods of Date.

  2. That representation is very simple, but unsuited to many of the operations we may wish to perform on dates. An alternative representation, in the file date.cc, represents a date as a Julian day number, i.e. the number of days since Monday 1st January 4713 BC. (Nothing special happened on that day, as far as I know, but it is conveniently prehistoric.)

    You don’t need to worry about the constants and functions defined before the Date class: they do the various conversions between representations. You won’t need to change them.

    Download date.cc and adapt your definition of the << operator to it. (This will be easy, since the new version supports the old public interface.)

  3. Implement a - operator to subtract two dates, returning a long, the difference in days between the dates. This can be done either as a member function or an independent function: use a member function.

    Use this to compute:

    1. The number of days to Christmas (don’t worry that this won’t work for the last 6 days of the year).

    2. Your age in days.

  4. It doesn’t make sense to add two Dates, but it is reasonable to add a Date and an int, in either order. Implement these two overloadings of the + operator.

    Use this to compute

    1. Tomorrow’s date (whenever the program is run).

    2. The date 28 days from today.

    3. The day when you’ll be twice as old (in days) as you are today.

    4. The day before 1 Jan 1.

    5. (Trick question) The day after 2 Sep 1752, when Britain and its colonies switched from the Julian calendar to the Gregorian one. (Run the command cal 1752 to confirm your answer.)

    Note that defining + does not automatically define a += operator for Dates. If you want to use that, you must define it too.

  5. Define == and < operators for Dates. If you have the include line
        #include <utility>
        using namespace std::rel_ops;

    this will also define the other relational operators.

    Use these to write a loop printing the dates for today, tomorrow, etc up to the end of the year.

  6. Write an input operator >> for dates.

  7. (Optional) The representation we’ve been using is very compact (only 4 bytes), but involves repeating the conversions from day numbers. For example printing a date does this conversion 3 times. Another representation would store the Julian day number as well as the day, month and year, keeping them synchronized. That would take more space, but some operations would be faster. What changes would be required?