Effective C++ Chapter6-繼承與面向?qū)ο笤O(shè)計(jì)

32、確定你的public繼承塑模出is-a關(guān)系
  • “public繼承”意味is-a。適用于base class身上的每一件事情一定也適用于derived class身上,因?yàn)槊恳粋€(gè)derived class對(duì)象也都是一個(gè)base class對(duì)象。
33、避免遮掩繼承而來(lái)的名稱
  • derived class 作用域被嵌套在base class作用域內(nèi)。


    0.png
![image.VI9LXY.png](http://upload-images.jianshu.io/upload_images/4602306-88bcfbefc4caac36.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
34、
35、考慮virtual函數(shù)以外的其他選擇
36、
#include <iostream>
using namespace std;

class Shape{
public:
    enum ShapedColor { Red=0,Green,Blue };
    virtual void draw(ShapedColor color = Red)const= 0 ;
    
};

class  Rectangle:public Shape{
public:
    
    void draw(ShapedColor color=Red )const;

};
void Rectangle::draw(ShapedColor color )const
{
    cout << "Recangle class: ";
    cout << color<<endl;
}

int main()
{
    Rectangle r ;
    r.draw();
    Shape*pr = &r;
    pr->draw();
    return 0;
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容