• <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>
            posts - 183,  comments - 10,  trackbacks - 0

            繼承層次中的輸入輸出運(yùn)算符重載

            class A
            {
            };

            class B : public A
            {
            };

            方案一
            可對(duì) A 進(jìn)行重載輸入輸出運(yùn)算符,然后也對(duì) B 重載相應(yīng)版本的輸入輸出運(yùn)算符

            方案二
            只對(duì) A 進(jìn)行重載輸入輸出運(yùn)算符,對(duì) A 定義一個(gè) virtual print 函數(shù),在 B 中也對(duì)該 virtual 函數(shù) override
            在重載的輸入輸出運(yùn)算符中調(diào)用 virtual print 函數(shù)

             1 #include <iostream>
             2 #include <string>
             3 using namespace std;
             4 
             5 class Complex
             6 {
             7 protected:
             8 // private:
             9     int nR;
            10     int nI;
            11 public:
            12     Complex(int tmpR = -100int tmpI = -100);
            13     virtual void display();
            14     virtual ostream& print(ostream& os) const;
            15     friend ostream& operator << (ostream&const Complex&);
            16 };
            17 
            18 class NameComplex : public Complex
            19 {
            20 private:
            21     string strName;
            22 public:
            23     NameComplex(const string& = "abc"int = -100int = -100);
            24     virtual void display();
            25     virtual ostream& print(ostream& os) const;
            26     // friend ostream& operator << (ostream&, const NameComplex&);
            27 };
            28 
            29 Complex::Complex(int tmpR, int tmpI) : nR(tmpR), nI(tmpI) {}
            30 
            31 void Complex::display()
            32 {
            33     cout << nR << endl;
            34     cout << nI << endl;
            35 }
            36 
            37 ostream& Complex::print(ostream& os) const
            38 {
            39     os << nR << ' ' << nI;
            40     return os;
            41 }
            42 
            43 /*
            44 ostream& operator << (ostream& os, const Complex& rhs)
            45 {
            46     os << rhs.nR << ' ' << rhs.nI;
            47     return os;
            48 }
            49 */
            50 
            51 ostream& operator << (ostream& os, const Complex& rhs)
            52 {
            53     return rhs.print(os);
            54 }
            55 
            56 NameComplex::NameComplex(const string& str, int nR, int nI) : Complex(nR, nI), strName(str) {}
            57 
            58 void NameComplex::display()
            59 {
            60     cout << strName << endl;
            61     Complex::display();
            62 }
            63 
            64 /*
            65 ostream& operator << (ostream& os, const NameComplex& rhs)
            66 {
            67     os << rhs.strName << ' ';
            68     operator << (os, static_cast<Complex>(rhs));
            69     return os;
            70 }
            71 */
            72 
            73 ostream& NameComplex::print(ostream& os) const
            74 {
            75     os << strName << ' ';
            76     return Complex::print(os);
            77 }
            78 
            79 int main()
            80 {
            81     Complex a;
            82     cout << a << endl;
            83     
            84     NameComplex b;
            85     cout << b << endl;
            86 }

            http://topic.csdn.net/u/20110627/22/c4c0b809-13f4-482d-aa26-5faf5c1fc0f0.html?54375

            posted on 2011-06-27 23:44 unixfy 閱讀(241) 評(píng)論(0)  編輯 收藏 引用

            只有注冊(cè)用戶登錄后才能發(fā)表評(píng)論。
            網(wǎng)站導(dǎo)航: 博客園   IT新聞   BlogJava   博問(wèn)   Chat2DB   管理


            中文成人久久久久影院免费观看 | 久久久久99精品成人片试看 | 精品多毛少妇人妻AV免费久久| 99精品久久精品| 久久久久久国产精品美女| 国产农村妇女毛片精品久久| 91久久九九无码成人网站| 久久午夜伦鲁片免费无码| 精品国产乱码久久久久软件| 久久精品国产亚洲av麻豆蜜芽| 国产一区二区久久久| 四虎国产精品成人免费久久| 久久精品国产2020| 亚洲中文字幕久久精品无码喷水| 中文字幕久久波多野结衣av| 久久99精品久久久大学生| 色妞色综合久久夜夜| 国产亚洲欧美成人久久片| 久久青青草原国产精品免费 | 国产精品日韩深夜福利久久 | 久久伊人五月丁香狠狠色| 99久久精品免费看国产一区二区三区| 久久久精品久久久久影院| 狠狠色婷婷久久一区二区| av午夜福利一片免费看久久 | 久久婷婷五月综合97色一本一本| 精品久久8x国产免费观看| 伊人色综合久久| 77777亚洲午夜久久多喷| 国产精品久久亚洲不卡动漫| 国产成人AV综合久久| 久久久这里只有精品加勒比| 国产69精品久久久久777| 久久99精品久久久久久水蜜桃 | 国产69精品久久久久观看软件| 久久夜色精品国产噜噜亚洲AV| 亚洲国产精品人久久| 国内精品久久久久影院薰衣草 | 久久精品9988| 伊人久久大香线蕉av不变影院| 中文字幕亚洲综合久久|