// time.h // Cary Matsuoka // modified on 12/17/96 // modified on 4/29/97 // last modified on 4/27/98 #include #include #ifndef _time_h #define _time_h class time{ public: // Lesson 28 material time(); // default constructor time(int hour, int minute, char format); // constructor with initial values time(const time&); // copy constructor void setTime(int hour=0, int minute=0, char format='a'); // set values for time void getTime(); // get time from cin void print() const; // public print to cout void changeDisplay(); // changes display to other state, standard or military // Lesson 29 material void print(ostream &) const; // to support use of << operator void getTime(istream &); // to support use of >> operator // Lesson 30 material time operator++(); // preincrement time operator++(int); // postincrement time operator--(); // predecrement time operator--(int); // postdecrement bool operator== (const time &) const; // overloaded == operator bool operator< (const time &) const; // overloaded < operator bool operator> (const time &) const; // overloaded > operator private: enum display {standard, military}; int myHour; // 0-23 int myMinute; // 0-59 display myDisplay; // standard or military }; // free functions void printMinute (ostream &, int); void printHour (ostream &, int); ostream& operator<< (ostream&, const time &); // overloaded << operator istream& operator>> (istream&, time&); // overloaded >> operator // Begin Lesson 28 material // Default constructor, allows for declaration of time class without initial values // time t1, t2; time::time() : myHour (0), myMinute(0), myDisplay(standard) { // all private variables initialized using initializer list } // Constructor with initial values supplied as 3 integers // Sample usage: // time t1(11,28,'p'); time::time (int hour, int minute, char format) { setTime (hour,minute,format); } // Copy constructor // Sample usage: // time t2(t1); creates a new time object, t2, as a copy of t1 time::time (const time &temp) : myHour(temp.myHour), myMinute(temp.myMinute), myDisplay(temp.myDisplay) { // all private variables initialized using initializer list } // Allows user to set a time, needs 3 values void time::setTime (int hour, int minute, char format) { if ((minute < 0) || (minute > 59)) // clean up minute if necessary minute = 0; myMinute = minute; // assign minute, no special cases if ('m' == format) // military format { if ((hour < 0) || (hour > 23)) // clean up hour if necessary hour = 0; myHour = hour; // stored as 0-23 myDisplay = military; } else { myDisplay = standard; if ((hour < 1) || (hour > 12)) // clean up hour if necessary hour = 12; if ('a' == format) if (12 == hour) myHour = 0; // internally represent 12 am as hour 0 else myHour = hour; else // format == 'p' if (hour < 12) myHour = hour + 12; else myHour = 12; } } // public getTime function, gets initial values for a time object from cin void time::getTime () { int hour, minute; char format; cin >> hour >> minute >> format; setTime (hour,minute,format); } void printMinute (ostream &out, int minute) { if (minute < 10) out << "0" << minute; else out << minute; } void printHour (ostream &out, int hour) { if (hour < 10) out << " " << hour; else out << hour; } // public print function void time::print() const { if (standard == myDisplay) if ((0 == myHour) || (12 == myHour)) cout << "12"; else printHour (cout, myHour % 12); else printHour (cout, myHour); // print military hour cout << ":"; printMinute (cout, myMinute); // same for both formats if (standard == myDisplay) if (myHour < 12) cout << " am"; else cout << " pm"; } // switches format of time to opposite state void time::changeDisplay() { if (standard == myDisplay) myDisplay = military; else myDisplay = standard; } // begin Lesson 29 material void time::print(ostream &outfile) const // to support use of << operator { if (standard == myDisplay) if ((0 == myHour) || (12 == myHour)) outfile << "12"; else printHour (outfile, myHour % 12); else printHour (outfile, myHour); // print military hour outfile << ":"; printMinute (outfile, myMinute); // same for both formats if (standard == myDisplay) if (myHour < 12) outfile << " am"; else outfile << " pm"; } void time::getTime(istream &infile) // to support use of >> operator { int hour, minute; char format; infile >> hour >> minute >> format; setTime (hour,minute,format); } ostream& operator<< (ostream &outfile, const time &temp) // overloaded << operator { temp.print (outfile); return outfile; } istream& operator>> (istream &infile, time &temp) // overloaded >> operator { temp.getTime(infile); return infile; } // Lesson 30 material // preincrement operation time time::operator++ () { if (59 == myMinute) { myMinute = 0; if (23 == myHour) myHour = 0; else myHour++; } else myMinute++; return *this; } // postincrement operation time time::operator++(int) { time temp = *this; ++(*this); // use the already defined preincrement operator return temp; } // predecrement operation time time::operator-- () { if (0 == myMinute) { myMinute = 59; if (0 == myHour) myHour = 23; else myHour--; } else myMinute--; return *this; } // postdeccrement operation time time::operator--(int) { time temp = *this; --(*this); return temp; } bool time::operator== (const time &temp2) const // overloaded == operator { return ((myHour == temp2.myHour) && (myMinute == temp2.myMinute)); } bool time::operator< (const time &temp2) const // overloaded < operator { if (myHour < temp2.myHour) return true; else if (myHour > temp2.myHour) return false; else // myHour == temp2.myHour return (myMinute < temp2.myMinute); } bool time::operator> (const time &temp2) const // overloaded > operator { return ! ((*this < temp2) || (*this == temp2)); } #endif