9-2 定義Time類中的運算子多載

來源:互聯網
上載者:User

/* (程式頭部注釋開始)
* 程式的著作權和版本聲明部分
* Copyright (c) 2011, 煙台大學電腦學院學生
* All rights reserved.
* 檔案名稱:
* 作 者: 張傳新
* 完成日期: 2012 年 04 月 14 日
* 版 本 號: V.1.0

* 對任務及求解方法的描述部分
* 問題描述:實現Time類中的運算子多載

* 程式頭部的注釋結束

#include <iostream> using namespace std;  class CTime  {  private:      unsigned short int hour;    // 時       unsigned short int minute;  // 分       unsigned short int second;  // 秒   public:      CTime(int h=0,int m=0,int s=0):hour(h), minute(m), second(s){ };      void setTime(int h,int m,int s);      void display();  friend ostream& operator << (ostream&,CTime&);//運算子“<<”重載為友元函數      //比較子(二目)的重載       bool operator > (CTime &t);      bool operator < (CTime &t);      bool operator >= (CTime &t);      bool operator <= (CTime &t);      bool operator == (CTime &t);      bool operator != (CTime &t);        //二目運算子的重載       CTime operator+(CTime &c);//返回c所規定的時、分、秒後的時間,例t1(8,20,25),t2(11,20,50),t1+t2為19:41:15       CTime operator-(CTime &c);//對照+理解       CTime operator+(int s);//返回s秒後的時間       CTime operator-(int s);//返回s秒前的時間         //一目運算子的重載       CTime operator++(int);//後置++,下一秒       CTime operator++();//前置++,下一秒       CTime operator--(int);//後置--,前一秒       CTime operator--();//前置--,前一秒         //賦值運算子的重載            CTime operator+=(CTime &c);      CTime operator-=(CTime &c);      CTime operator+=(int s);//返回s秒後的時間       CTime operator-=(int s);//返回s秒前的時間   };  //下面實現所有的運算子多載代碼。   //為簡化編程,請注意通過調用已有函數,利用好各函數之間的關係     void CTime::setTime(int h,int m,int s)  {      hour = h;      minute = m;      second = s;  }   //運算子“<<”重載為友元函數ostream& operator << (ostream& output,CTime& t){    output << t.hour << ":" << t.minute << ":" << t.second <<endl;return output;}  //比較子(二目)的重載   //對 “>”的定義   bool CTime::operator > (CTime &t)//將時分化成秒,再比較大小   {      if(hour * 3600 + minute * 60 + second > t.hour * 3600 + t.minute * 60 + second)      {          return true;      }        else      {          return false;      }  }    //對 “<”的定義   bool CTime::operator < (CTime &t)//將時分化成秒,再比較大小   {      if(hour * 3600 + minute * 60 + second < t.hour * 3600 + t.minute * 60 + second)      {          return true;      }        else      {          return false;      }  }    //對 “<=”的定義   bool CTime::operator <= (CTime &t)//將時分化成秒,再比較大小   {      if(hour * 3600 + minute * 60 + second <= t.hour * 3600 + t.minute * 60 + second)      {          return true;      }        else      {          return false;      }  }    //對 “>=”的定義   bool CTime::operator >= (CTime &t)//將時分化成秒,再比較大小   {      if(hour * 3600 + minute * 60 + second >= t.hour * 3600 + t.minute * 60 + second)      {          return true;      }        else      {          return false;      }  }    //對 “==”的定義   bool CTime::operator == (CTime &t)//時分秒都要相等   {      if(hour == t.hour && minute == t.minute && second == t.second)      {          return true;      }        else      {          return false;      }  }    //對 “!=”的定義   bool CTime::operator != (CTime &t)  {      if(hour != t.hour || minute != t.minute || second != t.second)      {          return true;      }        else      {          return false;      }  }    //二目運算子的重載   //返回c所規定的時、分、秒後的時間,例t1(8,20,25),t2(11,20,50),t1+t2為:41:15   CTime CTime::operator+(CTime &c)//先轉換成秒,再相加,然後輸出   {      int T;        T = hour * 3600 + minute *60 + second + c.hour * 3600 + c.minute * 60 + c.second;        hour = T / 3600;      T = T % 3600;      minute = T / 60;      second = T % 60;        return (*this);  }    //返回c所規定的時、分、秒後的時間   CTime CTime::operator-(CTime &c)//先轉換成秒,再相減,然後輸出   {      int T;        T = hour * 3600 + minute *60 + second - c.hour * 3600 - c.minute * 60 - c.second;      hour = T / 3600;      T = T % 3600;      minute = T / 60;      second = T % 60;        return (*this);  }  //返回s秒後的時間   CTime CTime::operator+(int s)  {      second = second + s;        if(second >= 60)      {          minute = minute + second /60;          second = second % 60;            if(minute >= 60)          {              hour = hour + minute / 60;              minute = minute % 60;                if(hour >= 24)              {                  hour = hour % 24;              }            }        }        return (*this);  }  //返回s秒前的時間   CTime  CTime::operator-(int s)  {      int T;        T = hour * 3600 + minute *60 + second - s;      hour = T / 3600;      T = T % 3600;      minute = T / 60;      second = T % 60;        return (*this);  }      //一目運算子的重載   //後置++,下一秒   CTime  CTime::operator++(int)  {      CTime temp = (*this);        int T;        T = hour * 3600 + minute *60 + second + 1;      hour = T / 3600;      T = T % 3600;      minute = T / 60;      second = T % 60;        return temp;  }      //前置++,下一秒   CTime CTime::operator++()  {      int T;        T = hour * 3600 + minute *60 + second + 1;      hour = T / 3600;      T = T % 3600;      minute = T / 60;      second = T % 60;        return (*this);  }  //後置--,下一秒   CTime  CTime::operator--(int)  {      CTime temp = (*this);        int T;        T = hour * 3600 + minute *60 + second - 1;      hour = T / 3600;      T = T % 3600;      minute = T / 60;      second = T % 60;        return temp;  }      //前置--,下一秒   CTime CTime::operator--()  {      int T;        T = hour * 3600 + minute *60 + second - 1;      hour = T / 3600;      T = T % 3600;      minute = T / 60;      second = T % 60;        return (*this);  }    CTime CTime::operator+=(CTime &c)  {      CTime c1;        c1 = (*this) + c;        return c1;  }  CTime CTime::operator-=(CTime &c)  {      CTime c1;        c1 = (*this) - c;        return c1;  }  CTime CTime::operator+=(int s)  {      CTime c1;        c1 = (*this) + s;        return c1;  }  CTime CTime::operator-=(int s)  {      CTime c1;        c1 = (*this) - s;        return c1;  }    void main()    {        CTime t1(8,20,25), t2(11,20,50), t;            cout << "t1為:";            cout << t1;            cout << "t2為:";            cout << t2;            cout << "比較兩個時間大小:" << endl;            if (t1 > t2) cout << "t1 > t2" << endl;            if (t1 < t2) cout << "t1 < t2" <<endl;            if (t1 == t2) cout <<"t1 = t2" <<endl;             if (t1 != t2) cout << "t1 ≠ t2" << endl;            if (t1 >= t2) cout << "t1 ≥ t2" << endl;            if (t1 <= t2) cout << "t1 ≤ t2" << endl;            cout<<endl;        t = t1;          t = t + t2;            cout << "t1 + t2 = ";            cout << t;        t = t1;        t = t - t2;            cout << "t1 - t2 = ";            cout << t;        t = t1;        t = t + 35;        cout<< "t1 + 35 = ";            cout << t;         t = t2;        t = t - 55;        cout<< "t2 - 55 = " ;        cout << t;        t = t1;        t = t ++;        cout << endl;        cout << "t1++ = ";        cout << t;        t = t1;        t = ++t;        cout << "++t1 = ";        cout << t;         t = t2;        t = t --;        cout << "t2-- = ";        cout << t;         t = t2;        t = --t;        cout << "--t1 = ";        cout << t;        t = t1;        t +=t2;        cout << "t1 += t2,t1 = " ;        cout << t;        t = t2;        t -= t1;        cout << "t2 -= t1,t2 = " ;        cout << t;         t = t1;        t += 5;        cout << "t1 += 5 = " ;        cout << t;        t = t1;        t -= 6;         cout << "t1 -= 6 = " ;        cout << t;        system("pause");    }  

運行結果:

經驗積累:<<和>>的運算子多載,改變了運算結果的顯示方式,使程式讀起來更順暢。。。

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.