Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- class Time {
- int h, m, s;
- public:
- Time() { h = 0; m = 0; s = 0; }
- Time(int h, int m, int s) { SetTime(h, m, s); }
- void SetTime(int h1, int m1, int s1);
- int GetHours() const { return h; }
- int GetMinutes() const { return m; }
- int GetSeconds() const { return s; }
- Time operator+(Time t2);
- Time &operator+=(Time t2); // returning reference suppress copying...
- Time &operator++();
- Time operator++(int); // To support postfix ++
- Time &operator--();
- Time operator--(int); // To support postfix --
- friend ostream &operator<<(ostream &midheta, Time t);
- };
- void Time::SetTime(int h1, int m1, int s1) { // Better solution (faster)
- int total = 3600 * h1 + 60 * m1 + s1; // but more complicated
- h = (total / 3600) % 24;
- m = (total % 3600) / 60;
- s = (total % 3600) % 60; // Even total % 60 will work too...
- }
- ostream &operator<<(ostream &midheta, Time t) {
- if(t.h < 10) midheta << "0";
- midheta << t.h << ":" ;
- if(t.m < 10) midheta << "0";
- midheta << t.m << ":";
- if(t.s < 10) midheta << "0";
- midheta << t.s;
- return midheta;
- }
- Time Time::operator +(Time t2) {
- return Time(h + t2.h, m + t2.m, s + t2.s);
- }
- Time &Time::operator +=(Time t2) {
- SetTime(h + t2.h, m + t2.m, s + t2.s);
- return *this;
- }
- Time &Time::operator++() {
- SetTime(h, m, s + 1);
- return *this;
- }
- Time Time::operator++(int) {
- Time old(*this);
- SetTime(h, m, s + 1);
- return old; // Returns the old value!
- }
- Time &Time::operator--() {
- SetTime(h, m, s - 1);
- return *this;
- }
- Time Time::operator--(int) {
- Time old(*this);
- SetTime(h, m, s - 1);
- return old; // Returns the old value!
- }
- int main() {
- Time t1(7, 25, 47), t2(5, 43, 29);
- Time t3 = t1 + t2;
- cout << t3 << endl;
- t1 = ++t3; // This syntax i supported too!
- t1 = t3++;
- ++t3;
- t3 += t2;
- cout << t3 << endl;
- --t3;
- cout << t3 << endl;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement