标签:des style blog http color 使用
本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie
关联条款 Item 36
接口继承和实现继承不同。在 public 继承下, derived classes 总是继承 base class 的接口
class Shape{ public: virtual void draw() const = 0; virtual void error(const std::string &msg); int objectID() const; //... }; class Rectangle: public Shape{...}; class Ellipse: public Shape{...};
class AirPort{...}; class AirPlane{ public: virtual void fly(const AirPort &destination); //... }; void AirPort::fly(const AirPort &destination){ //缺省代码,将飞机飞至指定的目的地 } class ModelA: public AirPlane {...}; class ModelB: public AirPlane {...}; class ModelC: public AirPlane { ... // 未声明 fly 函数, 但C型飞机的飞行方式与A,B不同。 }; AirPort PDX(...); AirPlane *pa = new ModelC; //... pa->fly(PDX); //调用 AirPlane::fly
class AirPlane{ public: virtual void fly(const AirPort &destination) = 0; //... protected: // protected 因为客户端不需要在意飞机能不能飞 void defaultFly(const AirPort &destination); //Item 36 }; void AirPlane::defaultFly(const AirPlane &destination){ //缺省代码,将飞机飞至指定的目的地 } class ModelA: public AirPlane{ public: virtual void fly(const AirPort &destination){ defaultFly(destination); } //... }; class ModelB: public AirPlane{ public: virtual void fly(const AirPort &destination){ defaultFly(destination); } //... }; class ModelC: public AirPlane{ public: virtual void fly(const AirPort &destination); //... }; void ModelC::fly(const AirPort &destination){ // pure virtual 函数迫使 ModelC 必须提供自己的 fly 版本 //... }
class AirPlane{ public: virtual void fly(const AirPort &destination) = 0; //... protected: // protected 因为客户端不需要在意飞机能不能飞 void defaultFly(const AirPort &destination); //Item 36 }; void AirPlane::fly(const AirPlane &destination){ //pure virtual 函数实现 //缺省代码,将飞机飞至指定的目的地 } class ModelA: public AirPlane{ public: virtual void fly(const AirPort &destination){ AirPlane::fly(destination); } //... }; class ModelB: public AirPlane{ public: virtual void fly(const AirPort &destination){ AirPlane::fly(destination); } //... }; class ModelC: public AirPlane{ public: virtual void fly(const AirPort &destination); //... }; void ModelC::fly(const AirPort &destination){ // pure virtual 函数迫使 ModelC 必须提供自己的 fly 版本 //... }
Effective C++ Item 34 区分接口继承与实现继承,布布扣,bubuko.com
Effective C++ Item 34 区分接口继承与实现继承
标签:des style blog http color 使用
原文地址:http://blog.csdn.net/zhengsenlie/article/details/37723787