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

C++作业5

完成沙发床的多继承(有指针成员)

代码:

#include <iostream>using namespace std;class Bed
{
private:double *money;
public:Bed(){cout << "Bed::无参构造函数" << endl;}Bed(double money):money(new double(money)){cout << "Bed::有参构造函数" << endl;}Bed(const Bed &other):money(new double(*(other.money))){cout << "Bed::拷贝构造函数" << endl;}Bed &operator=(const Bed &other){if(this != &other){money = new double(*(other.money));}cout << "Bed::拷贝赋值函数" << endl;return *this;}~Bed(){delete(money);money = nullptr;cout << "Bed::析构函数" << endl;}void show(){cout << "money=" << *money << endl;}
};class Sofa
{
private:double *leight;
public:Sofa(){cout << "Sofa::无参构造函数" << endl;}Sofa(double leight):leight(new double(leight)){cout << "Sofa::有参构造函数" << endl;}Sofa(const Sofa &other):leight(new double(*(other.leight))){cout << "Sofa::拷贝构造函数" << endl;}Sofa &operator=(const Sofa &other){if(this != &other){leight = new double(*(other.leight));}cout << "Sofa::拷贝赋值函数" << endl;return *this;}~Sofa(){delete(leight);leight = nullptr;cout << "Sofa::析构函数" << endl;}void show(){cout << "leight=" << *leight << endl;}
};class BedSofa:public Bed,public Sofa
{
private:double *height;
public:BedSofa(){cout << "BedSofa::无参构造函数" << endl;}BedSofa(double money,double leight,double height):Bed(money),Sofa(leight),height(new double(height)){cout << "BedSofa::有参构造函数" << endl;}BedSofa(const BedSofa &other):Bed(other),Sofa(other),height(new double(*(other.height))){cout << "BedSofa::拷贝构造函数" << endl;}BedSofa &operator=(const BedSofa &other){if(this != &other){height = new double(*(other.height));Bed::operator=(other);Sofa::operator=(other);}cout << "BedSofa::拷贝赋值函数" << endl;return *this;}~BedSofa(){delete(height);height = nullptr;cout << "BedSofa::析构函数" << endl;}void show(){cout << "height=" << *height << endl;}
};int main()
{BedSofa s1(120,4,0.5);s1.Bed::show();s1.Sofa::show();s1.show();return 0;
}

运行结果:

思维导图:

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

相关文章:

  • Go语言很难吗?为什么 Go 岗位这么少?
  • 为什么要替换 Object.defineProperty?
  • 百马百担c语言编程
  • C++检测字符串中有效的括号个数
  • 前端依赖下载速度过慢解决方法,nrm 镜像管理工具
  • 如何为 3D 模型制作纹理的最佳方法
  • 智慧校园:TSINGSEE青犀智能视频监控系统,AI助力优化校园管理
  • Three的lod技术
  • Git配置
  • 阻抗控制下机器人接触刚性环境振荡不稳定进行阻抗调节
  • 【鸿蒙应用ArkTS开发系列】-自定义底部菜单列表弹窗
  • yolov8添加ca注意力机制
  • linux java后台启动的几种方式
  • selinux-policy-default(2:2.20231119-2)软件包内容详细介绍(5)
  • 代码随想录二刷 |栈与队列 |理论基础
  • java--接口概述
  • 出海风潮:中国母婴品牌征服国际市场的机遇与挑战!
  • 一文读懂MongoDB的知识点(3),惊呆面试官。
  • ssm的“魅力”西安宣传网站(有报告)。Javaee项目。
  • 怎么让SecureCRT不自动断开连接
  • 介绍几种Go语言开发的IDE
  • 1、设计模式简介(7大原则,3大类)
  • 华为鲲鹏+银河麒麟V10编译FreeSWITCH1.10.9
  • CFS三层靶机内网渗透
  • 软件分享--智能照片识别分类软件
  • Leetcode—409.最长回文串【简单】
  • 计算机网络入侵检测技术研究
  • 深入学习锁--Synchronized各种使用方法
  • pycharm中绘制一个3D曲线
  • 人工智能_AI服务器安装清华开源_CHATGLM大语言模型_GLM-6B安装部署_人工智能工作笔记0092