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;
}
相关推荐
weixin_4373982119 分钟前
转Go学习笔记
linux·服务器·开发语言·后端·架构·golang
津津有味道28 分钟前
Qt C++串口SerialPort通讯发送指令读写NFC M1卡
linux·c++·qt·串口通信·serial·m1·nfc
StrongerIrene28 分钟前
rs build 的process.env的值undefined解决方案
开发语言·javascript·ecmascript
风逸hhh40 分钟前
python打卡day58@浙大疏锦行
开发语言·python
Q_970956391 小时前
java+vue+SpringBoo足球社区管理系统(程序+数据库+报告+部署教程+答辩指导)
java·开发语言·数据库
傅里叶的耶1 小时前
C++系列(二):告别低效循环!选择、循环、跳转原理与优化实战全解析
c++·visual studio
Vitta_U1 小时前
MFC的List Control自适应主界面大小
c++·list·mfc
为了更好的明天而战1 小时前
Java 中的 ArrayList 和 LinkedList 区别详解(源码级理解)
java·开发语言
JosieBook2 小时前
【Java编程动手学】Java中的数组与集合
java·开发语言·python
qq_589568102 小时前
element-plus按需自动导入的配置 以及icon图标不显示的问题解决
开发语言·javascript·ecmascript