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

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.kler.cn/a/158573.html

相关文章:

  • 第一个3D程序!
  • 仿真设计|基于51单片机的无线投票系统仿真
  • 内外网文件摆渡企业常见应用场景和对应方案
  • android获取EditText内容,TextWatcher按条件触发
  • 留学生scratch计算机haskell函数ocaml编程ruby语言prolog作业VB
  • 后端token校验流程
  • 【win32_003】不同字符集下的通用字符串语法TCHAR、TEXT、PTSTR、PCTSTR
  • 有趣的代码——有故事背景的程序设计3
  • 驱动模块--内核模块
  • Qt 布局讲解及举例
  • 打破界限:SQL数据库水平扩展的8大挑战与机遇
  • 【开源】基于JAVA的医院门诊预约挂号系统
  • (C++)有效三角形的个数--双指针法
  • 推荐6款本周 火火火火 的开源项目
  • SpringBoot学习笔记-实现微服务:匹配系统(下)
  • C语言初学4:C 存储类
  • RocketMQTemplate 发送消息的高级用法
  • 流程编排-java
  • GOLAND搭建GIN框架以及基础框架搭建
  • 一文解决msxml3.dll文件缺失问题,快速修复msxml3.dll
  • postgresql-effective_cache_size参数详解
  • 对小程序的初了解
  • 理解DuLinkList L中的“”引用符号
  • 创建JDK8版本的SpringBoot项目的方法
  • Git 分支详解
  • 【shell】多行重定向与免交互expect与ssh、scp的结合使用