#ifndef TIME_H_ #define TIME_H_ #include #include using namespace std; //a time consists of hour, minute, second //and AM/PM //military time (24hour) //normal/regular (12 hour) //hour can have the value 0-23 //minute can have value 0-59 //second can have value 0-59 class Time { private: int hour; int minute; int second; string amPm; public: friend ostream& operator<<(ostream&, const Time &); friend istream& operator>>(istream&, Time &); bool operator==(const Time&) const; void printTime();//normal 12 hour void printMilitary();//24 hour void tick();//increments seconds by 1 int getHour(); int getMinute(); int getSecond(); void setHour(int); void setMinute(int); void setSecond(int); Time(int, int, int); //constructor //this constructor takes 3 pieces of information Time(); virtual ~Time();//deconstructor }; //constructor takes hour (h), minute (m), //and second (s) and uses that to initialize //the private data members Time::Time(int h, int m, int s) { //we really should check that h, m, and s //are valid, how would we do that? setHour(h); setMinute(m); setSecond(s); } Time::Time() {hour=0;minute=0;second=0;} Time::~Time() { } //tick function/method void Time::tick(){ //need to increment seconds by 1 second++; //did I go over 59? if (second > 59) { minute++; second=0; //did minutes go over 59, if so add to hour if (minute > 59) { hour++; minute=0; //did hour go over 23, if so reset to 0 if(hour > 23){ hour=0; } } } }//end tick method //printTime method void Time::printTime(){ //prints the time in 12 hour format //hh:mm:ss //hour doesn't print right, we need a temporary //hour variable to print 1PM-11PM correctly //this statement does the same thing as the //following if/else statement //you can look it up // int temphour = (hour>12) ? hour-12 : hour; int temphour; if(hour > 12) temphour = hour-12;//gives us the right //hour number 13 = 1, 14 = 2, etc. else temphour = hour; //the setfill function fills empty space with //the specified character, in this case zero cout << setfill('0') << setw(2) << temphour << ":"; cout << setw(2) << minute << ":" << setw(2) << second; //how about the AM/PM? if (hour < 12) //0-11 cout << " AM"; else cout << " PM"; } void Time::printMilitary(){ //prints the time in 12 hour format //hh:mm:ss //hour doesn't print right, we need a temporary //hour variable to print 1PM-11PM correctly //this statement does the same thing as the //following if/else statement //you can look it up //the setfill function fills empty space with //the specified character, in this case zero cout << setfill('0') << setw(2) << hour << ":"; cout << setw(2) << minute << ":" << setw(2) << second; } int Time::getHour() { return hour; } int Time::getMinute() { return minute; } int Time::getSecond() { return 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; } bool Time::operator==(const Time &otherTime) const { return (otherTime.hour == hour && otherTime.minute == minute && otherTime.second == second); }//end == operator ostream& operator<<(ostream &out, const Time &t) { out << setw(2) << setfill('0'); out << t.hour << ":" << setw(2) << t.minute << ":" << setw(2) << t.second; out << setfill(' '); return out; }//end overloaded stream insertion operator istream& operator>>(istream &in, Time &t) { in>>t.hour; in.ignore(1);//ignore ':' in>>t.minute; in.ignore(1);//ignore ':' in>>t.second; return in; } #endif /*TIME_H_*/