1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
|
#include <iostream>
#include <string>
#include <cstdlib>
using namespace std;
class Time{
private:
unsigned hours; //(legal values are in [0, 23]
unsigned minutes; //(legal values are in [0, 59]
unsigned seconds; //(legal values are in [0, 59]
public:
//constructor
Time( unsigned hoursValue, unsigned minutesValue, unsigned secondsValue );
//accessors
unsigned getHours() const;
unsigned getMinutes() const;
unsigned getSeconds() const;
//mutators
Time & setHours( unsigned hoursValue );
Time & setMinutes( unsigned minutesValue );
Time & setSeconds( unsigned secondsValue );
static bool ok (unsigned hoursValue, unsigned minutesValue, unsigned secondsValue);
}; //class Time
bool die (const string & msg);
int main (){
Time p(-1,18, 49);
cout<<p.getHours()<<":"<<p.getMinutes()<<":"<<p.getSeconds()<<endl;
}// end main
Time::Time( unsigned hoursValue, unsigned minutesValue, unsigned secondsValue ):
hours(hoursValue), minutes(minutesValue), seconds(secondsValue)
{
if (!ok(hours, minutes, seconds)) die( "Time::Time: not legal" );
} //Time::Time
Time & Time::setHours( unsigned hoursValue ){
if(hoursValue >59 ) die( "Time::hoursValue: not legal" );
hours= hoursValue;
}//Time::setHours
Time & Time::setMinutes( unsigned minutesValue ){
if(minutesValue > 59 ) die( "Time::minutesValue: not legal" );
minutes=minutesValue;
return *this;
}//Time::setMinutes
Time & Time::setSeconds( unsigned secondsValue ){
if(secondsValue >59 ) die( "Time::secondsValue: not legal" );
seconds=secondsValue;
return *this;
}//Time::setSeconds
unsigned Time::getHours() const {
return hours;
}
unsigned Time::getSeconds() const {
return seconds;
}
unsigned Time::getMinutes() const {
return minutes;
}
bool Time::ok (unsigned hoursValue, unsigned minutesValue, unsigned secondsValue){
if ( hoursValue >23 && minutesValue > 59 && secondsValue >59 ) {
return false;
} else return true;
}
bool die (const string & msg) {
cerr<<endl<<"Fatal Error"<<msg<<endl;
exit(EXIT_FAILURE);
}
|