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

40 lines
643 B
C++
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// 创建一个基类 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;
}