10.11作业

多继承代码实现沙发床

cpp 复制代码
#include <iostream>

using namespace std;

class Sofa {
  private:
    int h;
  public:
    Sofa() {
        cout << "Sofa无参构造" << endl;
    }
    Sofa(int h): h(h) {
        cout << "Sofa有参构造" << endl;
    }
    Sofa(const Sofa& other): h(other.h) {
        cout << "Sofa拷贝构造" << endl;
    }
    Sofa& operator=(const Sofa& other) {
        cout << "Sofa赋值拷贝" << endl;
        h = other.h;
        return *this;
    }
    ~Sofa() {
        cout << "Sofa析构函数" << endl;
    }
};
class Bad {
  private:
    int w;
  public:
    Bad() {
        cout << "Bad无参构造" << endl;
    }
    Bad(int w): w(w) {
        cout << "Bad有参构造" << endl;
    }
    Bad(const Bad& other): w(other.w) {
        cout << "Bad拷贝构造" << endl;
    }
    Bad& operator=(const Bad& other) {
        cout << "Bad赋值拷贝" << endl;
        w = other.w;
        return *this;
    }
    ~Bad() {
        cout << "Bad析构函数" << endl;
    }
};
class Sofa_Bad: public Sofa, protected Bad {
  private:
    string name;
  public:
    Sofa_Bad() {
        cout << "Sofa_Bad无参构造" << endl;
    }
    Sofa_Bad(string name, int h, int w): Sofa(h), Bad(w), name(name) {
        cout << "Sofa_Bad有参构造" << endl;
    }
    Sofa_Bad(const Sofa_Bad& other): Sofa(other), Bad(other), name(other.name) {
        cout << "Sofa_Bad拷贝构造" << endl;
    }
    Sofa_Bad& operator=(const Sofa_Bad& other) {
        Sofa::operator=(other);
        Bad::operator=(other);
        name = other.name;
        cout << "Sofa_Bad赋值拷贝" << endl;
        return *this;
    }
    ~Sofa_Bad() {
        cout << "Sofa_Bad析构函数" << endl;
    }
};
int main() {
    Sofa_Bad s1;
    Sofa_Bad s2("aaaa", 100, 200);
    Sofa_Bad s3(s2);
    s1 = s3;
    return 0;
}
相关推荐
爱上妖精的尾巴11 小时前
6-4 WPS JS宏 不重复随机取值应用
开发语言·前端·javascript
fie888912 小时前
NSCT(非下采样轮廓波变换)的分解和重建程序
算法
小鸡吃米…12 小时前
Python 列表
开发语言·python
kaikaile199512 小时前
基于C#实现一维码和二维码打印程序
开发语言·c#
晨晖212 小时前
单链表逆转,c语言
c语言·数据结构·算法
我不是程序猿儿13 小时前
【C#】画图控件的FormsPlot中的Refresh功能调用消耗时间不一致缘由
开发语言·c#
rit843249913 小时前
C# Socket 聊天室(含文件传输)
服务器·开发语言·c#
kk哥889913 小时前
C++ 对象 核心介绍
java·jvm·c++
helloworddm13 小时前
WinUI3 主线程不要执行耗时操作的原因
c++
嘉琪00113 小时前
Vue3+JS 高级前端面试题
开发语言·前端·javascript