This preview shows page 1. Sign up to view the full content.
Unformatted text preview: k at the
implementation, to go in TimeType.cpp.
void TimeType::Set(int hrs, int mins,
void TimeType::Set(int hrs, int
hours = hrs;
minutes = mins;
seconds = secs;
} We just refer to the private data by name.
Applied Programming Applied Programming Here's an example.
TimeType time1, time2;
cout << "time1 equals time2\n";
This is like a binary operator (==) getting its left
operand from before the . and the second
operand from the argument.
This is actually how operators are done, as we
shall soon see.
shall Applied Programming This is just some newfangled way of data hiding.
Can't we do better?
Sure. We can ensure that the value of a
TimeType is always valid.
W hat if the hours to be set exceeds 23, or the
seconds is negative?
There are varying levels of protection, each
requiring differing levels of intervention.
Applied Programming Here's how. Precede the store with Level 1 if (secs > 59) // assume non-negative
mins += secs/60;
secs %= 60;
if (mins > 59) // also here
hrs += mins/60;
mins %= 60;
hrs %= 24; // and again Whatever i...
View Full Document
This document was uploaded on 04/07/2014.
- Spring '14