| 2 | using namespace std; |
| 3 | |
| 4 | class Time{ |
| 5 | private: |
| 6 | int hh,mm,ss; |
| 7 | public: |
| 8 | explicit Time(int h=0, int m=0, int s=0): hh(h),mm(m),ss(s) {} |
| 9 | |
| 10 | // 重载 = |
| 11 | Time& operator = (const Time& t) { |
| 12 | hh = t.hh; |
| 13 | mm = t.mm; |
| 14 | ss = t.ss; |
| 15 | return *this; |
| 16 | } |
| 17 | |
| 18 | // 重载 () |
| 19 | void operator()(int h,int m,int s) { |
| 20 | hh = h; |
| 21 | mm = m; |
| 22 | ss = s; |
| 23 | } |
| 24 | |
| 25 | // 重载 [] |
| 26 | int operator[] (int i) { |
| 27 | cout << "X::operator[" << i << "]" << endl; |
| 28 | return i; |
| 29 | }; |
| 30 | int operator[] (const char * cp) { |
| 31 | cout << "X::operator[" << cp << "]" << endl; |
| 32 | return 0; |
| 33 | }; |
| 34 | |
| 35 | // 友元重载需要参数 |
| 36 | friend Time operator--(Time &t); |
| 37 | friend Time operator--(Time &t, int); |
| 38 | |
| 39 | // 重载二元运算符 |
| 40 | Time operator+ (Time t) const { |
| 41 | return Time(hh + t.hh, mm + t.mm, ss + t.ss); |
| 42 | } |
| 43 | Time operator- (Time t) const { |
| 44 | return Time(hh - t.hh, mm - t.mm, ss - t.ss); |
| 45 | } |
| 46 | |
| 47 | |
| 48 | void ShowTime() const { |
| 49 | cout << hh << ":" << mm << ":" << ss << endl; |
| 50 | } |
| 51 | }; |
| 52 | |
| 53 | Time operator--(Time &t) { |
| 54 | -- t.ss; |