qfedu-cpp-level/day6/homework/h4.cpp

40 lines
643 B
C++
Raw Permalink Normal View History

2023-08-01 09:07:49 +08:00
// 创建一个基类 Animal其中包含一个纯虚函数 move() ,用于描述动物的移动方式。从 Animal 派生出 Bird 类和 Fish 类,分别实现不同类型动物的移动方式。
#include <iostream>
#include <cstring>
#include <cstdlib>
using namespace std;
class Animal
{
public:
virtual void move() = 0;
};
class Bird : public Animal
{
public:
void move()
{
cout << "Bird" << endl;
}
};
class Fish : public Animal
{
public:
void move()
{
cout << "Fish" << endl;
}
};
int main()
{
Animal *p = new Bird;
p->move();
p = new Fish;
p->move();
return 0;
}