Chapter 13
Operator overloading
Simple Time class
Time
- hour: int
- min: int
+Time( )
+Time(hr: int, mn: int)
+equals( tm: Time): bool
+lessThan( tm: Time): bool
+ print( )
+setHour(hr: int)
+setMin(mn: int)
Holds hours and minutes
prints as : HH:MM
Could be used like this....
// in main
Time currentTime(hr, mn);
const Time LUNCH_TIME( 11, 30 ); // use 2 parameter constructor
cout << "Lunch time: ";
( cout );
cout << endl;
while( ( LUNCH_TIME ) ) { // loop until time to eat
cout << "Clock: ";
( cout );
cout << endl;
........ // other code
( getHourFromSystemClock( ) ) ;
( getMinuteFromSystemClock( ) ) ;
}
cout << "Class is Over: Time to go eat \n";
But we would like to make it more normal
// in main
Time currentTime(hr, mn);
const Time LUNCH_TIME( 11, 30 ); // use 2 parameter constructor
cout << "Lunch time: ";
( cout );
cout << endl;
while( ( LUNCH_TIME ) ) { // loop until time to eat
cout << "Clock: ";
( cout );
cout << endl;
........ // other code
( getHourFromSystemClock( ) ) ;
( getMinuteFromSystemClock( ) ) ;
}
cout << "Class is Over: Time to go eat \n";
Making Time more normal....
// in main
Time currentTime; // use default cons
马钢钢板质保书 来自淘豆网www.taodocs.com转载请标明出处.