/******************************************************************************************** example 7 -- miltime.cpp Developing the military time class ********************************************************************************************/ #include "miltime.h" #include <iostream> using namespace std; //------- Enhanced Default Constructor ------- miltime::miltime(const int h, const int m, const int s) { hour=0; minute = 0; second = 0; addSecond(s); addMinute(m); addHour(h); } //------- Print the object in "pretty" format ------- void miltime::print(ostream& out) const { if (hour < 10) out << '0'; out << hour << ':'; if (minute < 10) out << '0'; out << minute << ':'; if (second < 10) out << '0'; out << second; }; //------- "managed" addition functions ------- void miltime::addHour(const int h) { hour = (hour + h) % 24; } void miltime::addMinute(const int m) { addHour((minute + m) / 60); minute = (minute + m) % 60; } void miltime::addSecond(const int s) { addMinute((second + s)/60); second = (second + s) % 60; } void miltime::addSame(const miltime& mt) { addHour(mt.hour); addMinute(mt.minute); addSecond(mt.second); } //------- user-defined operators ------- miltime miltime::operator+(const miltime& mt) const { miltime temp; temp.addHour(hour); temp.addMinute(minute); temp.addSecond(second); temp.addSame(mt); return temp; } ostream& operator<<(ostream& out, const miltime& mt) { mt.print(out); return out; }