#include <iostream>
using namespace std;
class Sofa{
private:
string setting;
string *lying = new string;
public:
Sofa(){
cout << "Sofa::无参构造函数" << endl;
}
Sofa(string setting,string lying):setting(setting),lying(new string (lying)){
cout << "Sofa::有参构造函数" << endl;
}
Sofa (const Sofa &other):setting(other.setting),lying(new string (*(other.lying))){
cout << "Sofa::拷贝构造函数" << endl;
}
Sofa &operator=(const Sofa &other){
if(this != &other){
setting = other.setting;
lying = other.lying;
}
cout << "Sofa::拷贝赋值函数" << endl;
return *this;
}
~Sofa(){
cout << "Sofa::析构函数" << endl;
}
};
class Bed{
private:
string sleep;
string *gameing = new string;
public:
Bed(){
cout << "Bed::无参构造函数" << endl;
}
Bed(string sleep,string gameing):sleep(sleep),gameing(new string(gameing)){
cout << "Bed::有参构造函数" << endl;
}
Bed(const Bed &other):sleep(other.sleep),gameing(new string(*(other.gameing))){
cout << "Bed::拷贝构造函数" << endl;
}
Bed &operator=(const Bed &other){
if(this != &other){
sleep = other.sleep;
gameing = other.gameing;
}
cout << "Bed::拷贝赋值函数" << endl;
return *this;
}
~Bed(){
cout << "Bed::析构函数" << endl;
}
};
class sofa_Bed:public Sofa,public Bed{
private:
string color;
public:
sofa_Bed(){
cout << "sofa_Bed::无参构造函数" << endl;
}
sofa_Bed(string setting,string lying,string sleep,string gameing,string color):Sofa(setting,lying),Bed(sleep,gameing),color(color){
cout << "sofa_Bed::有参构造函数" << endl;
}
sofa_Bed(const sofa_Bed &other):Sofa(other),Bed(other),color(other.color){
cout << "sofa_Bed::拷贝构造函数" << endl;
}
sofa_Bed &operator=(const sofa_Bed &other){
if(this != &other){
color = other.color;
Sofa::operator=(other);
Bed::operator=(other);
}
cout << "sofa_Bed::拷贝赋值函数" << endl;
return *this;
}
~sofa_Bed(){
cout << "sofa_Bed::析构函数" << endl;
}
};
int main()
{
sofa_Bed ab1;
cout << "========================" << endl;
sofa_Bed ab2("s","l","sl","wzry","black");
cout << "========================" << endl;
sofa_Bed ab3=ab2;
cout << "========================" << endl;
ab1=ab2;
cout << "========================" << endl;
cout << "程序已结束" << endl;
return 0;
}
运行结果:
思维导图: