40 lines
643 B
C++
40 lines
643 B
C++
// 创建一个基类 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;
|
||
}
|