解决多继承三角问题实例(SofaBed)
来源:互联网 发布:故宫淘宝 编辑:程序博客网 时间:2024/06/14 03:06
解决多继承三角问题实例(SofaBed)
主函数:
#include "stdafx.h"#include <iostream>using namespace std;#include "Sofa.h"#include "Bed.h"#include "SofaBed.h"//为解决//sofabed len wid high 两份数据 冗余// discription() 访问问题int _tmain(int argc, _TCHAR* argv[]){Sofa sf(3,2,1);sf.sit();sf.discription();//有虚继承就会有虚基类表指针 而指针大小为4字节 所以sofa为16cout << sizeof(Sofa) << endl; Bed bed(1,2,3);bed.sleep();bed.discription();cout << sizeof(Bed) << endl;//Bed Sofa 这些中间类,并不受virtual的影响 像普通类一样使用 //virtual发生的阶段 在生成孙子类的时候生效SofaBed sb(2,3,3);sb.sit();sb.sleep();sb.discription();//SofaBed 虚继承继承俩个父类 产生俩个虚基类表指针 8字节cout << sizeof(SofaBed) << endl; //12+8=20字节return 0;}
Furniture.h
#pragma once#include <iostream>using namespace std;class Furniture{public:Furniture(int l,int w,int h);~Furniture();void discription();private:int len;int wid;int high;};
Furniture.cpp
#include "Furniture.h"Furniture::Furniture(int l, int w, int h){len = l;wid = w;high = h;}void Furniture::discription(){cout << "len: " << len << endl;cout << "wid: " << wid << endl;cout << "high: " << high << endl;}Furniture::~Furniture(){}
Bed.h
#pragma once#include <iostream>using namespace std;#include "Furniture.h"class Bed:virtual public Furniture{public:Bed(int l,int w,int h);~Bed();void sleep();};
Bed.cpp
#include "Bed.h"Bed::Bed(int l, int w, int h):Furniture(l,w,h){}void Bed::sleep(){cout << "go to bed and have a sleep" << endl;}Bed::~Bed(){}
Sofa.h
#pragma once#include <iostream>using namespace std;#include "Furniture.h"class Sofa:virtual public Furniture{public:Sofa(int l,int w,int h);~Sofa();void sit();};
Sofa.cpp
#include "Sofa.h"Sofa::Sofa(int l, int w, int h):Furniture(l,w,h){}void Sofa::sit(){cout << "have a sit and have a rest" << endl;}Sofa::~Sofa(){}
SofaBed.h
#pragma once#include <iostream>using namespace std;#include "Bed.h"#include "Sofa.h"class SofaBed:public Sofa,public Bed{public:SofaBed(int l,int w,int h);~SofaBed();};
SofaBed.cpp
#include "SofaBed.h"SofaBed::SofaBed(int l, int w, int h):Sofa(l, w, h), Bed(l, w, h), Furniture(l,w,h){}SofaBed::~SofaBed(){}
阅读全文
0 0
- 解决多继承三角问题实例(SofaBed)
- 多继承中的三角问题(虚继承来解决)
- PHP解决数字三角问题
- python解决“杨辉三角”问题
- Python解决杨辉三角问题
- 用js解决杨辉三角问题
- 用队列解决杨辉三角问题
- c语言解决杨辉三角问题
- 二维数组解决杨辉三角问题
- 用队列解决杨辉三角问题
- CKEDITOR多实例问题的解决(转)
- 解决html透明度继承问题
- 三角问题
- 多继承实例
- 数组解决杨辉三角问题(输出10行)
- 一个差三角问题的穷举法解决
- 【OOP】PHP下解决多继承的问题
- 多继承原理抛砖 - 虚继承的应用场景 - 虚继承能解决的问题有限
- (八)Scrapy框架
- 解决hive 与hbase 集成报空指针的问题at org.apache.hadoop.net.DNS.reverseDns(DNS.java:92)
- SimpleDateFormat结合对象池化工具commons-pool
- 51Nod-2020-排序相减
- python 学习之路(字典)
- 解决多继承三角问题实例(SofaBed)
- ActiveMQ安装
- 深层神经网络过拟合优化
- 图像处理中的数学原理详解(Part1 总纲)
- XGBoost 安装失败最后尝试的一种方法
- Extractive Summarization using Continuous Vector Space Models
- Jzoj4384 Hashit
- Android图片加载框架最全解析(七),实现带进度的Glide图片加载功能
- 【数位DP+莫比乌斯函数】2017计蒜之道复赛A[阿里云秘钥池]题解