12月2号作业

#include <iostream>

using namespace std;
class Sofa{
private:
    string setting;
    string *lying = new string;

public:
    Sofa(){
        cout << "Sofa::无参构造函数" << endl;
    }

    Sofa(string setting,string lying):setting(setting),lying(new string (lying)){
        cout << "Sofa::有参构造函数" << endl;
    }

    Sofa (const Sofa &other):setting(other.setting),lying(new string (*(other.lying))){
        cout << "Sofa::拷贝构造函数" << endl;
    }

    Sofa &operator=(const Sofa &other){
        if(this != &other){
            setting = other.setting;
            lying = other.lying;
        }
        cout << "Sofa::拷贝赋值函数" << endl;
        return *this;
    }

    ~Sofa(){
        cout << "Sofa::析构函数" << endl;
    }

};

class Bed{
private:
    string sleep;
    string *gameing = new string;

public:
    Bed(){
        cout << "Bed::无参构造函数" << endl;
    }
    Bed(string sleep,string gameing):sleep(sleep),gameing(new string(gameing)){
        cout << "Bed::有参构造函数" << endl;
    }

    Bed(const Bed &other):sleep(other.sleep),gameing(new string(*(other.gameing))){
        cout << "Bed::拷贝构造函数" << endl;
    }

    Bed &operator=(const Bed &other){
        if(this != &other){
            sleep = other.sleep;
            gameing = other.gameing;
        }
        cout << "Bed::拷贝赋值函数" << endl;
        return *this;
    }

    ~Bed(){
        cout << "Bed::析构函数" << endl;
    }
};

class sofa_Bed:public Sofa,public Bed{
private:
    string color;
public:
    sofa_Bed(){
        cout << "sofa_Bed::无参构造函数" << endl;
    }
    sofa_Bed(string setting,string lying,string sleep,string gameing,string color):Sofa(setting,lying),Bed(sleep,gameing),color(color){
        cout << "sofa_Bed::有参构造函数" << endl;
    }

    sofa_Bed(const sofa_Bed &other):Sofa(other),Bed(other),color(other.color){
        cout << "sofa_Bed::拷贝构造函数" << endl;
    }

    sofa_Bed &operator=(const sofa_Bed &other){
        if(this != &other){
            color = other.color;
            Sofa::operator=(other);
            Bed::operator=(other);
        }
        cout << "sofa_Bed::拷贝赋值函数" << endl;
        return *this;
    }

    ~sofa_Bed(){
        cout << "sofa_Bed::析构函数" << endl;
    }
};
int main()
{
    sofa_Bed ab1;
    cout << "========================" << endl;

    sofa_Bed ab2("s","l","sl","wzry","black");
    cout << "========================" << endl;

    sofa_Bed ab3=ab2;
    cout << "========================" << endl;

    ab1=ab2;
    cout << "========================" << endl;

    cout << "程序已结束" << endl;

    return 0;
}

运行结果:

思维导图:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值