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;
}
相关推荐
不想看见404几秒前
Power of Four二进制特性--力扣101算法题解笔记
数据结构·算法
做怪小疯子15 分钟前
Leetcode刷题——8.重叠区间
算法·leetcode·职场和发展
共享家952716 分钟前
Java入门(多态)
java·开发语言
机器视觉知识推荐、就业指导16 分钟前
拆 Qt,为什么要先引入libmodbus?
开发语言·qt
2401_8578652317 分钟前
C++模块接口设计
开发语言·c++·算法
蓝莓星冰乐25 分钟前
第一章:C语言概述与环境搭建
c语言·开发语言
add45a27 分钟前
嵌入式C++低功耗设计
开发语言·c++·算法
DeepModel30 分钟前
【概率分布】指数分布(Exponential Distribution)原理、推导与实战
python·算法·概率论
毕设源码-赖学姐30 分钟前
【开题答辩全过程】以 基于Java的婚礼策划平台的设计与实现为例,包含答辩的问题和答案
java·开发语言
_饭团34 分钟前
指针核心知识:5篇系统梳理3
c语言·数据结构·算法·leetcode·面试·学习方法·改行学it