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;
}

运行结果:

思维导图:

相关推荐
小牛itbull9 分钟前
ReactPress vs VuePress vs WordPress
开发语言·javascript·reactpress
怀澈12211 分钟前
高性能服务器模型之Reactor(单线程版本)
linux·服务器·网络·c++
请叫我欧皇i18 分钟前
html本地离线引入vant和vue2(详细步骤)
开发语言·前端·javascript
闲暇部落21 分钟前
‌Kotlin中的?.和!!主要区别
android·开发语言·kotlin
GIS瞧葩菜30 分钟前
局部修改3dtiles子模型的位置。
开发语言·javascript·ecmascript
chnming198735 分钟前
STL关联式容器之set
开发语言·c++
带多刺的玫瑰38 分钟前
Leecode刷题C语言之统计不是特殊数字的数字数量
java·c语言·算法
爱敲代码的憨仔40 分钟前
《线性代数的本质》
线性代数·算法·决策树
威桑1 小时前
MinGW 与 MSVC 的区别与联系及相关特性分析
c++·mingw·msvc
熬夜学编程的小王1 小时前
【C++篇】深度解析 C++ List 容器:底层设计与实现揭秘
开发语言·数据结构·c++·stl·list