10.c++面向对象编程-多重继承
·
- 多继承

- 实现这样的例子

- 测试程序
#include <stdio.h>
#include <iostream>
using namespace std;
class Sofa{
public:
void watchTV(void){
cout<<"watch TV"<<endl;
}
};
class Bed{
public:
void sleep(void){
cout<<"sleep"<<endl;
}
};
class Sofabed:public Sofa,public Bed{
};
int main()
{
Sofabed s;
s.sleep();
s.watchTV();
system("pause");
return 0;
}
- testdemo2

#include <stdio.h>
#include <iostream>
using namespace std;
class Furniture{
private:
int weight;
public:
void setWeight(int w){this->weight = w;}
int getWeight(void) const {return weight;}
};
class Sofa:virtual public Furniture{
public:
void watchTV(void){
cout<<"watch TV"<<endl;
}
};
class Bed:virtual public Furniture{
public:
void sleep(void){
cout<<"sleep"<<endl;
}
};
class Sofabed:public Sofa,public Bed{
};
int main()
{
Sofabed s;
s.sleep();
s.watchTV();
s.setWeight(100);
system("pause");
return 0;
}
更多推荐



所有评论(0)