2023.10.11

本文详细介绍了C++中的Sofa,Bad,和Sofa_Bad类,包括它们的无参构造、有参构造、析构函数以及拷贝构造和赋值操作。展示了如何创建和初始化这些类的对象。
摘要由CSDN通过智能技术生成

#include <iostream>

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值