CS 202 Fall 2013 > In-Class Challenge for Thursday, October 3, 2013 |
CS 202 Fall 2013
In-Class Challenge for Thursday, October 3, 2013
This challenge deals with C++ class TimeSec
again.
It is implemented in files
timesec.h
and
timesec.cpp
,
unfinished versions of which are on the web page.
You do not need to have made the changes from last time to do this challenge!
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
int
parameters. 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 theostream
as 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.
- Comparison operators “
==
” and “!=
” for this class. This should take twoTimeSec
objects and see if they represent the same time of day. - Operators “
++
” (both pre and post versions) and “--
” (again, both pre and post) Be sure that hours, minutes, and seconds all stay within range. - Starting with files
timesec.h
andtimesec.cpp
from the web page, write a minimal version of operators==
and!=
so that the resulting code compiles with the test programtest2_timesec.cpp
. - Write the operators so that the code works and the tests pass.
- Write minimal versions of
the pre and post versions of the
++
operator. The resulting code should compile with the test programtest3_timesec.cpp
. - Write the operators so that the code works and the tests pass.
- Write similar
--
operators. - 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.
So code like the following should work.
[C++]
TimeSec t1(8,59,59); // 8:59:59 TimeSec t2(9); // 9:00:00 ++t1; // Now t1 is also 9:00:00 if (t1 == t2) cout << "Success!" << endl;
What to Do
Do as many of the following as you can before class ends. After each, please show me your work.
Some Reminders
CS 202 Fall 2013: In-Class Challenge for Thursday, October 3, 2013 /
Updated: 3 Oct 2013 /
Glenn G. Chappell