2023.10.11

2023.10.11_第1张图片

#include 

using namespace std;

class Sofa{
private:
    int price;
    int* size;
public:
    //无参构造
    Sofa(){}
    //有参构造
    Sofa(int p,int size):price(p),size(new int(size)){}
    //析构
    ~Sofa()
    {
        delete size;
    }
    //拷贝构造
    Sofa(Sofa &other):price(other.price),size(new int(*other.size)){}
    //拷贝赋值
    Sofa &operator=(Sofa const &other)
    {
        price=other.price;
        size=new int(*other.size);
        return *this;
    }
    void show()
    {
        cout << "Sofa   " << price << "¥    " << *size << "大小" << endl;
    }
};

class Bad{
private:
    int len;
    int* wide;
public:
    //无参构造
    Bad(){}
    //有参构造
    Bad(int len,int wide):len(len),wide(new int(wide)){}
    //析构
    ~Bad()
    {
        delete wide;
    }
    //拷贝构造
    Bad(Bad &other):len(other.len),wide(new int(*other.wide)){}
    //拷贝赋值
    Bad &operator=(Bad const &other)
    {
        len=other.len;
        wide=new int(*other.wide);
        return *this;
    }
    void show()
    {
        cout << "Bad   " << len << "长度    " << *wide << "宽度" << endl;
    }
};

class Sofa_Bad:public Sofa,protected Bad
{
private:
    string name;
    string brand;
public:
    //无参构造
    Sofa_Bad(){}
    //有参构造
    Sofa_Bad(int p,int size,int len,int wide,string name,string brand):Sofa(p,size),Bad(len,wide),name(name),brand(brand){}
    //析构
//    ~Sofa_Bad(){}
    //拷贝构造
    Sofa_Bad(Sofa_Bad &other):Sofa(other),Bad(other),name(other.name),brand(other.brand){}
    //拷贝赋值
    Sofa_Bad &operator=(Sofa_Bad const &other)
    {
        name=other.name;
        brand=other.brand;
        Bad::operator=(other);
        Sofa::operator=(other);
        return *this;
    }
    void show()
    {
        Sofa::show();
        Bad::show();
        cout << "Sofa_Bad   " << name << "名    " << brand << "品牌" << endl;
    }
};

int main()
{
    Sofa_Bad s0;
    Sofa_Bad s1(120,25,180,150,"good","scpy");
    s1.show();
    s0=s1;
    s0.show();
    Sofa_Bad s2(s1);
    s2.show();
    return 0;
}

2023.10.11_第2张图片

你可能感兴趣的:(算法)