• <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>
            流量統(tǒng)計(jì):
            Rixu Blog (日需博客)
            日需博客,每日必需來(lái)踩踩哦..
            posts - 108,comments - 54,trackbacks - 0
            以下示例中定義了一個(gè)class test, 重載了<, +, +=, =, ==, <<, >>等符號(hào):

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

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

               /*以下重載雙等號(hào) == */  
                 //對(duì)象==對(duì)象 
                 bool operator==(const test &t1)const{
                     return (v == t1.v);
                 }  
                 //對(duì)象==int
                 bool operator==(const int &t1)const{
                     return (v == t1);
                 }  
                
               /*以下重載 輸入>> 輸出<< */
                 /*友元函數(shù),輸出對(duì)象*/
                 friend inline ostream & operator << (ostream & os, test &t1){
                     cout << "class t(" << t1.v << ")" << endl;
                     return os;
                 }
                 /*友元函數(shù),輸入對(duì)象*/
                 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    
            本文版權(quán)歸作者和博客園共有,歡迎轉(zhuǎn)載,但未經(jīng)作者同意必須保留此段聲明,且在文章頁(yè)面明顯位置給出原文連接,否則保留追究法律責(zé)任的權(quán)利。
            posted on 2011-08-04 14:35 日需博客 閱讀(702) 評(píng)論(0)  編輯 收藏 引用 所屬分類(lèi): C C++轉(zhuǎn)載
            九九久久精品无码专区| 国产精品久久久天天影视香蕉| 亚洲欧美伊人久久综合一区二区| 亚洲中文字幕无码久久精品1| 国产午夜精品理论片久久影视 | 91麻豆精品国产91久久久久久 | 国产女人aaa级久久久级| 成人午夜精品无码区久久| 情人伊人久久综合亚洲| 一本久久知道综合久久| 一本色道久久88综合日韩精品| 国产精品美女久久久久网| 一本色道久久99一综合| 一本一本久久a久久精品综合麻豆| 麻豆一区二区99久久久久| 97视频久久久| 久久精品国产一区二区电影| 成人久久综合网| 日韩精品久久久久久免费| 亚洲午夜久久久久妓女影院| 亚洲成av人片不卡无码久久| 国产精品久久久久一区二区三区| 狠狠色丁香久久综合五月| 一本色道久久88精品综合| 精品熟女少妇AV免费久久 | 久久青青色综合| 久久无码人妻精品一区二区三区| 品成人欧美大片久久国产欧美| 狠狠干狠狠久久| 日韩亚洲欧美久久久www综合网 | 久久久久国产| 久久精品这里只有精99品| 久久久久噜噜噜亚洲熟女综合| 久久国产精品偷99| 亚洲国产精品一区二区三区久久| 久久久无码精品午夜| 久久亚洲AV无码西西人体| 一本综合久久国产二区| 午夜天堂精品久久久久| 久久综合88熟人妻| 亚洲狠狠久久综合一区77777|