c++沉思录 学习笔记 第五章 代理类
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++沉思录 学习笔记 第五章 代理类的更多相关文章
- c++沉思录 学习笔记 第六章 句柄(引用计数指针雏形?)
一个简单的point坐标类 class Point {public: Point():xval(0),yval(0){} Point(int x,int y):xval(x),yval(y){} in ...
- Programming Entity Framework-dbContext 学习笔记第五章
### Programming Entity Framework-dbContext 学习笔记 第五章 将图表添加到Context中的方式及容易出现的错误 方法 结果 警告 Add Root 图标中的 ...
- [HeadFrist-HTMLCSS学习笔记]第五章认识媒体:给网页添加图像
[HeadFrist-HTMLCSS学习笔记]第五章认识媒体:给网页添加图像 干货 JPEG.PNG.GIF有何不同 JPEG适合连续色调图像,如照片:不支持透明度:不支持动画:有损格式 PNG适合单 ...
- 【马克-to-win】学习笔记—— 第五章 异常Exception
第五章 异常Exception [学习笔记] [参考:JDK中文(类 Exception)] java.lang.Object java.lang.Throwable java.lang.Except ...
- 《Spring实战》学习笔记-第五章:构建Spring web应用
之前一直在看<Spring实战>第三版,看到第五章时发现很多东西已经过时被废弃了,于是现在开始读<Spring实战>第四版了,章节安排与之前不同了,里面应用的应该是最新的技术. ...
- opencv图像处理基础 (《OpenCV编程入门--毛星云》学习笔记一---五章)
#include <QCoreApplication> #include <opencv2/core/core.hpp> #include <opencv2/highgu ...
- 学习笔记 第五章 使用CSS美化网页文本
第五章 使用CSS美化网页文本 学习重点 定义字体类型.大小.颜色等字体样式: 设计文本样式,如对齐.行高.间距等: 能够灵活设计美观.实用的网页正文版式. 5.1 字体样式 5.1.1 定义字体 ...
- [汇编学习笔记][第五章[BX]和loop指令]
第五章[BX]和loop指令 前言 定义描述性符号“()”来表示一个寄存器或一个内存单元的内容,比如: (ax)表示ax中的内容,(al)表示al的内容. 约定符号ideta表示常量. 5.1 [BX ...
- [Python学习笔记][第五章Python函数设计与使用]
2016/1/29学习内容 第四章 Python函数设计与使用 之前的几页忘记保存了 很伤心 变量作用域 -一个变量已在函数外定义,如果在函数内需要修改这个变量的值,并将这个赋值结果反映到函数之外,可 ...
随机推荐
- LINUX 查看当前系统的内存使用情况 vmstat
Linux vmstat 命令 Vmstat命令是最常见的Linux/Unix监控工具,可以展现给定时间间隔的服务器的状态值,包括服务器的CPU使用率,内存使用,虚拟内存交换情况,IO读写情况. 相比 ...
- Git和Svn对比
From: https://wenku.baidu.com/view/1f090e2e7275a417866fb84ae45c3b3567ecdd12.html Git和Svn对比 共享文档 ...
- git clone git@github.com:xxx.git Permission denied (publickey) 问题解决办法
From: https://www.cnblogs.com/restart/p/4633928.html 如果git无法通过普通的http去clone远程分支,可以选用ssh方式去连接.这时需要配置相 ...
- [UE4]Expandable Area可展开的区域
一.Expandable Area控件分成头部和主体部分,头部和主体都是一个Named Slot,可以放置任何控件进去,点击头部会因此主体部分 1.如果勾选Size To Content,则收起的时候 ...
- 涨姿势:Java 分业务、分级别实现自定义日志打印
自定义日志级别 通常的日志框架都有以下几个级别,从低到高TRACE,DEBUG,INFO,WARN,ERROR,FATAL. 默认情况,假如我们定义日志打印级别INFO,它会把大于等于INFO级别的日 ...
- Promise的实现原理
1.Promise 介绍 Promise类似一个事务管理器,将用户异步操作流程用流水的形式来表达,用来延迟deferred和异步asynchronous. 特点如下: (1)对象的状态不受外界影响 P ...
- 8、Curator的监听机制
原生的zookeeper的监听API所实现的方法存在一些缺点,对于开发者来说后续的开发会考虑的细节比较多. Curator所实现的方法希望摒弃原声API 的不足,是开发看起来更加的简单,一些重连等操作 ...
- Oracle 学习笔记(六)
Oracle 数据库常用的闪回sql 语句及其它操作语句: --Oracle 数据库dml sql -- 查看当前用户所拥有的表 select * from tab; --表空间,auto: 自动管理 ...
- [java,2017-05-16] java中清空StringBuffer的方法以及耗费时间比较
java中清空StringBuffer的方法,我能想到的有4种: 1. buffer.setLength(0); 设置长度为0 2. buffer.delete(0, buffer.length() ...
- win10 linux 子系统 所在 目录
C:\Users\用户名\AppData\Local\Packages\CanonicalGroupLimited.UbuntuonWindows_79rhkp1fndgsc\LocalState\r ...