: Default constructor with default parameters for hours, minutes, and seconds, all set to zero. Overloaded extraction operator that prompts a user to enter the values for hours, minutes, and seconds for the Time. Continuously prompt the user if the entered value is not valid for a 24-hour clock. Overloaded insertion operator to display the Time in the form hr:min: sec. If hours, minutes, and/or seconds is a single digit, the time should be shown as 0x: where x is hours, minutes, or seconds. Example: 02:33:45, 13:05:66, 23:17:09 Overloaded prefix increment and postfix increment operators to increment the Time by one second. Overloaded subtraction operator that returns a Time object that indicates the difference between two Times. Overloaded == operator that returns true or false indicating that two Times are the same or not, respectively. Two Times are the same if they have equal values for hours, minutes, and seconds.
#include <iostream>
#include <iomanip>
using namespace std;
class Time {
int hours;
int minutes;
int seconds;
public:
Time();
Time& operator++();
Time operator++(int);
Time operator-(const Time& rhs) const;
bool operator==(const Time& rhs) const;
friend istream& operator>>(istream& is, Time& t);
friend ostream& operator<<(ostream& os, const Time& t);
};
Time::Time() : hours(0), minutes(0), seconds(0)
{}
Time& Time::operator++() {
if (++seconds == 60) {
seconds = 0;
++minutes;
}
if (minutes == 60){
minutes = 0;
++hours;
}
if (hours == 24) {
hours = 0;
}
return *this;
}
Time Time::operator++(int) {
Time tmp = *this;
++(*this);
return tmp;
}
Time Time::operator-(const Time& rhs) const {
Time res;
res.seconds = seconds - rhs.seconds;
if (res.seconds < 0) {
res.seconds += 60;
res.minutes = -1;
}
res.minutes += minutes - rhs.minutes;
if (res.minutes < 0) {
res.minutes += 60;
res.hours = -1;
}
res.hours += hours - rhs.hours;
if (res.hours < 0) {
res.hours += 24;
}
return res;
}
bool Time::operator==(const Time& rhs) const {
return seconds == rhs.seconds && minutes == rhs.minutes &&
hours == rhs.hours;
}
istream& operator>>(istream& is, Time& t) {
while (true) {
cout << "Enter values for hours: ";
is >> t.hours;
if (t.hours < 0 || t.hours > 23) {
cout << "Incorrect value, try again" << endl;
}
else {
break;
}
}
while (true) {
cout << "Enter values for minutes: ";
is >> t.minutes;
if (t.minutes < 0 || t.minutes > 59) {
cout << "Incorrect value, try again" << endl;
}
else {
break;
}
}
while (true) {
cout << "Enter values for seconds: ";
is >> t.seconds;
if (t.seconds < 0 || t.seconds > 59) {
cout << "Incorrect value, try again" << endl;
}
else {
break;
}
}
return is;
}
ostream& operator<<(ostream& os, const Time& t) {
os << setfill('0') << setw(2) << t.hours << ":"
<< t.minutes << ":" << t.seconds;
return os;
}
int main() {
Time t1, t2, t3;
cout << "Enter a first time" << endl;
cin >> t1;
cout << "Enter a second time" << endl;
cin >> t2;
cout << "t1++ is " << t1++ << endl;
cout << "++t2 is " << ++t2 << endl;
cout << t1 << " - " << t2 << " = " << t1 - t2 << endl;
return 0;
}
Comments
Leave a comment