美文网首页
Effective C++ Chapter6-继承与面向对象设计

Effective C++ Chapter6-继承与面向对象设计

作者: 寒冰豌豆 | 来源:发表于2017-04-21 11:02 被阅读0次
    32、确定你的public继承塑模出is-a关系
    • “public继承”意味is-a。适用于base class身上的每一件事情一定也适用于derived class身上,因为每一个derived class对象也都是一个base class对象。
    33、避免遮掩继承而来的名称
    • derived class 作用域被嵌套在base class作用域内。


      0.png
    ![image.VI9LXY.png](https://img.haomeiwen.com/i4602306/88bcfbefc4caac36.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
    34、
    35、考虑virtual函数以外的其他选择
    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;
    }
    

    相关文章

      网友评论

          本文标题:Effective C++ Chapter6-继承与面向对象设计

          本文链接:https://www.haomeiwen.com/subject/xfgonttx.html