/******************************************************************************************** ex2.cpp Developing the military time class ********************************************************************************************/ #include <iostream> using namespace std; //class definition class miltime { public: int hour; // 0-23 int minute; // 0-59 int second; // 0-59 }; void setup(miltime& t); void print(const miltime& t, ostream& out = cout); void addHour(miltime& t, const int h); void addMinute(miltime& t, const int m); void addSecond(miltime& t, const int s); void main() { miltime mtTest; setup(mtTest); print(mtTest); //Add 30 seconds to it and reprint it addSecond(mtTest, 30); print(mtTest); //Add 30 minutes to it and print it addMinute(mtTest, 30); print(mtTest); //Add 30 hours to it and print it addHour(mtTest, 30); print(mtTest); } void addHour(miltime& t, const int h) { t.hour = (t.hour + h) % 24; } void addMinute(miltime& t, const int m) { addHour(t, (t.minute + m) / 60); t.minute = (t.minute + m) % 60; } void addSecond(miltime& t, const int s) { addMinute(t, (t.second + s)/60); t.second = (t.second + s) % 60; } //------- Initialize the object void setup(miltime& t) { t.hour = 0; t.minute = 0; t.second = 0; } //------- Print the object in "pretty" format ------- void print(const miltime& t, ostream& out) { if (t.hour < 10) out << '0'; out << t.hour << ':'; if (t.minute < 10) out << '0'; out << t.minute << ':'; if (t.second < 10) out << '0'; out << t.second << endl; };