C++作业5

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

代码:

cpp 复制代码
#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;
}

运行结果:

思维导图:

相关推荐
小胖伦的夕阳粉15 分钟前
js 获取树节点上某节点的最底层叶子节点数据
开发语言·javascript·ecmascript
好记性+烂笔头17 分钟前
hot100-438. 找到字符串中所有字母异位词
算法
不见长安见晨雾24 分钟前
将Java程序打包成EXE程序
java·开发语言
可愛小吉27 分钟前
Python 课程14-TensorFlow
开发语言·人工智能·python·tensorflow
编程零零七28 分钟前
Python数据分析工具(四):pymysql的用法
开发语言·python·oracle·数据挖掘·数据分析·python项目·python源码
六点半88830 分钟前
【C/C++】速通涉及string类的经典编程题
c语言·开发语言·c++·算法
汤姆和杰瑞在瑞士吃糯米粑粑32 分钟前
string类(C++)
开发语言·c++
逸狼1 小时前
【JavaEE初阶】多线程(5 单例模式 \ 阻塞队列)
java·开发语言
学霸小羊1 小时前
C++游戏
c++·游戏
Jack黄从零学c++1 小时前
自制网络连接工具(支持tcpudp,客户端服务端)
linux·c语言·开发语言·网络协议·tcp/ip·udp·信息与通信