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

c++ day5

完成沙发床的多继承,包含指针成员!

#include <iostream>

using namespace std;
class Sofa
{
private:
    string siting;
    int *num;//个数,指针成员
public:
    Sofa(){cout << "SOfa::无参构造函数" << endl;}//无参构造
    Sofa(string siting,int num):siting(siting),num(new int(num))//有参构造
    {
        cout << "Sofa::有参构造函数" << endl;
    }
    Sofa(const Sofa &other):siting(other.siting),num(new int(*(other.num)))//拷贝构造
    {
        cout << "Sofa::拷贝构造函数" << endl;
    }
    Sofa &operator=(const Sofa &other)//拷贝赋值
    {
        if (this!=&other)
        {
            siting=other.siting;
            num=new  int(*(other.num));
        }
        cout << "SOfa拷贝赋值函数" << endl;
        return  *this;
    }
    ~Sofa()
    {
        delete  num;
        cout << "sofa::析构函数" << endl;
    }
    void show()
    {
        cout << "siting=" << siting << endl;
        cout << "num="   << *num << endl;
    }
};
class Bed
{
private:
    string sleeping;
    int * area;//面积,指针成员
public:
    Bed(){cout << "Bed::无参构造函数" << endl;}//无参构造
    Bed(string sleeping,int area):sleeping(sleeping),area(new int(area))//有参构造
    {
        cout << "Bed::有参构造函数" << endl;
    }
    Bed(const Bed &other):sleeping(other.sleeping),area(new int(*(other.area)))//拷贝构造
    {
        cout << "Bed::拷贝构造函数" << endl;
    }
    Bed &operator=(const Bed &other)//拷贝赋值
    {
        if (this!=&other)
        {
            sleeping=other.sleeping;
            area=new  int(*(other.area));
        }
        cout << "Bed::拷贝赋值函数" << endl;
        return  *this;
    }
    ~Bed()//析构函数
    {
        delete  area;
        cout << "Bed::析构函数" << endl;
    }
    void show()
    {
        cout << "sleeping=" << sleeping << endl;
        cout << "arae="   << *area << endl;
    }

};
class Sofa_Bed:public Sofa,public Bed
{
private:
    string color;
public:
    Sofa_Bed(){cout << "SOFA_Bed::无参构造函数" << endl;}
    Sofa_Bed(string color,string siting,int num,string sleeping,int area):Sofa(siting,num),Bed(sleeping,area),color(color)
    {
        cout << "Sofa_Bed::有参构造函数" << endl;
    }
    Sofa_Bed(const Sofa_Bed &other):Sofa(other),Bed(other),color(other.color)
    {
         cout << "Sofa_Bed::拷贝构造函数" << endl;
    }
    Sofa_Bed &operator=(const Sofa_Bed &other)
    {
        if(this!=&other)
        {
            color=other.color;
            Sofa::operator=(other);
            Bed::operator=(other);
        }
        cout << "Sofa_Bed::拷贝赋值函数" << endl;
        return *this;
    }
    ~Sofa_Bed()
    {
        cout << "Sofa_Bed::析构函数" << endl;
    }
    void show()
    {
        Sofa::show();
        Bed::show();
        cout << "color= " << color << endl;
    }
};
int main()
{
    Sofa_Bed bs("yellow","可坐",2,"可睡",20);
    cout << "-------------------------" << endl;
    bs.Sofa::show();

    cout << "-------------------------" << endl;
    bs.Bed::show();

    cout << "-------------------------" << endl;
    bs.show();
    return 0;
}


http://www.kler.cn/news/157089.html

相关文章:

  • Windows下打包C++程序无法执行:无法定位程序输入点于动态链接库
  • PTA 7-225 sdut-C语言实验- 冒泡排序中数据交换的次数
  • 继承 多态 拆箱装箱 128陷阱 枚举类
  • 【Java】类和对象之超级详细的总结!!!
  • PPT NO.4 更改PPT“另存为”分辨率
  • java中一个空的Object对象在HotSpot虚拟机中占用多大的内存空间?
  • Vue3 组合式实现 带连接线的Tree型 架构图(一级树形图)
  • 基于springboot + vue框架的网上商城系统
  • Android多用户初探
  • vscode里面使用vue的一些插件,方便开发
  • Spring Security 6.x 系列(8)—— 源码分析之配置器SecurityConfigurer接口及其分支实现
  • MySQL官网推荐书籍
  • 【接口测试】POST请求提交数据的三种方式及Postman实现
  • 【广州华锐视点】机械零件拆装VR仿真教学系统
  • 【EI会议征稿】第五届人工智能与机电自动化国际学术会议(AIEA 2024)
  • bad_python
  • Vue3实现一个拾色器功能
  • TimeGPT:时间序列预测模型实例
  • TDA4开发环境Docker化
  • 《系统架构设计师教程(第2版)》第2章-计算机系统基础知识-01-计算机硬件
  • Spring中通知是什么
  • Redis7--基础篇4(Redis事务)
  • CocosCreator 面试题(二十) Cocos creator 如何实现一个置灰Shader?
  • [Ubuntu 20.04] 使用Netplan配置网络静态IP
  • RH850P1X芯片学习笔记-Pin Functions
  • 智能优化算法应用:基于松鼠算法无线传感器网络(WSN)覆盖优化 - 附代码
  • 什么是Overlay网络?Overlay网络与Underlay网络有什么区别?
  • 搭建CIG容器重量级监控平台
  • C\C++ 获取最值
  • 无人机覆盖路径规划综述