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

C++12.4

 沙发床的多继承
  • 多继承代码实现沙发床
  • 沙发床继承于沙发和床
 代码:
#include <iostream>

using namespace std;

//封装 沙发 类
class Sofa
{
private:
    string sitting;
    double *size;
public:
    //无参构造函数
    Sofa() {cout << "Sofa::无参构造函数" << endl;}

    //有参构造函数
    Sofa(string s,double si):sitting(s),size(new double(si))
    {cout << "Sofa::有参构造函数" << endl;}

    //拷贝构造函数
    Sofa(const Sofa &other):sitting(other.sitting),size(other.size)
    {
        cout << "Sofa::拷贝构造函数" << endl;
    }

    //拷贝赋值函数
    Sofa &operator=(const Sofa &other)
    {
        if(this != &other)
        {
        cout << "Sofa::拷贝赋值函数" << endl;
        this->sitting = other.sitting;
        this->size = other.size;
        }
        return *this;
    }

    //析构函数
    ~Sofa()
    {
        delete(size);
        size = nullptr;
        cout << "Sofa::析构函数" << endl;
    }

    void show()
    {
        cout << sitting << "  " << *size << endl;
    }
};

//封装 床 类
class Bed
{
private:
    string sleep;
public:
    //无参构造函数
    Bed() {cout << "Bed::无参构造函数" << endl;}

    //有参构造函数
    Bed(string s):sleep(s)
    {cout << "Bed::有参构造函数" << endl;}

    //拷贝构造函数
    Bed(const Bed &other):sleep(other.sleep)
    {
        cout << "Bed::拷贝构造函数" << endl;
    }

    //拷贝赋值函数
    Bed &operator=(const Bed &other)
    {
        cout << "Bed::拷贝赋值函数" << endl;
        this->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, double size,string sl, string c):Sofa(s,size), Bed(sl),color(c)
    {
        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)
    {
        cout << "Sofa::拷贝赋值函数" << endl;
        this->color = other.color;
        this->Sofa::operator=(other);
        this->Bed::operator=(other);
        return *this;
    }
    //析构函数
    ~Sofa_bed()
    {
        cout << "Sofa_bed::析构函数" << endl;
    }

    void show()
    {
        Sofa::show();
        Bed::show();
        cout << color << endl;
    }
};
int main()
{
    Sofa_bed sb("可以坐", 180, "可以睡", "灰不溜秋");
    cout << "++++++++++++++++++++++++++++++++++++++" << endl;
    sb.Sofa::show();
    cout << "++++++++++++++++++++++++++++++++++++++" << endl;
    sb.Bed::show();
    cout << "++++++++++++++++++++++++++++++++++++++" << endl;
    sb.show();
    cout << "++++++++++++++++++++++++++++++++++++++" << endl;
    return 0;
}
运行效果图:

思维导图

相关文章:

  • mysql主从复制-redis集群扩容缩容、缓存优化(缓存更新策略、穿透,击穿,雪崩)、mysql主从搭建、django实现读写分离
  • 「Qt Widget中文示例指南」如何创建一个计算器?(二)
  • [ROS2] --- ROS diff ROS2
  • Python 3 使用 write()、writelines() 函数写入文件
  • SQL Server对象类型(8)——4.8.约束(Constraint)
  • 个人硬件测试用例入门设计
  • 跟我学c++高级篇——动态反射之二动态列表
  • python动态圣诞下雪图
  • C-语言每日刷题
  • MySQL基础『数据类型』
  • echarts 地图
  • 微信扫码登录修改二维码的样式
  • git的安装及ssh配置(Linux)
  • Linux中top命令输出日志分析?
  • Kubernetes学习笔记-Part.01 Kubernets与docker
  • 如何使用群晖管家结合内网穿透实现公网远程访问本地黑群晖
  • Docker快速入门(docker加速,镜像,容器,数据卷常见命令操作整理)
  • 用C++语言编写的图书馆系统代码,包括图书录入、查询、插入、修改、删除和添加功能
  • RNN:文本生成
  • DAPP开发【02】Remix使用
  • 外企聊营商|特雷通集团:税务服务“及时雨”
  • 因操纵乙烯价格再遭诉讼,科莱恩等四家企业被陶氏索赔60亿
  • 【社论】个人破产探索,要守住“诚实而不幸”的底线
  • 广州一饮品店取名“警茶”?市监局:取名没问题,但图像会产生误解
  • 王毅人民日报撰文:共商发展振兴,共建中拉命运共同体
  • OpenAI与微软正谈判修改合作条款,以推进未来IPO