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;
}
相关推荐
Yurko1321 小时前
【C语言】基本语法结构(上篇)
c语言·开发语言·学习
草莓熊Lotso21 小时前
《C++ Stack 与 Queue 完全使用指南:基础操作 + 经典场景 + 实战习题》
开发语言·c++·算法
孤客网络科技工作室21 小时前
Python - 100天从新手到大师:第五十七天获取网络资源及解析HTML页面
开发语言·python·html
武文斌7721 小时前
复习总结最终版:计算机网络
linux·开发语言·学习·计算机网络
敲上瘾21 小时前
单序列和双序列问题——动态规划
c++·算法·动态规划
ajassi200021 小时前
开源 C++ QT QML 开发(二十二)多媒体--ffmpeg编码和录像
c++·qt·开源
太过平凡的小蚂蚁21 小时前
策略模式:让算法选择像点菜一样简单
算法·策略模式
帅大大的架构之路21 小时前
高级篇:Python脚本(101-150)
开发语言·python
reasonsummer1 天前
【办公类-115-06】20250920职称资料上传04——docx复制、docx转PDF(课程表11个)
开发语言·windows·python·c#
栀寒老醑1 天前
Python实现的服务器日志监控脚本
开发语言·python