• <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)載
            国产精品激情综合久久| 国产精品美女久久福利网站| 久久―日本道色综合久久| 婷婷综合久久狠狠色99h| 国产A级毛片久久久精品毛片| 久久久久久曰本AV免费免费| 国产高潮国产高潮久久久| 欧美午夜A∨大片久久 | 久久久久久国产a免费观看黄色大片 | 精品视频久久久久| 国色天香久久久久久久小说| 欧美精品一区二区精品久久| 久久久噜噜噜久久中文字幕色伊伊| 久久精品9988| 狼狼综合久久久久综合网| 国产成人精品久久| 99国产精品久久| 日本欧美久久久久免费播放网| 久久国产精品视频| 久久99国产精品久久| 久久久久AV综合网成人| 亚洲愉拍99热成人精品热久久 | 久久久久久狠狠丁香| 国内精品人妻无码久久久影院| 久久久久高潮综合影院| 亚洲色欲久久久久综合网| 久久久WWW成人| 国产精品午夜久久| 久久精品成人免费观看97| 精品国产综合区久久久久久 | 97久久精品人人做人人爽| 久久久久亚洲av无码专区| 久久人妻少妇嫩草AV无码专区| 久久99九九国产免费看小说| 亚洲а∨天堂久久精品| 欧美国产精品久久高清| 欧美激情精品久久久久久久九九九 | 久久99久国产麻精品66| 久久妇女高潮几次MBA| 久久精品亚洲精品国产色婷| 99久久久精品免费观看国产|