• <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 日需博客 閱讀(697) 評論(0)  編輯 收藏 引用 所屬分類: C C++轉載
            亚洲国产成人久久精品99 | 无遮挡粉嫩小泬久久久久久久| 综合久久给合久久狠狠狠97色| 久久久久久国产精品美女| 91精品国产高清91久久久久久| 婷婷久久综合九色综合绿巨人| 久久久久人妻精品一区二区三区 | 久久精品18| 成人免费网站久久久| 蜜桃麻豆www久久国产精品| 精品久久久久久成人AV| 少妇人妻综合久久中文字幕| 久久99久久成人免费播放| 久久精品国产色蜜蜜麻豆| 狠狠久久综合| 99久久精品免费国产大片| 蜜臀av性久久久久蜜臀aⅴ| 日韩久久无码免费毛片软件| 久久久青草青青亚洲国产免观| 亚洲日韩中文无码久久| 青青草原综合久久大伊人| 国产精品综合久久第一页| 久久最近最新中文字幕大全| 日本人妻丰满熟妇久久久久久| 欧美精品国产综合久久| 欧美成a人片免费看久久| 国产精品无码久久久久 | 久久久久久国产a免费观看黄色大片 | 国产精品久久久久免费a∨| 久久久久久亚洲精品不卡| 亚洲国产精品婷婷久久| 中文字幕亚洲综合久久| 日本精品久久久久中文字幕| 国内精品久久久久久野外| A狠狠久久蜜臀婷色中文网| 成人国内精品久久久久一区| 国内高清久久久久久| 无码人妻少妇久久中文字幕蜜桃 | 久久99精品久久久久久动态图| 久久Av无码精品人妻系列| 久久精品国产清高在天天线|