Problem C: 动物要吃饭

Problem C: 动物要吃饭

Time Limit: 1 Sec  Memory Limit: 128 MB
Submit: 1356  Solved: 1226
[Submit][Status][Web Board]

Description

定义Animal类,只有一个纯虚函数eat。

定义Dog、Cat、Sheep、Chicken四个类,它们都是Animal的子类。

每个类重载eat方法,每个方法输出的内容见样例。

Input

一系列0~3之内的整数。

Output

每个输入对应一行输出,0、1、2、3分别代表Dog、Cat、Sheep、Chicken。

Sample Input

01232310

Sample Output

Dog eats bone.Cat eats fish.Sheep eats grass.Chicken eats worm.Sheep eats grass.Chicken eats worm.Cat eats fish.Dog eats bone.

HINT

 

Append Code

append.cc,

 

[Submit][Status][Web Board]

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

#include <iostream>

#include <iostream>

#include <iomanip>

#include <stdio.h>

#include <cstring>

#include <cmath>

#include <list>

#include <vector>

using namespace std;

class Animal

{

public:

    virtual void eat()const = 0;

};

class Dog:public Animal

{

   public:

       void eat()const{cout<<"Dog eats bone."<<endl;}

};

class Cat:public Animal

{

   public:

       void eat()const{cout<<"Cat eats fish."<<endl;}

};

class Sheep:public Animal

{

   public:

       void eat()const{cout<<"Sheep eats grass."<<endl;}

};

class Chicken:public Animal

{

   public:

       void eat()const{cout<<"Chicken eats worm."<<endl;}

};

int main()

{

    int c;

    vector<Animal*> animals;

    vector<Animal*>::iterator itr;

    while(cin>>c)

    {

        switch(c)

        {

        case 0 :

            animals.push_back(new Dog());

            break;

        case 1:

            animals.push_back(new Cat());

            break;

        case 2:

            animals.push_back(new Sheep());

            break;

        case 3:

            animals.push_back(new Chicken());

        }

    }

    for (itr = animals.begin(); itr != animals.end(); itr++)

        (*itr)->eat();

    return 0;

}

 

上一篇:继承: .单继承


下一篇:面向对象之多态[向上/向下]转型