标签:style blog http color width 2014
本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie
经验:"public继承"意味 is-a。适用于 base classes 身上的每一件事情一定也适用于 derived classes 身上,
因为每一个 derived classes 身上,因为每一个 derived class 对象也都是一个 base class 对象。
示例:
class Person {...};
class Student: public Person {...};
void eat(const Person &p); //任何人都会吃
void study(const Student &s); //只有学生才到校学习
Person p; //p 是人
Student s; //s 是学生
eat(p); //ok p是人
eat(s); //ok s是学生,但学生也是(is-a)人
study(s); //ok s是学生
study(p); //error p不是学生class Bird{
public:
virtual void fly(); //鸟可以飞
};
class Penguin: public Bird{ //企鹅是一种鸟
};class Bird{
//... -->没有声明 fly 函数,鸟不一定会飞
};
class FlyingBird: public Bird{
public:
virtual void fly();
};
class Penguin: public Bird{
//... -->没有声明 fly 函数
};class Rectangle{
virtual void setHeight(int newHeight);
virtual void setWidth(int newWidth);
virtual int height() const; //返回当前值
virtual int width() const;
//...
};
void makeBigger(Rectangle &r) //这个函数用以增加 r 的面积
{
int oldHeight = r.height();
r.setWidth(r.width() + 10); //为 r 的宽度加10
assert(r.height() == oldHeight); //判断 r 的高度是否未曾改变
}
class Square: public Rectangle {...};
Square s;
//...
assert(s.width() == s.height()); //这对所有正方形一定为真
makeBigger(s); //由于s 是一种(is-a)矩形,所以我们增加其面积。
assert(s.width() == s.height()); //对所有正方形应该仍然为真 --> 但事实上由于上一步 makeBigger函数的作用,这里已经不为真了Effective C++ Item 32 确定你的 public 继承塑模出 is-a 关系,布布扣,bubuko.com
Effective C++ Item 32 确定你的 public 继承塑模出 is-a 关系
标签:style blog http color width 2014
原文地址:http://blog.csdn.net/zhengsenlie/article/details/37689765