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

运行结果:

思维导图:

相关推荐
一颗松鼠3 分钟前
JavaScript 闭包是什么?简单到看完就理解!
开发语言·前端·javascript·ecmascript
泉崎3 分钟前
11.7比赛总结
数据结构·算法
有梦想的咸鱼_5 分钟前
go实现并发安全hashtable 拉链法
开发语言·golang·哈希算法
你好helloworld5 分钟前
滑动窗口最大值
数据结构·算法·leetcode
海阔天空_201310 分钟前
Python pyautogui库:自动化操作的强大工具
运维·开发语言·python·青少年编程·自动化
天下皆白_唯我独黑17 分钟前
php 使用qrcode制作二维码图片
开发语言·php
夜雨翦春韭21 分钟前
Java中的动态代理
java·开发语言·aop·动态代理
小远yyds23 分钟前
前端Web用户 token 持久化
开发语言·前端·javascript·vue.js
何曾参静谧35 分钟前
「C/C++」C/C++ 之 变量作用域详解
c语言·开发语言·c++
AI街潜水的八角44 分钟前
基于C++的决策树C4.5机器学习算法(不调包)
c++·算法·决策树·机器学习