| 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.
- Constructor from 0, 1, 2, or 3
intparameters. Set hour, minute, and second from the parameters specified; unspecified values are set to zero. setTime. Parameters and effect just like constructor.- Get and set functions for the hour, minute, and second:
getHr,setHr,getMin,setMin,getSec,setSec. - Function
print. Takes anostream. Outputs the stored time to theostreamas hh:mm:ss, with hours padded with blanks, and minutes & seconds padded with zeroes.
Our Goal
When we are done, we want class TimeSec
to have the following operators available.
- An “
+=” operator for this class. This should take aTimeSecas its left operand and anintas its right operand. It should add that many seconds to the time stored in theTimeSec. Be sure that hours, minutes, and seconds all stay within range. - A corresponding binary “
+” operator.
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.
- Starting with files
timesec.handtimesec.cppfrom the web page, write a minimal version of the above operators so that the resulting code compiles with the test programtest_timesec.cpp. - 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.
- Make the operators work with negative numbers of seconds as well.
For example, “
TimeSec t3 = t2 + (-3);”. - Going even further:
Write similar
“
-=” and binary “-” operators: “TimeSec t4 = t3 - 100;”.
Some Reminders
- The name of the function implementing
(for example) the “
%” operator is “operator%”. For today, make this a global function. - Parameter passing:
- Pass most objects by reference-to-const
(
const Foo & f1). - Pass most simple types by value
(
double n). - Pass things you want to modify by reference
(
Foo & f2).
- Pass most objects by reference-to-const
(
- Return value of an operator:
- If modifying an existing value, return by reference
(
Foo & operator...). - If creating a new value, return by value
(
Foo operator...). - Not relevant today.
If giving access to an existing value,
return by reference
(
Foo & operator...) or by reference-to-const (const Foo & operator...), depending on whether the value is modifiable.
- If modifying an existing value, return by reference
(
- Some things are more fundamental than others. Made the not-so-fundamental things out of the fundamental things.
CS 202 Fall 2013: In-Class Challenge for Tuesday, October 1, 2013 /
Updated: 1 Oct 2013 /
Glenn G. Chappell