#include<iostream>
using namespace std;
class Bed
{
public:
Bed(){ cout << "Bed()" << endl; }
~Bed(){ cout << "~Bed()" << endl; }
void lying()
{
cout << "lying on bed" << endl;
}
};
class Sofa
{
public:
Sofa(){ cout << "Sofa()" << endl; }
~Sofa(){ cout << "~Sofa()" << endl; }
void lying()
{
cout << "lying on Sofa" << endl;
}
};
class SofaBed : public Bed, public Sofa //父类构造顺序与继承顺序一致,且不要重复继承同一个父类
{
public:
SofaBed(){ cout << "SofaBed()" << endl; }
~SofaBed(){ cout << "~SofaBed()" << endl; }
void lying()
{
cout << "lying on SofaBed" << endl;
}
};
void main()
{
SofaBed sofabed;
sofabed.Bed::lying();
sofabed.Sofa::lying();
sofabed.lying();
}
多继承
最新推荐文章于 2024-03-05 20:50:55 发布