5,530
社区成员




#include <stdio.h>
#include <vector>
class Flyable
{
public:
virtual void fly()= 0;
virtual ~Flyable(){}
};
class FlyWithSwings : public Flyable
{
public:
void fly()
{
puts("I believe I can fly");
}
protected:
//因为FlyWithSwings的实例没有意义,这个类是为了被继承的
~FlyWithSwings(){};
};
class FlyNoWay : public Flyable
{
public:
void fly()
{
puts("I wish I could, but I can't");
}
protected:
//因为FlyWithSwings的实例没有意义,这个类是为了被继承的
~FlyNoWay(){};
};
class Eagle : public FlyWithSwings
{};
class Duck : public FlyNoWay
{};
//针对接口编程
void goFly(std::vector<Flyable*> const& fv)
{
for(auto f: fv)
{
f->fly();
}
}
int main (void)
{
Eagle e;
Duck d;
std::vector<Flyable*> fv(2);
fv[0] = &e, fv[1] = &d;
goFly(fv);
return 0;
}