第四节 · 遗产分配 · 访问控制与派生类作用域
class Flyable
{
public:
void fly()
{
cout << "Flying in the sky" << endl;
}
};
class Swimmable
{
public:
void swim()
{
cout << "Swimming in water" << endl;
}
};
// 鸭子既能飞又能游
class Duck : public Flyable, public Swimmable
{
public:
void quack()
{
cout << "Quack quack!" << endl;
}
};
int main()
{
Duck d;
d.fly(); // 继承自 Flyable
d.swim(); // 继承自 Swimmable
d.quack(); // 自己的方法
return 0;
}习题
Last updated