当前位置: 首页 > news >正文

12.4 C++ 作业

完成沙发床的多继承

#include <iostream>using namespace std;//封装 沙发 类
class Sofa
{
private:string *sitting;
public://无参构造函数Sofa(){cout << "Sofa::无参构造函数" << endl;}//有参构造函数Sofa(string s):sitting(new string(s)){cout << "Sofa::有参构造函数" << endl;}//拷贝构造函数Sofa(const Sofa &other):sitting(new string(*(other.sitting))){cout << "Sofa::拷贝构造函数" << endl;}//拷贝赋值函数Sofa &operator=(const Sofa &other){if(this != &other){sitting = new string(*(other.sitting));}cout << "Sofa::拷贝赋值函数" << endl;return *this;}//析构函数~Sofa(){delete sitting;cout << "Sofa::析构函数" << endl;}void show(){cout << *sitting << endl;}
};//封装 床 类
class Bed
{
private:string sleep;
public://无参构造函数Bed(){cout << "Bed::无参构造函数" << endl;}//无参构造函数Bed(string sleep):sleep(sleep){cout << "Bed::有参构造函数" << endl;}//拷贝构造函数Bed(const Bed &other):sleep(other.sleep){cout << "Bed::拷贝构造函数" << endl;}//拷贝赋值函数Bed &operator=(const Bed &other){if(this != &other){sleep = other.sleep;}return *this;}//析构函数~Bed(){cout << "Bed::拷贝析构函数" << endl;}void show(){cout << sleep << endl;}
};
//封装 沙发床 类 共有继承于沙发和床
class Sofa_bed:public Sofa,public Bed
{
private:string *color;
public://无参构造函数Sofa_bed(){cout << "Sofa_bed::无参构造函数" << endl;}//有参构造函数Sofa_bed(string s,string sleep,string color):Sofa(s),Bed(sleep),color(new string(color)){cout << "Sofa_bed::有参构造函数" << endl;}//拷贝构造函数Sofa_bed(const Sofa_bed &other):Sofa(other),Bed(other),color(new string(*(other.color))){cout << "Sofa_bed::拷贝构造函数" << endl;}//拷贝赋值函数Sofa_bed &operator=(const Sofa_bed &other){if(this != &other){Sofa::operator=(other);Bed::operator=(other);color = new string(*(other.color));}cout << "Sofa_bed::拷贝赋值函数" << endl;return  *this;}//析构函数~Sofa_bed(){delete color;}void show(){Sofa::show();Bed::show();cout << *color << endl;}
};
int main()
{Sofa_bed sd;Sofa_bed sd1("可坐", "可睡", "blud");sd1.show();Sofa_bed sd2(sd1);sd2.show();sd = sd1;sd.show();return 0;
}

http://www.lryc.cn/news/252359.html

相关文章:

  • 基于ssm品牌会员在线商城源码
  • 骨传导耳机音量大了有害吗?骨传导能保护听力吗?
  • 百望云供应链协同解决方案入选北大创新评论产业研究案例库
  • selenium中元素定位正确但是操作失败,6种解决办法全搞定
  • 触控板绘画工具Inklet mac功能介绍
  • 〔005〕虚幻 UE5 像素流多用户部署
  • 11. 哈希冲突
  • 12.04 二叉树中等题
  • Redis的安装
  • JDK安装太麻烦?一篇文章搞定
  • 漫谈HBuilderX App-Jenkins热更新构建
  • 技术前沿丨Teranode如何实现无限扩容
  • 世岩清上:如何制作年终工作汇报宣传片
  • 练习十一:简单卷积器的设计
  • 外包干了4年,技术退步太明显了。。。。。
  • 微服务实战系列之EhCache
  • SpringBoot:SpringMVC(上)
  • 一文搞懂Go语言中包导入
  • Vue2学习笔记(事件处理)
  • 【2023第十二届“认证杯”数学中国数学建模国际赛】A题 太阳黑子预报完整解题思路
  • Huawei FusionSphere FusionCompte FusionManager
  • GSLB是什么?谈谈对该技术的一点理解
  • 【接口测试】POST请求提交数据的三种方式及Postman实现
  • SpringBoot系列之集成Jedis教程
  • centos用什么命令可查看版本号
  • 大数据之Redis
  • 【React设计】React企业级设计模式
  • 赴日程序员高年薪过上“躺平”生活?
  • Windows开启SQL Server服及1433端口
  • 网盘系统设计:万亿 GB 网盘如何实现秒传与限速?