初探運算符重載------(減號)


可以參考: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

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM