初探运算符重载------(减号)

可以参考:https://blog.csdn.net/zgl_dm/article/details/1767201

本例中 t1- t2,t2可以看做Time成员函数中的形参。

 1 #include<iostrean>
 2 
 3 using namespace std;
 4 
 5 int main(int argc, char* argv[])
 6 {
 7     Time t1(1, 1);
 8     Time t2(2, 0);
 9     Time t = t1- t2;
10 
11     cout << t.h <<"  "<<t.min<<endl;
12     system("PAUSE");
13     return 0;
14 }
main.c
 1 #ifndef TIME_H
 2 #define TIME_H
 3 
 4 class Time
 5 {
 6     public:
 7     Time(int h, int min)
 8     {
 9         this->h=h;
10         this->min=min;
11     }
12     ~Time(){};
13     int h;
14     int min;
15     
16     //在类的内部进行运算符重载
17     Time operator-(const Time &t)const
18     {
19         Time sub(0,0);
20         sub.h = h - t.h;
21         sub.min = min - t.min;
22         return sub;
23     }
24 }
25 
26 #endif
Time.h
原文地址:https://www.cnblogs.com/wangbin-heng/p/9527135.html