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. ubuntu-docker入门到放弃(七)操作系统

    操作系统相信很多人都会装,但是当使用docker容器来安装操作系统的时候,还是跟我们平时安装操作系统有很大区别的,我们之前也下载安装过centos系统,你会发现跟我们之前的操作系统相比,很精简,那么我 ...

  2. eclipse 安装配置

    https://blog.csdn.net/jklinux/article/details/77861450 JAVA环境配置 https://jingyan.baidu.com/article/db ...

  3. Redis和Memcache区别,优缺点对比

    1. Redis和Memcache都是将数据存放在内存中,都是内存数据库.不过memcache还可用于缓存其他东西,例如图片.视频等等. 2.Redis不仅仅支持简单的k/v类型的数据,同时还提供li ...

  4. https://api.highcharts.com/gantt/

    <a href="https://api.highcharts.com/gantt/">https://api.highcharts.com/gantt/</a& ...

  5. autoit3编辑器SCITE字体设置

    选项→打开全局设置文件,就是SciTEGlobal.properties,修改下面的部分即可,保存之后立刻生效.如果不行,就打开用户设置文件SciTEUser.properties进行修改: font ...

  6. 查找二叉树(tree_a)

    问题 E: 查找二叉树(tree_a) 时间限制: 1 Sec  内存限制: 128 MB提交: 206  解决: 152[提交][状态][讨论版][命题人:quanxing][Edit] [Test ...

  7. linux下误删目录文件后恢复神器extundelete

    原文链接:https://blog.51cto.com/wzlinux/2052835 参考:https://blog.csdn.net/cwg_1992/article/details/463100 ...

  8. CSS之display

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  9. 谷歌浏览器内核Cef js代码整理(三) 字符串处理

    *字符串截取方法*/ var s="abc_def[ghi]jk[i]"; var temp;function CopyFromStr(str_source,str_key, bl ...

  10. uva-993-贪心

    题意:给你一个数字y,生成另外一个最小的数字x,使得x里面的每一位相乘等于y 解题思路:直接贪心就是,x里面的每一位都小于等于9 #include <string> #include< ...