Vehicle 一个车辆的虚基类

class Vehicle {
public:
virtual double weight()const = 0;
virtual void start() = 0;
virtual Vehicle* copy() = 0;
virtual ~Vehicle() {};
};

衍生出RoadVehicle AutoVehicle两个类

如果有一个停车场类 可以容纳100辆车

一开始计划 有Vehicle[100]这种变量来表示停车场

但是Vehicle是虚基类 不能有实例化的实体 也就不会有数组

所以需要一个代理类来保存Vehicle的指针 它可以指向任意的Vehicle的衍生类

同时考虑到拷贝操作 添加了拷贝函数

class VehicelSurrogate {
public:
VehicelSurrogate() :vp(0) {};
VehicelSurrogate( Vehicle& v) :vp(v.copy()) {};
VehicelSurrogate(const VehicelSurrogate& v):vp(v.vp ? v.vp->copy() : 0) {};
~VehicelSurrogate() {
delete vp;
};
VehicelSurrogate& operator=(const VehicelSurrogate& v) {
if (this != &v) {
delete vp;
vp = (v.vp ? v.vp->copy() : 0);
}
return *this;
};
double weight() {
return vp->weight();
}

private:
Vehicle* vp;
};

如此 就可以使用VehicelSurrogate[100]来表示停车场的概念

完整代码如下

 // test5.cpp: 定义控制台应用程序的入口点。
// #include "stdafx.h"
#include <iostream> class Vehicle {
public:
virtual double weight()const = ;
virtual void start() = ;
virtual Vehicle* copy() = ;
virtual ~Vehicle() {};
}; class RoadVehicle:public Vehicle{
public:
virtual double weight() const{
return ;
}
virtual void start() {}
virtual Vehicle* copy() {
return new RoadVehicle(*this);
}
virtual ~RoadVehicle() {}
};
class AutoVehicle:public RoadVehicle{
public:
virtual double weight() const{
return ;
}
virtual void start() {}
virtual Vehicle* copy() {
return new AutoVehicle(*this);
}
virtual ~AutoVehicle() {} }; class VehicelSurrogate {
public:
VehicelSurrogate() :vp() {};
VehicelSurrogate( Vehicle& v) :vp(v.copy()) {};
VehicelSurrogate(const VehicelSurrogate& v):vp(v.vp ? v.vp->copy() : ) {};
~VehicelSurrogate() {
delete vp;
};
VehicelSurrogate& operator=(const VehicelSurrogate& v) {
if (this != &v) {
delete vp;
vp = (v.vp ? v.vp->copy() : );
}
return *this;
};
double weight() {
return vp->weight();
} private:
Vehicle* vp;
}; int main()
{
VehicelSurrogate parking_lot[];
AutoVehicle x;
parking_lot[] = x;
std::cout << parking_lot[].weight() << std::endl;;
return ;
}

c++沉思录 学习笔记 第五章 代理类的更多相关文章

  1. c++沉思录 学习笔记 第六章 句柄(引用计数指针雏形?)

    一个简单的point坐标类 class Point {public: Point():xval(0),yval(0){} Point(int x,int y):xval(x),yval(y){} in ...

  2. Programming Entity Framework-dbContext 学习笔记第五章

    ### Programming Entity Framework-dbContext 学习笔记 第五章 将图表添加到Context中的方式及容易出现的错误 方法 结果 警告 Add Root 图标中的 ...

  3. [HeadFrist-HTMLCSS学习笔记]第五章认识媒体:给网页添加图像

    [HeadFrist-HTMLCSS学习笔记]第五章认识媒体:给网页添加图像 干货 JPEG.PNG.GIF有何不同 JPEG适合连续色调图像,如照片:不支持透明度:不支持动画:有损格式 PNG适合单 ...

  4. 【马克-to-win】学习笔记—— 第五章 异常Exception

    第五章 异常Exception [学习笔记] [参考:JDK中文(类 Exception)] java.lang.Object java.lang.Throwable java.lang.Except ...

  5. 《Spring实战》学习笔记-第五章:构建Spring web应用

    之前一直在看<Spring实战>第三版,看到第五章时发现很多东西已经过时被废弃了,于是现在开始读<Spring实战>第四版了,章节安排与之前不同了,里面应用的应该是最新的技术. ...

  6. opencv图像处理基础 (《OpenCV编程入门--毛星云》学习笔记一---五章)

    #include <QCoreApplication> #include <opencv2/core/core.hpp> #include <opencv2/highgu ...

  7. 学习笔记 第五章 使用CSS美化网页文本

    第五章   使用CSS美化网页文本 学习重点 定义字体类型.大小.颜色等字体样式: 设计文本样式,如对齐.行高.间距等: 能够灵活设计美观.实用的网页正文版式. 5.1 字体样式 5.1.1 定义字体 ...

  8. [汇编学习笔记][第五章[BX]和loop指令]

    第五章[BX]和loop指令 前言 定义描述性符号“()”来表示一个寄存器或一个内存单元的内容,比如: (ax)表示ax中的内容,(al)表示al的内容. 约定符号ideta表示常量. 5.1 [BX ...

  9. [Python学习笔记][第五章Python函数设计与使用]

    2016/1/29学习内容 第四章 Python函数设计与使用 之前的几页忘记保存了 很伤心 变量作用域 -一个变量已在函数外定义,如果在函数内需要修改这个变量的值,并将这个赋值结果反映到函数之外,可 ...

随机推荐

  1. MySQL创建数据库与用户

    root远程访问授权 mysql> SHOW DATABASES; +--------------------+ | Database           | +---------------- ...

  2. zeal工具的安装与使用(离线api文档浏览器)

    本来想下载一个dash来用一下,结果它只有mac版本,没有windows版,遂使用zeal zeal官网:https://zealdocs.org/ 文档地址:http://kapeli.com/do ...

  3. C++环境的配置( windows)

    方法一.——VS: 使用windows开发神器visio studio.这种方法比较简单,直接下载一个最新的vs安装就行.不单单是C++,C.C#.VB等都可以开发. 方法二.——只安装C++编译器: ...

  4. 《算法导论》——随机化快排RandomizedQuickSort

    今日算法:随机化快排RandomizedQuickSort 基础工作swap交换和partition分治 /* *交换数组的两个元素 *fromIndex和toIndex为要交换的两个元素的索引 */ ...

  5. leetcode207

    拓扑排序问题. class Solution { public: bool canFinish(int numCourses, vector<pair<int, int>>&a ...

  6. JEECG 上传插件升级-Online

    前言: 现有的uploadify上传是基于swf的,随着H5的普及,flash即将退出历史舞台,JEECG本着与时俱进的原则,将全面升级JEECG系统中的上传功能,采用新式上传插件plupload,此 ...

  7. poi横纵动态导入

    dao层 <insert id ="saveInTarget" parameterType="java.util.List" > INSERT IN ...

  8. php一次性大量数据入库解决方法

    当有业务需求需要一次性循环n条数据,插入或更新数据库时,如果单纯的循环,插入/更新,会消耗太多的数据库资源   一下是一种简单的解决方案   数据库的insert 是可以批量更新的,当有大量数据循环i ...

  9. python学习记录

    学习python中······· 今天写了个装饰器用来登录用,用户名和密码是存在文件里的,涉及到了装饰器和带参数的装饰器 文件里的内容如下 {"liming": 123456} { ...

  10. python入门学习0

    Python 是什么类型的语言 Python是脚本语言 Python下载地址:https://www.python.org/downloads/ Python版本:Python 3.4.2 - 64b ...