目录
1>思维导图
2> 多继承代码实现沙发床
#include <iostream>
using namespace std;
//创建沙发类
class sofa
{
private:
string sitting;
public:
sofa(){
cout << "sofa的无参构造函数" << endl;
}
sofa(string s):sitting(s)
{
cout << "sofa的有参构造函数" << endl;
}
//拷贝构造函数
sofa(const sofa &other):sitting(other.sitting)
{
cout << "sofa的拷贝构造函数" << endl;
}
//拷贝赋值函数
sofa &operator=(const sofa &other)
{
cout << "sofa的拷贝赋值函数" << endl;
sitting=other.sitting;
return *this;
}
//析构函数
~sofa()
{
cout << "sofa的析构函数" << endl;
}
void show()
{
cout << "sofa" << endl;
}
};
//创建床类
class bed
{
protected:
string sleeping;
public:
bed(){
cout << "bed的无参构造函数" << endl;
}
bed(string b):sleeping(b)
{
cout << "bed的有参构造函数" << endl;
}
//拷贝构造函数
bed(const bed &other):sleeping(other.sleeping)
{
cout << "bed的拷贝构造函数" << endl;
}
//拷贝赋值函数
bed &operator=(const bed &other)
{
cout << "bed的拷贝赋值函数" << endl;
sleeping=other.sleeping;
return *this;
}
//析构函数
~bed()
{
cout << "bed的析构函数" << endl;
}
void show()
{
cout << "bed" << endl;
}
};
//构造沙发床类
class sofa_bed: public bed,public sofa
{
private:
string color;
public:
sofa_bed(){
cout << "sofa_bed的无参构造函数" << endl;
}
sofa_bed(string s,string b,string c):bed(b),sofa(s),color(c)
{
cout << "sofa_bed的有参构造函数" << endl;
}
//拷贝构造函数
sofa_bed(const sofa_bed &other):bed(other.sleeping),sofa(other),color(other.color)
{
cout << "sofa_bed的拷贝构造函数" << endl;
}
//拷贝赋值函数
sofa_bed &operator=(const sofa_bed &other)
{
cout << "sofa_bed的拷贝赋值函数" << endl;
color=other.color;
sofa::operator=(other);
sleeping=other.sleeping;
return *this;
}
//析构函数
~sofa_bed()
{
cout << "sofa_bed的析构函数" << endl;
}
void show()
{
cout << "sofa_bed的show" << endl;
}
};
int main()
{
cout << "Hello World!" << endl;
sofa_bed s1;
sofa_bed s2("坐着","睡觉","yellow");
s2.show();
sofa_bed s3(s2);
s1=s2;
return 0;
}
效果图 :