//time.cpp //an example of a simple time class //header file(s) #include //for cin, cout, and endl using namespace std; //class declaration class Time { public: //use default constructors and //destructors bool Set12(int hr, int min, char ap); //returns false if an invalid time //is entered //noon is 12:00 am, //midnight is 12:00 pm. bool Set24(int hr, int min); //returns false if an invalid time //is entered //noon is 12:00, midnight is 00:00. int GetMinutes(); int GetHour12(); int GetHour24(); char GetAMPM(); void Display12(); void Display24(); private: int hour; //stored on 24 hour clock int minute; }; //end of time class //implementation of class functions bool Time::Set12(int hr, int min, char ap) //Sets time when given hours and minutes //in 12 hour format. //When an invalid time //is entered, sets time to noon and //returns false. //noon is 12:00 am, midnight is 12:00 pm. { //set to a valid time, noon hour=12; minute=0; if ((hr<1) || (min<0) || (min>59) || (hr>12)) // || is OR return 0; //0 is false if ((ap!='a') && (ap!='A') && (ap!='p') && (ap!='P')) // && is AND // symbols in single quotes // are characters return 0; //Got here only if valid time if ((ap=='a') || (ap=='A')) // am // == is comparison EQUAL hour=hr; // = is ASSIGNMENT else // pm hour=hr+12; minute=min; if (hour == 24) hour = 0; return 1; //any nonzero integer is true } //end set12 bool Time::Set24(int hr, int min) //Sets time when given hours and minutes //in 24 hour format. //When an invalid time //is entered, sets time to noon and //returns false. //noon is 12:00, midnight is 0:00. //set default time { hour = 12; minute = 0; if ((hr<0) || (min<0) || (min>59) || (hr>=24)) // || is OR return 0; //0 is false hour=hr; minute=min; return 1; //any nonzero integer is true } //end set24 int Time::GetMinutes() {return minute;} int Time::GetHour12() { if ((hour<=12) && (hour>=1)) return hour; else if (hour==0) return 12; else return hour-12; } int Time::GetHour24() {return hour;} char Time::GetAMPM() { if (hour>0 && hour<=11) return 'A'; else if ((hour==12) && (minute==0)) return 'A'; else if ((hour==0) && (minute>0)) return 'A'; else return 'P'; } //end GetAMPM void Time::Display12() { cout << GetHour12() << ':' << minute << ' ' << GetAMPM() << 'M'; return; } //end Display12 void Time::Display24() { cout << hour << ':' << minute; } //end of Time implementation int main() { Time dawn; cout << "Before anything" << " has been set: "; dawn.Display24(); dawn.Set12(5,30,'a'); cout << "\nOnce set properly: "; dawn.Display12(); cout << "\nor on a 24 hour clock: "; dawn.Display24(); Time dusk; dusk.Set24(20,14); cout << "\n\nDusk in Denmark: "; dusk.Display24(); cout << "\nDusk in Syracuse: "; dusk.Display12(); Time bad; bad.Set12(5, 70, 'a'); cout << "\n\nBad time: "; bad.Display12(); cout << endl; if (bad.Set24(36,00)) { //Only gets here if request //to set was a good one. cout << "Good time: "; bad.Display24(); cout << '\n'; } else cout << "Invalid time.\n"; //You can't use private information //outside the member functions. //Try //minute=75; //dusk.minute=-32; return 0; } //end main