虚函数的意义
1 /* 虚函数的意义 */
2
3 #include<iostream>
4 #include<cstdlib>
5
6 using namespace std;
7
8
9 class myinterface
10 {
11 public:
12 virtual void server()
13 {
14 cout << "请找奶妈" << endl;
15 }
16 };
17
18
19 class apple : public myinterface
20 {
21 public:
22 void server()
23 {
24 cout << "请吃苹果" << endl;
25 }
26 };
27
28 class orange : public myinterface
29 {
30 public:
31 void server()
32 {
33 cout << "请吃橘子" << endl;
34 }
35 };
36
37 class cow : public myinterface
38 {
39 public:
40 void server()
41 {
42 cout << "请吃奶牛的奶" << endl;
43 }
44 };
45
46
47 class rennai : public myinterface
48 {
49 public:
50 void server()
51 {
52 cout << "请吃奶牛妈的奶" << endl;
53 }
54 };
55
56
57
58 void main()
59 {
60 myinterface *p = nullptr;
61
62 p = new cow;
63
64 p->server();// 一个接口实现很多个功能
65
66 cin.get();
67 }
长风破浪会有时,直挂云帆济沧海
posted on 2015-06-09 23:31 Dragon-wuxl 阅读(143) 评论(0) 收藏 举报
浙公网安备 33010602011771号