C++面向对象高级编程(下)-Geekband
11, 组合和继承
一, Composition 复合 has-a的关系
二,Delegation 委托 Composition by reference( 这里其实包含指针和引用两种情况 )
三, 继承 Is-a
12, 虚函数与多态
继承的是调用权而非功能 - 关键句
非虚: 不希望子类override
虚: 希望子类override , 但自己已有定义
纯虚: 子类一定要定义它
虚函数重要用途:
子类对象调用父类非虚函数funA(), 而funA()中调用了虚函数funB(), 而子类override了funB(), 那么funA中将调用子类的funB()而不是父类的funB.
即funA()中的关键功能推迟实现 - Template Method
这里的funA称之为应用框架.
那么这里的子类funB是如何被调用起来的?
答:
class CMyDoc: public CDocument
myDoc.Open() ->CDocument::Open(&this)
这里显示出了this指针是多么的神奇
13, 委托相关设计
如上视图中, 在一个文件夹中有文件夹也有文件, 而文件夹中的文件夹很可能也有文件夹和文件....
这里就需要一个类创建的对象中,既能保存一个文件夹,也能保存一个文件;而其中的文件夹还能保存文件及文件夹.
所以, 用到委托+继承来解决这个问题:
设:
class Primitive代表文件
class Composite代表文件夹 ,文件夹中肯定是可以放文件夹和文件的.
所以我们在类Composite中有 vector<Composite*> 和vector <Primitive*>
那当然也有函数 add(Composite )和add(Primitive)分别用来保存文件夹中含有的文件夹和文件.
有没有发现很麻烦? 如果我们能够只用一个vector和一个add是不是会更省事?
此时,我们使用 继承
创建基类:
class Component
{
//..
public:
virtrual void add(Component*) { } //这里不能为纯虚函数 . 你猜 , 为什么?
};
而另外两个类应该继承Component:
class Primitive : public Component
{
//..
};
class Composite : public Component
{
vector<Compenent*> c;
public:
void add(Component* elem){
c.push_back(elem);
}
//....
};
继承+委托
Prototype设计模式
让父类管理未来的子类,在父类还不知道子类类名的时候就可以进行管理了. 这里的管理指, 所有从本父类派生出去的子类对象都能够通过父类名称来得到.
如果要通过父类名称来获得子类的对象, 那么我们就需要:
static Image *_prototypes[10];
static int _nextSlot;
这样就可以用于保存子类的对象. 别忘了在类声明外部,定义这些静态变量哟! ~
那我们如何将子类的对象保存到这个数组中? 那我们需要子类调用一个函数,把自己给放进来:
static void addPrototype(Image *image)
{
_prototypes[_nextSlot++] = image;
}
子类如何主动触发去调用addPrototype这个函数呢? 我们的目的就是不让用户根据子类名称去创建对象,而是通过父类.
所以前提条件就是:
子类默认构造函数为私有:
private:
LandSatImage()
{
addPrototype(this);
}
那如何创建这个"this"对象呢? 那么我就应该在子类内声明一个本类的成员变量,并且为静态:
// This is only called when the private static data member is inited
static LandSatImage _landSatImage;
这样在程序运行起来的时候就会创建.
别忘了在类外定义该静态对象:
// Register the subclass's prototype
LandSatImage LandSatImage::_landSatImage;
此时就会调用构造函数, 并且将自己放到父类的静态数组中.
到此我们已经在父类中拥有了这个LandSatImage子类的对象.如果有多个不同子类那么就会加进来更过不同的子类对象.
那如何通过父类来获得特定子类的一个对象?而且我们是不知道子类的名称的, 所以这个创建新子类对象的实现办法只能让子类自己去完成:
在子类中:
Image *clone()
{
return new LandSatImage(1); //new有没有什么问题?为什么是1
}
在父类中如何才能得到该特定的子类呢? 父类中的实现办法:
Image *Image::findAndClone(imageType type)
{
for (int i = 0; i < _nextSlot; i++)
if (_prototypes[i]->returnType() == type)
return _prototypes[i]->clone();
}
此时的type就是用户自己设定的子类类型. 是子类编写用户去添加的枚举类型, 父类只负责判断是否相等就行,这样就将需要判断的类型定义也推迟到后期实现.
枚举内加入新子类的类型:
enum imageType
{
LSAT, SPOT
};
其实上的子类clone中的new是有玄机的. 你想想 如果 new又调用了默认构造函数的话,又把自己这种类型的子类addPrototype到父类的静态数组中,这就不能保证数组中单一子类型的唯一性了, 所以我们需要写一个新的构造函数:
protected:
// This is only called from clone()
LandSatImage(int dummy)
{
_id = _count++;
}
这里为了和默认构造函数区分开,我们多个int参数.int并没有用,所以在new LandSatImage(1)的1是随便写的.
完整代码:
#include <iostream.h> //
enum imageType
{
LSAT, SPOT
}; class Image
{
public:
virtual void draw() = 0;
static Image *findAndClone(imageType);
protected:
virtual imageType returnType() = 0;
virtual Image *clone() = 0;
// As each subclass of Image is declared, it registers its prototype
static void addPrototype(Image *image)
{
_prototypes[_nextSlot++] = image;
}
private:
// addPrototype() saves each registered prototype here
static Image *_prototypes[10];
static int _nextSlot;
}; Image *Image::_prototypes[];
int Image::_nextSlot; // Client calls this public static member function when it needs an instance
// of an Image subclass
Image *Image::findAndClone(imageType type)
{
for (int i = 0; i < _nextSlot; i++)
if (_prototypes[i]->returnType() == type)
return _prototypes[i]->clone();
} class LandSatImage: public Image
{
public:
imageType returnType()
{
return LSAT;
}
void draw()
{
cout << "LandSatImage::draw " << _id << endl;
}
// When clone() is called, call the one-argument ctor with a dummy arg
Image *clone()
{
return new LandSatImage(1);
}
protected:
// This is only called from clone()
LandSatImage(int dummy)
{
_id = _count++;
}
private:
// Mechanism for initializing an Image subclass - this causes the
// default ctor to be called, which registers the subclass's prototype
static LandSatImage _landSatImage;
// This is only called when the private static data member is inited
LandSatImage()
{
addPrototype(this);
}
// Nominal "state" per instance mechanism
int _id;
static int _count;
}; // Register the subclass's prototype
LandSatImage LandSatImage::_landSatImage;
// Initialize the "state" per instance mechanism
int LandSatImage::_count = 1; class SpotImage: public Image
{
public:
imageType returnType()
{
return SPOT;
}
void draw()
{
cout << "SpotImage::draw " << _id << endl;
}
Image *clone()
{
return new SpotImage(1);
}
protected:
SpotImage(int dummy)
{
_id = _count++;
}
private:
SpotImage()
{
addPrototype(this);
}
static SpotImage _spotImage;
int _id;
static int _count;
}; SpotImage SpotImage::_spotImage;
int SpotImage::_count = 1; // Simulated stream of creation requests
const int NUM_IMAGES = 8;
imageType input[NUM_IMAGES] =
{
LSAT, LSAT, LSAT, SPOT, LSAT, SPOT, SPOT, LSAT
}; int main()
{
Image *images[NUM_IMAGES]; // Given an image type, find the right prototype, and return a clone
for (int i = 0; i < NUM_IMAGES; i++)
images[i] = Image::findAndClone(input[i]); // Demonstrate that correct image objects have been cloned
for (i = 0; i < NUM_IMAGES; i++)
images[i]->draw(); // Free the dynamic memory
for (i = 0; i < NUM_IMAGES; i++)
delete images[i];
}
彩蛋:
C++面向对象高级编程(下)-Geekband的更多相关文章
- C++面向对象高级编程(上)-Geekband
头文件和类声明 一定要注意使用防卫式的头文件声明: #ifndef _CLASSHEAD_ #define _CLASSHEAD_ . . . . #endif 基于对象和面向对象 : 基于对象 单一 ...
- C++面向对象高级编程(五)类与类之间的关系
技术在于交流.沟通,转载请注明出处并保持作品的完整性. 本节主要介绍一下类与类之间的关系,也就是面向对象编程先介绍两个术语 Object Oriented Programming OOP面向对象编 ...
- C++面向对象高级编程(三)基础篇
技术在于交流.沟通,转载请注明出处并保持作品的完整性. 概要 一.拷贝构造 二.拷贝赋值 三.重写操作符 四.生命周期 本节主要介绍 Big Three 即析构函数,拷贝构造函数,赋值拷贝函数,前面主 ...
- C++面向对象高级编程(一)基础篇
技术在于交流.沟通,转载请注明出处并保持作品的完整性. 概要: 知识点1 构造函数与析构函数 知识点2 参数与返回值 知识点3 const 知识点4 函数重载(要与重写区分开) 知识点5 友元 先以C ...
- Java面向对象 网络编程 下
Java面向对象 网络编程 下 知识概要: (1)Tcp 练习 (2)客户端向服务端上传一个图片. (3) 请求登陆 (4)url 需求:上传图片. 客户端: ...
- C++面向对象高级编程(九)Reference与重载operator new和operator delete
摘要: 技术在于交流.沟通,转载请注明出处并保持作品的完整性. 一 Reference 引用:之前提及过,他的主要作用就是取别名,与指针很相似,实现也是基于指针. 1.引用必须有初值,且不能引用nul ...
- C++面向对象高级编程(八)模板
技术在于交流.沟通,转载请注明出处并保持作品的完整性. 这节课主要讲模板的使用,之前我们谈到过函数模板与类模板 (C++面向对象高级编程(四)基础篇)这里不再说明 1.成员模板 成员模板:参数为tem ...
- C++面向对象高级编程(七)point-like classes和function-like classes
技术在于交流.沟通,转载请注明出处并保持作品的完整性. 1.pointer-like class 类设计成指针那样,可以当做指针来用,指针有两个常用操作符(*和->),所以我们必须重载这两个操作 ...
- C++面向对象高级编程(六)转换函数与non-explicit one argument ctor
技术在于交流.沟通,转载请注明出处并保持作品的完整性. 1.conversion function 转换函数 //1.转换函数 //conversion function //只要你认为合理 你可以任 ...
- C++面向对象高级编程(四)基础篇
技术在于交流.沟通,转载请注明出处并保持作品的完整性. 一.Static 二.模板类和模板函数 三.namespace 一.Static 静态成员是“类级别”的,也就是它和类的地位等同,而普通成员是“ ...
随机推荐
- Linux 常用命令:解压缩篇
前言 Linux常用命令中,有很多用于对文件的压缩或解压,本文将介绍这些解压缩命令中不常见却非常实用的用法. tar tar是linux中最常用的解压缩命令.tar命令可用于处理后缀名为tar,tar ...
- RAKsmart新出香港服务器的优势
RAKsmart为了更好地服务用户,所以最近RAKsmart新推出得香港服务器又带给了用户更多的选择,那这次RAKsmart新推出香港服务器有哪些优势呢? 1.带宽更大可升至10Mpbs 香港服务器的 ...
- Laravel 迁移检查表是否存在
Schema::hasTable('TableName'); //检查表释放存在 Schema::hasColumn('tableName', 'columeName'); //检查表是否存在某个字段 ...
- eclipse总结source folder和Deployment Assembly部署
在src下创建多级目录 然后右键build path-->use as source folder 就可以直接将多级普通文件夹转换成source folder build path下也可以直接n ...
- thinkphp 切换数据库
除了在预先定义数据库连接和实例化的时候指定数据库连接外,我们还可以在模型操作过程中动态的切换数据库,支持切换到相同和不同的数据库类型.用法很简单, 只需要调用Model类的db方法,用法: 常州大理石 ...
- 搞大数据,你不懂这三大数据处理趋势就OUT了
搞大数据,你不懂这三大数据处理趋势就OUT了 企业数据每年以PB级甚至上百PB爆炸式增长,越来越大的数据量正为扩大分析策略在企业应用软件领域的拓展提供了数据基础,但数据的价值是有时效性的,越早分析越能 ...
- 有道云笔记新功能发现——有道云笔记剪报,完美解决不开会员保存csdn博客到本地的问题。
怎么用 方法一:谷歌插件 方法二:http://note.youdao.com/web-clipper-chrome.html 添加到书签 功能: 能够把网页浏览的内容保存到有道云笔记 解决了自己的难 ...
- transient在java中的作用
java 的transient关键字的作用是需要实现Serilizable接口,将不需要序列化的属性前添加关键字transient,序列化对象的时候,这个属性就不会序列化到指定的目的地中. trans ...
- npm安装vuex及防止页面刷新数据丢失
npm install vuex 在项目scr目录下新建store文件夹,在store文件夹下新建index.js文件. import Vue from 'vue'; import Vuex from ...
- Win10系统无法安装可选功能提示错误代码0x800F081F的解决方法
DISM /Online /Cleanup-Image /RestoreHealth /Source:wim:H:\sources\install.wim:1 /limitaccess