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;
}
相关推荐
Joker-01111 小时前
深入 Go 底层原理(十二):map 的实现与哈希冲突
算法·go·哈希算法·map
lifallen2 小时前
深入解析RocksDB的MVCC和LSM Tree level
大数据·数据结构·数据库·c++·lsm-tree·lsm tree
鹿野素材屋2 小时前
C#中对于List的多种排序方式
开发语言·c#
君鼎2 小时前
Effective C++ 条款18:让接口容易被正确使用,不易被误用
c++
whxnchy2 小时前
C++刷题 - 7.27
开发语言·c++
金融小师妹2 小时前
AI量化模型解析黄金3300关口博弈:市场聚焦“非农数据”的GRU-RNN混合架构推演
大数据·人工智能·算法
金融小师妹2 小时前
基于LSTM-GRU混合网络的动态解析:美联储维稳政策与黄金单日跌1.5%的非线性关联
大数据·人工智能·算法
白日梦想家-K3 小时前
题单【模拟与高精度】
开发语言·c++·算法
岁忧3 小时前
(LeetCode 面试经典 150 题) 138. 随机链表的复制 (哈希表)
java·c++·leetcode·链表·面试·go
鹦鹉0073 小时前
IO流中的字节流
java·开发语言·后端