Effective C++ Item 32 确保你的 public 继承模子里出来 is-a 关联
本文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不是学生
解析:上面的样例仅仅对 public继承才成立。 对private, protected 继承不成立。
public 继承的问题演示样例
1.企鹅是一种鸟。鸟会飞,但企鹅不会飞
class Bird{
public:
virtual void fly(); //鸟能够飞
};
class Penguin: public Bird{ //企鹅是一种鸟
};
假设你的软件系统如今以及未来不须要差别会不会飞的鸟的话。
这种“双classes继承体系”就能够了,否则能够进行例如以下更改
class Bird{
//... -->没有声明 fly 函数,鸟不一定会飞
};
class FlyingBird: public Bird{
public:
virtual void fly();
};
class Penguin: public Bird{
//... -->没有声明 fly 函数
};
2.class Square 应该以 public 形式继承 class Rectangle 吗?
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 关联的更多相关文章
- 读书笔记 effective c++ Item 32 确保public继承建立“is-a”模型
1. 何为public继承的”is-a”关系 在C++面向对象准则中最重要的准则是:public继承意味着“is-a”.记住这个准则. 如果你实现一个类D(derived)public继承自类B(ba ...
- Effective C++ 笔记:条款 32 确定你的public继承塑造出正确的is-a关系
32 : Make sure public inheritance models "is-a." 0 引言 Inheritance and Object-Oriented Desi ...
- 读书笔记 effective c++ Item 4 确保对象被使用前进行初始化
C++在对象的初始化上是变化无常的,例如看下面的例子: int x; 在一些上下文中,x保证会被初始化成0,在其他一些情况下却不能够保证.看下面的例子: class Point { int x,y; ...
- 条款32:确定你的public继承塑模出is-a的关系
Make sure public inheritance models "is –a " 如果令clsss D 以public的形式继承class B,你便是告诉编译器说,每一个类 ...
- effective C++ Item 33 避免隐藏继承而来的名字
1. 普通作用域中的隐藏 名字实际上和继承没有关系.有关系的是作用域.我们都知道像下面的代码: int x; // global variable void someFunc() { double x ...
- 读书笔记 effective c++ Item 38 通过组合(composition)为 “has-a”或者“is-implemented-in-terms-of”建模
1. 什么是组合(composition)? 组合(composition)是一种类型之间的关系,这种关系当一种类型的对象包含另外一种类型的对象时就会产生.举个例子: class Address { ...
- 读书笔记 effective c++ Item 39 明智而谨慎的使用private继承
1. private 继承介绍 Item 32表明C++把public继承当作”is-a”关系来对待.考虑一个继承体系,一个类Student public 继承自类Person,如果一个函数的成功调用 ...
- 读书笔记 effective C++ Item 33 避免隐藏继承而来的名字
1. 普通作用域中的隐藏 名字实际上和继承没有关系.有关系的是作用域.我们都知道像下面的代码: int x; // global variable void someFunc() { double x ...
- Effective C++ -----条款32:确定你的public继承塑模出is-a关系
“public继承”意味is-a.适用于base classes身上的每一件事情一定也适用于derived classes身上,因为每一个derive class对象也都是一个base class对象 ...
随机推荐
- hdu-1016素数环
这个题就是给出一个数字n,表示有n个数.编号为1~n. 然后要求我们将这n个数连起来变成一个环,要求随意两个数相加所得值必须为素数. 假设满足条件就将这个环输出来! 这个题:dfs+回溯+推断.然后注 ...
- Graphical Shell with WebShell - WebOS Internals
Graphical Shell with WebShell - WebOS Internals Graphical Shell with WebShell From WebOS Internals J ...
- Enum实现单例模式
package com.wjy.effective; public enum Singleton { INSTANCE; private int numa; private int numb; pub ...
- POJ 2676/2918 数独(dfs)
思路:记录每行每列每一个宫已经出现的数字就可以.数据比較弱 另外POJ 3074 3076 必须用剪枝策略.但实现较麻烦,还是以后学了DLX再来做吧 //Accepted 160K 0MS #incl ...
- 怎样在Linux下通过ldapsearch查询活动文件夹的内容
从Win2000開始.微软抛弃NT域而採用活动文件夹来管理Windows域.而活动文件夹就是微软基于遵守LDAP协议的文件夹服务.假设用扫描器扫描的话能够发现活动文件夹的389port是打开的.并且微 ...
- ActivityGroup简单介绍
ActivityGroup简单介绍 1.ActivityGroup的核心就是继承了该类,可以通过getLocalActivityManager()得到一个LocalActivityManager 如, ...
- vs2008+opencv2.4.9 +win7X64位系统 2.
小编用自身血淋淋的例子,来给大家做个参考,共耗时近2天时间,终于屈服于安装vs2010,然后配置成功了.但是在这个配置成功后,我终于发现了我08配置不成功的原因,写下心得,供各位参考 1.准备工具 v ...
- Determining IP information for eth1... failed; no link present. Check cable? 解决办法
有时候会遇到这种问题 解决办法为 进入网卡配置,将 BOOTPROTO 改为 none 然后 ifconfig –a 查看可以得到 eth1 已经可以寻到 iP 地址,如下: Service netw ...
- 网络安全之IP伪造
眼下非常多站点的涉及存在一些安全漏洞,黑客easy使用ip伪造.session劫持.xss攻击.session注入等手段危害站点安全.在纪录片<互联网之子>(建议搞IT的都要看下)中.亚伦 ...
- HttpClient(联网)
抽样: void GameRequest::initRequset(const char* url, cocos2d::CCObject* pTarget, cocos2d::SEL_CallFunc ...