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 静态成员是“类级别”的,也就是它和类的地位等同,而普通成员是“ ...
随机推荐
- CSIC_716_20191119【常用模块的用法 subprocess、re、logging、防止自动测试、包的理论】
subprocess模块 可以通过python代码给操作系统终端发送命令,并可以得到返回结果. import subprocess str = input('>>>请输入命令') # ...
- MD5 AND JSON AND XML
MD5JSON.h #pragma once #include "include/json/json.h" #include "include/md5/md5.h&quo ...
- django2 连接mysql实现第一个rest framework
1.安装pymysql,mysqlclient,创建项目django-admin startproject django2 2.settings中把DataBase配置换掉 DATABASES = { ...
- Installer - Eclipse配置Data Source Explorer
一.配置Oracle数据库连接 1.打开配置窗口 在[window]菜单中的[show windows]选择[other],在弹出的窗口中选择[Data Source Explorer]进行数据数据库 ...
- PAT甲级——A1108 Finding Average【20】
The basic task is simple: given N real numbers, you are supposed to calculate their average. But wha ...
- Apache服务器性能如何优化
配置网站,要充分的把系统资源利用出去,榨干系统的一切潜能,让网站速度发挥到极致.Apache,是用户浏览和网站数据的一个中间桥梁.但是在这里,我们虽然做得并不是很多,但是如果心细打理,有些设置也会起到 ...
- [Neo4j] 添加算法插件包
下载graph-algorithms-algo-xxx.jar包,我下的是3.5.3.1,放到neo4j目录的plugins文件夹下 修改 conf目录下的配置文件 neo4j.conf ,加一行: ...
- 面试系列22 dubbo的工作原理
(1)dubbo工作原理 第一层:service层,接口层,给服务提供者和消费者来实现的 第二层:config层,配置层,主要是对dubbo进行各种配置的 第三层:proxy层,服务代理层,透明生成客 ...
- 【BZOJ4407】于神之怒加强版
题面 题目分析 \[ \begin{split} \sum\limits_{i=1}^n\sum\limits_{j=1}^mgcd(i,j)^k&=\sum\limits_{d=1}^nd^ ...
- Widget Factory
Widget Factory 有N种零件,生产所需天数都为3~9天,有M条记录,记录开工星期几,和停工星期几,并告诉你这条记录所加工的零件,求每种零件的生产时间,\(1≤N,M≤300\). 解 显然 ...