• <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++轉載
            久久天天躁狠狠躁夜夜96流白浆| 久久免费的精品国产V∧| 国产精品一久久香蕉国产线看观看| 东方aⅴ免费观看久久av | 国产精品99久久精品爆乳| 久久精品国产一区二区三区| 亚洲欧美一区二区三区久久| 久久国产热精品波多野结衣AV| 99热精品久久只有精品| 国产69精品久久久久久人妻精品| 99久久免费国产特黄| 一级女性全黄久久生活片免费| 国产精品毛片久久久久久久| 色诱久久av| 亚洲国产精品久久久久网站| 亚洲午夜久久久影院| 亚洲国产天堂久久久久久| 99国产欧美久久久精品蜜芽| 热久久视久久精品18| 伊人久久综在合线亚洲2019| 热re99久久精品国99热| 色欲综合久久躁天天躁| 国产精品成人99久久久久91gav| 中文字幕无码免费久久| 开心久久婷婷综合中文字幕| 91久久九九无码成人网站| 粉嫩小泬无遮挡久久久久久| 久久强奷乱码老熟女网站| 久久久久亚洲AV综合波多野结衣 | 久久国产三级无码一区二区| 久久婷婷五月综合97色| 亚洲va中文字幕无码久久| 性做久久久久久久久浪潮| 欧美性猛交xxxx免费看久久久| 精品久久久久中文字幕一区| 伊人色综合久久| 国产精品美女久久久免费 | 久久精品久久久久观看99水蜜桃| 久久综合九色欧美综合狠狠| 久久久久国色AV免费观看| 亚洲七七久久精品中文国产 |