c++多态小程序,求达人解决
1 #include<iostream>
2 using namespace std;
3 #include<string>
4 class Animal{
5 string name;
6 public:
7 virtual void eat()
8 {
9 cout<<"I don't know"<<endl;
10 }
11 virtual void sleep()
12 {
13 cout<<"animal sleep"<<endl;
14 }
15 virtual void shout()
16 {
17 cout<<"animal shout"<<endl;
18 }
19 };
20 class Cat:public Animal{
21 public:
22 void eat()
23 {
24 cout<<"Cat eat mouse"<<endl;
25 }
26 void sleep()
27 {
28 cout<<"Cat sleep on bed"<<endl;
29 }
30 void shout()
31 {
32 cout<<"Cat miao miao jiao"<<endl;
33 }
34 };
35 class Dog:public Animal{
36 public:
37 void eat()
38 {
39 cout<<"dog gutou"<<endl;
40 }
41 void sleep()
42 {
43 cout<<"gou wo sleep"<<endl;
44 }
45 void shout()
45 void shout()
46 {
47 cout<<"dog is wangwang jiao"<<endl;
48 }
49 };
50 class JiaFei:public Cat{
51 public:
52 void eat()
53 {
54 cout<<"Jia Fei like itali mian"<<endl;
55 }
56 void sleep()
57 {
58 cout<<"Sleep on safa"<<endl;
59 }
60 void shout()
61 {
62 cout<<"good afternoon"<<endl;
63 }
64 };
65 class Player{
66 string name;
67 public:
68 Player(string n):name(n){}
69 void play(Animal* p)
70 {
71 p->eat();
72 p-sleep();
73 p->shout();
74 }
75 };
76 int main()
77 {
78 //cout<<sizeof(Animal)<<endl;
79 Cat c;
80 Dog d;
81 JiaFei j;
82 Player p1("zhaodekui");
83 Player p2("xielaoshi");
84 p1.play(&c);
85 p2.play(&d);
86 p2.play(&j);
87 }
an.cc: In member function `void Player::play(Animal*)':
/usr/include/unistd.h:390: error: too few arguments to function `unsigned int sl eep(unsigned int)'
an.cc:72: error: at this point in file
[解决办法]
72 p-sleep();--->p->sleep();
[解决办法]
围观达人
LS也就犀利了,这种格式都可以一眼看出,膜拜
[解决办法]