本文介绍: 【代码】C++12.4。
 沙发床的多继承
 代码
#include <iostream>

using namespace std;

//封装 沙发 类
class Sofa
{
private:
    string sitting;
    double *size;
public:
    //无参构造函数
    Sofa() {cout << "Sofa::无参构造函数" << endl;}

    //有参构造函数
    Sofa(string s,double si):sitting(s),size(new double(si))
    {cout << "Sofa::有参构造函数" << endl;}

    //拷贝构造函数
    Sofa(const Sofa &amp;other):sitting(other.sitting),size(other.size)
    {
        cout << "Sofa::拷贝构造函数" << endl;
    }

    //拷贝赋值函数
    Sofa &amp;operator=(const Sofa &amp;other)
    {
        if(this != &amp;other)
        {
        cout << "Sofa::拷贝赋值函数" << endl;
        this-&gt;sitting = other.sitting;
        this->size = other.size;
        }
        return *this;
    }

    //析构函数
    ~Sofa()
    {
        delete(size);
        size = nullptr;
        cout << "Sofa::析构函数" << endl;
    }

    void show()
    {
        cout << sitting << "  " << *size << endl;
    }
};

//封装 床 类
class Bed
{
private:
    string sleep;
public:
    //无参构造函数
    Bed() {cout << "Bed::无参构造函数" << endl;}

    //有参构造函数
    Bed(string s):sleep(s)
    {cout << "Bed::有参构造函数" << endl;}

    //拷贝构造函数
    Bed(const Bed &amp;other):sleep(other.sleep)
    {
        cout << "Bed::拷贝构造函数" << endl;
    }

    //拷贝赋值函数
    Bed &amp;operator=(const Bed &amp;other)
    {
        cout << "Bed::拷贝赋值函数" << endl;
        this->sleep = other.sleep;
        return *this;
    }

    //析构函数
    ~Bed()
    {
        cout << "Bed::析构函数" << endl;
    }

    void show()
    {
        cout << sleep << endl;
    }
};

//封装 沙发床 类 共有继承沙发和床
class Sofa_bed:public Sofa,public Bed
{
private:
    string color;
public:
    //无参构造函数
    Sofa_bed(){cout << "Sofa_bed::无参构造函数" << endl;}

    //有参构造函数
    Sofa_bed(string s, double size,string sl, string c):Sofa(s,size), Bed(sl),color(c)
    {
        cout << "Sofa_bed::有参构造函数" << endl;
    }

    //拷贝构造函数     深拷贝
    Sofa_bed(const Sofa_bed &amp;other):Sofa(other),Bed(other),color(other.color)
    {
        cout << "Sofa_bed::拷贝构造函数" << endl;
    }

    //拷贝赋值函数
    Sofa_bed &amp;operator=(const Sofa_bed &amp;other)
    {
        cout << "Sofa::拷贝赋值函数" << endl;
        this->color = other.color;
        this->Sofa::operator=(other);
        this->Bed::operator=(other);
        return *this;
    }
    //析构函数
    ~Sofa_bed()
    {
        cout << "Sofa_bed::析构函数" << endl;
    }

    void show()
    {
        Sofa::show();
        Bed::show();
        cout << color << endl;
    }
};
int main()
{
    Sofa_bed sb("可以坐", 180, "可以睡", "灰不溜秋");
    cout << "++++++++++++++++++++++++++++++++++++++" << endl;
    sb.Sofa::show();
    cout << "++++++++++++++++++++++++++++++++++++++" << endl;
    sb.Bed::show();
    cout << "++++++++++++++++++++++++++++++++++++++" << endl;
    sb.show();
    cout << "++++++++++++++++++++++++++++++++++++++" << endl;
    return 0;
}
运行效果图

思维导图

原文地址:https://blog.csdn.net/qq_65427718/article/details/134791696

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任

如若转载,请注明出处:http://www.7code.cn/show_42120.html

如若内容造成侵权/违法违规/事实不符,请联系代码007邮箱suwngjj01@126.com进行投诉反馈,一经查实,立即删除

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注