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

运行结果:

思维导图:

相关推荐
冷崖4 小时前
const 与 constexpr
c++·学习
枫叶丹44 小时前
【Qt开发】多元素类控件(三)-> QTreeWidget
开发语言·数据库·c++·qt
晨非辰4 小时前
【数据结构入坑指南】--《层序分明:堆的实现、排序与TOP-K问题一站式攻克(源码实战)》
c语言·开发语言·数据结构·算法·面试
hansang_IR4 小时前
【题解】P2217 [HAOI2007] 分割矩阵 [记忆化搜索]
c++·数学·算法·记忆化搜索·深搜
洲覆5 小时前
Redis 驱动适配 Reactor 模式
开发语言·网络·数据库·redis
fl1768315 小时前
基于matlab实现的DnCNN网络
开发语言·matlab
第二层皮-合肥5 小时前
如何设置等长的最大走线长度
服务器·开发语言·php
掘根5 小时前
【Protobuf】proto3语法详解1
开发语言·前端·javascript
Lee_yayayayaya5 小时前
《通信之道—从微积分到5G》阅读笔记
开发语言·matlab
普密斯科技5 小时前
图像尺寸测量仪应用Type-C接口:精准检测,赋能科技
c语言·开发语言·科技