多继承:
#include <iostream>
using namespace std;
class Sofa
{
private:
string sitting;
public:
//沙发的无参构造函数
Sofa()
{
cout << "沙发的无参构造函数" << endl;
}
//沙发的有参构造函数
Sofa(string sit):sitting(sit)
{
cout << "沙发的有参构造函数" << endl;
}
void show()
{
cout << sitting << endl;
}
};
class Bed
{
private:
string sleeping;
public:
//床的无参构造函数
Bed()
{
cout << "床的无参构造函数" << endl;
}
//床的有参构造函数
Bed(string sleep):sleeping(sleep)
{
cout << "床的有参构造函数" << endl;
}
void show()
{
cout << sleeping << endl;
}
};
class Sofa_Bed:public Sofa,public Bed
{
private:
string colour;
public:
//沙发床的无参构造函数
Sofa_Bed()
{
cout << "沙发床的无参构造函数" << endl;
}
//沙发床的有参构造函数
Sofa_Bed(string sit,string sleep,string colour):Sofa(sit),Bed(sleep),colour(colour)
{
cout << "沙发床的有参构造函数" << endl;
}
};
int main()
{
Sofa_Bed s1("可坐","可躺","pink");
s1.Sofa::show();
s1.Bed::show();
return 0;
}
思维导图: