(一)代码
#include <iostream>
#include <string.h>
#include <unistd.h>
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(int argc, char **argv)
{
Sofabed s;
s.watchTV();
s.sleep();
return 0;
}
解析:Sofabed继承了Sofa和bed类的功能

本文介绍了一个C++代码示例,展示了如何使用多重继承实现类的功能组合。通过Sofabed类同时继承Sofa和Bed类,实现了观看电视和睡觉的功能。
1135

被折叠的 条评论
为什么被折叠?



