CS 202 Fall 2013  >  In-Class Challenge for Tuesday, October 1, 2013

CS 202 Fall 2013
In-Class Challenge for Tuesday, October 1, 2013

This challenge deals with a C++ class TimeSec. It is implemented in files timesec.h and timesec.cpp, unfinished versions of which are on the web page.

What is Already Written

An object of class TimeSec keeps track of a time of day in seconds, 24-hour time. The class has the following member functions.

Our Goal

When we are done, we want class TimeSec to have the following operators available.

So code like the following should work.

[C++]

TimeSec t1(8,59,30);  // 8:59:30
t1 += 40;             // 40 seconds later
t1.print();           // Should print " 9:00:10"
cout << endl;

TimeSec t2;
t2 = t1 + 120;        // 120 seconds after 9:00:10
                      // Note: the compiler writes the "=" operator
t2.print();           // Should print " 9:02:10"
cout << endl;

What to Do

Do as many of the following as you can before class ends. After each, please show me your work.

  1. Starting with files timesec.h and timesec.cpp from the web page, write a minimal version of the above operators so that the resulting code compiles with the test program test_timesec.cpp.
  2. Write the operators so that the code works and the tests pass. Here you may assume that we always add a nonnegative number of seconds to a stored time.
  3. Make the operators work with negative numbers of seconds as well. For example, “TimeSec t3 = t2 + (-3);”.
  4. Going even further: Write similar “-=” and binary “-” operators: “TimeSec t4 = t3 - 100;”.

Some Reminders


CS 202 Fall 2013: In-Class Challenge for Tuesday, October 1, 2013 / Updated: 1 Oct 2013 / Glenn G. Chappell