新聞中心

        EEPW首頁 > 嵌入式系統(tǒng) > 設(shè)計應(yīng)用 > C++子類覆蓋基類及virtual虛擬處理

        C++子類覆蓋基類及virtual虛擬處理

        作者: 時間:2016-12-01 來源:網(wǎng)絡(luò) 收藏
        #include
        class animal
        {
        public:
        animal()
        {
        cout<<"animal construct"<
        }
        ~animal()
        {
        cout<<"construct animal"<
        }
        virtual void breath() //virtual定義虛函數(shù)
        {
        cout<<"bubble2"<
        }
        };
        class fish:public animal //只調(diào)用animal中的animal()
        {
        public:
        fish()
        {
        // cout<<"hello world"<
        }
        ~fish()
        {
        // cout<<"construct helllo world"<
        }
        void breath()
        {
        //animal::breath();
        //cout<<"fish bubble"<
        }
        };
        void fn(animal *pan)
        {
        pan->breath();
        }
        void main()
        {
        fish fh;
        //st.breath ();//如果子類和基類有兩個同名函數(shù),則最后用子類覆蓋基類,這叫重載。
        animal *pan;
        pan=&fh; //改變指針,把a(bǔ)nimal的指針用fish替換了。但是還是輸出animal。若在animal前加virtual虛擬,則輸出fish
        fn(pan);
        }



        評論


        技術(shù)專區(qū)

        關(guān)閉
        主站蜘蛛池模板: 贵阳市| 孟村| 桂东县| 洪洞县| 桑日县| 商水县| 尖扎县| 洞口县| 呈贡县| 鸡泽县| 鄂托克前旗| 安徽省| 宿松县| 洛南县| 息烽县| 大埔县| 南召县| 延吉市| 上蔡县| 泗阳县| 太仆寺旗| 伊宁县| 罗平县| 都昌县| 高阳县| 太白县| 滕州市| 额尔古纳市| 塘沽区| 微山县| 额敏县| 沂水县| 丰原市| 二连浩特市| 策勒县| 浙江省| 句容市| 武鸣县| 永城市| 温宿县| 门头沟区|