#include <iostream>using namespace std;class Time{private:int hh,mm,ss;public:explicit Time(int h=0, int m=0, int s=0): hh(h),mm(m),ss(s) {}// 重载 =Time& operator = (const Time& t) {hh = t.hh;mm = t.mm;ss = t.ss;return *this;}// 重载 ()void operator()(int h,int m,int s) {hh = h;mm = m;ss = s;}// 重载 []int operator[] (int i) {cout << "X::operator[" << i << "]" << endl;return i;};int operator[] (const char * cp) {cout << "X::operator[" << cp << "]" << endl;return 0;};// 友元重载需要参数friend Time operator--(Time &t);friend Time operator--(Time &t, int);// 重载二元运算符Time operator+ (Time t) const {return Time(hh + t.hh, mm + t.mm, ss + t.ss);}Time operator- (Time t) const {return Time(hh - t.hh, mm - t.mm, ss - t.ss);}void ShowTime() const {cout << hh << ":" << mm << ":" << ss << endl;}};Time operator--(Time &t) {-- t.ss;return t;}Time operator--(Time &t, int) {t.ss --;return t;}int main() {Time t1(12,10,11);t1.ShowTime();cout << "reload () method1:\n";// 第一种写法t1.operator()(23,20,34);t1.ShowTime();cout << "reload () method2:\n";// 第二种写法t1(10,10,10);t1.ShowTime();cout << endl;cout << "reload = method:\n";Time t2;t2 = t1;t2.ShowTime();cout << endl;cout << "reload [] method:\n";int a = t2[1];int b = t2["abcd"];cout << "a=" << a << endl;cout << "b=" << b << endl << endl;cout << "reload --t and t--:\n";-- t2;t2.ShowTime();t2 --;t2.ShowTime();cout << endl;cout << "reload + and -:\n";Time t3 = t1 + t2;t3.ShowTime();Time t4 = t1 - t2;t4.ShowTime();return 0;}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。