//Fig. 16.2: fig16_02.cpp
//Time class.
#include
using namespace std;
//Time abstract data type (ADT) definition
class Time
{
public:
Time();
void setTime( int, int, int );
void printMilitary();
void printStandard();
private:
int hour;
int minute;
int second;
}; // end class Time
// Time constructor initializes each data member to zero.
// Ensure all Time objects start in a consistent state.
Time::Time()
{
hour = minute = second =0;
}
// Set a new Time value using military time. Perform validity
// checks on the fata values. Set invalid values to zero.
void Time::setTime( int h, int m, int s )
{
hour = ( h >=0 && h minute = ( m>=0 && m second = ( s>=0 && s} // end function setTime
//Print Time in military format
void Time::printMilitary()
{
cout }// end function printMilitary
// Print Time in standard format
void Time::printStandard()
{
cout }// end function printStandard
// Driver to test simple class Time
int main()
{
Time t; // instantiate object t of class Time
cout t.printMilitary();
cout t.printStandard();
t.setTime( 13, 27, 6 );
cout t.printMilitary();
cout t.printStandard();
t.setTime( 99, 99, 99 ); // attempt invalid settings
cout t.printMilitary();
cout t.printStandard();
cout return 0;
}// end function main
- Apr 14 Thu 2011 14:16
Time class
close
全站熱搜
留言列表
禁止留言
留言列表

