#include <iostream.h>
class Time {
public:
Time( int = 0, int = 0, int = 0);
void setTime( int, int, int );
void setHour( int );
void setMinute( int );
void setSecond( int );
int getHour() const;
int getMinute() const;
int getSecond() const;
void printUniversal() const;
void printStandard();
private:
int hour; // 0 - 23 (24-hour clock format)
int minute; // 0 - 59
int second; // 0 - 59
}
Time::Time( int hour, int minute, int second )
{
setTime( hour, minute, second );
}
void Time::setTime( int hour, int minute, int second )
{
setHour( hour );
setMinute( minute );
setSecond( second );
}
void Time::setHour( int h )
{
hour = ( h >= 0 && h < 24 ) ? h : 0;
}
void Time::setMinute( int m )
{
minute = ( m >= 0 && m < 60 ) ? m : 0;
}
void Time::setSecond( int s )
{
second = ( s >= 0 && s < 60 ) ? s : 0;
}
int Time::getHour() const
{
return hour;
}
int Time::getMinute() const
{
return minute;
}
int Time::getSecond() const
{
return second;
}
void Time::printUniversal() const
{
cout << (hour<10 ? "0":"") << hour << ":"
<< (minute<10 ? "0":"") << minute << ":"
<< (second<10 ? "0":"") << second;
}
void Time::printStandard()
{
cout << ( ( hour == 0 || hour == 12 ) ?
12 : hour % 12 ) << ":"
<< (minute<10 ? "0":"") << minute << ":"
<< (second<10 ? "0":"") << second
<< (hour < 12 ? " AM" : " PM" );
}
int main()
{
Time wakeUp( 6, 45, 0 ); // non-constant object
const Time noon( 12, 0, 0 ); // constant object
// OBJECT MEMBER FUNCTION
wakeUp.setHour( 18 ); // non-const non-const
noon.setHour( 12 ); // const non-const
wakeUp.getHour(); // non-const const
noon.getMinute(); // const const
noon.printUniversal(); // const const
noon.printStandard(); // const non-const
return 0;
}