• <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

            繼承層次中的輸入輸出運算符重載

            class A
            {
            };

            class B : public A
            {
            };

            方案一
            可對 A 進行重載輸入輸出運算符,然后也對 B 重載相應版本的輸入輸出運算符

            方案二
            只對 A 進行重載輸入輸出運算符,對 A 定義一個 virtual print 函數,在 B 中也對該 virtual 函數 override
            在重載的輸入輸出運算符中調用 virtual print 函數

             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 閱讀(242) 評論(0)  編輯 收藏 引用
            久久国产精品波多野结衣AV| 一本色道久久综合亚洲精品| 久久综合狠狠综合久久激情 | 色综合久久中文综合网| 亚洲国产成人久久综合一区77| 久久一日本道色综合久久| 色成年激情久久综合| 亚洲精品无码久久久久去q | 无码8090精品久久一区| 亚洲午夜无码久久久久| 久久99精品国产麻豆婷婷| 无码人妻少妇久久中文字幕蜜桃 | 99久久精品费精品国产一区二区 | 人妻无码αv中文字幕久久琪琪布 人妻无码久久一区二区三区免费 人妻无码中文久久久久专区 | 久久久青草青青亚洲国产免观| 日韩人妻无码一区二区三区久久99| 99久久免费国产特黄| 日本精品久久久久影院日本| 久久国产乱子伦精品免费强| 久久精品无码一区二区WWW| 久久久久亚洲AV综合波多野结衣 | 午夜视频久久久久一区 | 久久久这里有精品| 狠狠色伊人久久精品综合网| 狠狠色噜噜狠狠狠狠狠色综合久久| 久久人人爽人人爽人人片av麻烦| 国产精品亚洲美女久久久| 久久久精品午夜免费不卡| 国产欧美一区二区久久| 久久婷婷国产麻豆91天堂| 久久精品国产99国产电影网| 韩国无遮挡三级久久| 成人国内精品久久久久影院| 精品无码久久久久国产| 999久久久无码国产精品| 久久午夜伦鲁片免费无码| 久久久噜噜噜www成人网| 久久r热这里有精品视频| 久久精品视频网| 久久久精品人妻无码专区不卡 | 久久久久久免费一区二区三区 |