• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>
            流量統計:
            Rixu Blog (日需博客)
            日需博客,每日必需來踩踩哦..
            posts - 108,comments - 54,trackbacks - 0
            以下示例中定義了一個class test, 重載了<, +, +=, =, ==, <<, >>等符號:

            #include<iostream>
            #include<vector>
            using namespace std;

            class test{
            public:
                 int v;
               /*構造函數*/
                 test():v(0){}
                 test(const int &a):v(a){}
                 test(const test &t1):v(t1.v){} 
                
               /*以下重載小于號 < */
                 //比較兩個對象的大小 
                 bool operator<(const test &t1) const{ 
                     return (v < t1.v);
                 }
                 //比較對象和int的大小 
                 bool operator<(const int &t1) const{ 
                     return (v < t1);
                 }
                 //友元函數,比較int和對象的大小 
                 friend inline bool operator<(const int &a, const test & t1){
                     return (a < t1.v);
                 }
                
               /*以下重載賦值號 = */
                 //對象間賦值 
                 test & operator=(const test &t1){
                     v = t1.v;
                     return *this;
                 }
                 //int賦值給對象 
                 test & operator=(const int &t1){
                     v = t1;
                     return *this;
                 }
                
               /*以下重載加號 + */
                 //對象加上 int 
                 test operator+(const int & a){
                     test t1;
                     t1.v = v + a;
                     return t1;
                 }
                 //對象加對象 
                 test operator+(test &t1){
                     test t2;
                     t2.v = v + t1.v;
                     return t2;
                 }
                
               /*以下重載加等號 += */  
                 //對象加上對象 
                 test &operator+=(const test &t1){
                     v += t1.v;
                     return *this;
                 }  
                 //對象加上int
                 test &operator+=(const int &a){
                     v += a;
                     return *this;
                 }

               /*以下重載雙等號 == */  
                 //對象==對象 
                 bool operator==(const test &t1)const{
                     return (v == t1.v);
                 }  
                 //對象==int
                 bool operator==(const int &t1)const{
                     return (v == t1);
                 }  
                
               /*以下重載 輸入>> 輸出<< */
                 /*友元函數,輸出對象*/
                 friend inline ostream & operator << (ostream & os, test &t1){
                     cout << "class t(" << t1.v << ")" << endl;
                     return os;
                 }
                 /*友元函數,輸入對象*/
                 friend inline istream & operator >> (istream & is, test &t1){
                     cin >> t1.v;
                     return is;
                 }
            };

            int main(){
                 test t0, t1(3);
                 test t2(t1);
                 cout << t0 << t1 << t2;
                 cin >> t1;
                 t2 = t1;
                 t2 += t1;
                 t1 += 10;
                 cout << t2;
                 if(t1 < t2) cout << "t1 < t2"; 
                 else if(t1 == t2) cout << "t1 = t2";
                 else /* t1 > t2*/ cout << "t1 > t2"; 
                 cout <<endl;
                 system("pause");
                 return 0;
            }
            Logo
            作者:Gezidan
            出處:http://www.rixu.net    
            本文版權歸作者和博客園共有,歡迎轉載,但未經作者同意必須保留此段聲明,且在文章頁面明顯位置給出原文連接,否則保留追究法律責任的權利。
            posted on 2011-08-04 14:35 日需博客 閱讀(689) 評論(0)  編輯 收藏 引用 所屬分類: C C++轉載
            综合网日日天干夜夜久久| 久久精品国产亚洲一区二区三区| 四虎国产精品成人免费久久| 97久久国产综合精品女不卡| 国产精品久久自在自线观看| 久久91这里精品国产2020| 中文字幕无码精品亚洲资源网久久 | 久久夜色精品国产噜噜亚洲AV| 国内精品伊人久久久久av一坑 | 天天躁日日躁狠狠久久| 国产精品久久毛片完整版| 一本色综合久久| 国产成人精品久久亚洲高清不卡| 久久国语露脸国产精品电影| 国产精品99久久久久久董美香| 狠狠综合久久综合88亚洲| 国产99久久久国产精品~~牛| 亚洲精品乱码久久久久久按摩| 国产亚洲美女精品久久久| 国产成人精品久久免费动漫| 久久精品人人做人人爽电影| 很黄很污的网站久久mimi色| 国产精品一区二区久久不卡| 久久婷婷午色综合夜啪| 久久成人国产精品一区二区| 欧美综合天天夜夜久久| 97久久久精品综合88久久| 亚洲中文久久精品无码| 国产精品久久久久久五月尺| 色综合久久天天综线观看| 久久久综合香蕉尹人综合网| 久久精品国产69国产精品亚洲| 精品综合久久久久久888蜜芽| 亚洲av伊人久久综合密臀性色| 99久久无色码中文字幕人妻| 久久99九九国产免费看小说| 噜噜噜色噜噜噜久久| 久久久www免费人成精品| 久久综合亚洲色HEZYO社区| 久久久久久久97| 欧美亚洲色综久久精品国产|