抽象:
纯虚函数:
1.virtual函数声明时后面加上 "=0";
2.纯虚函数不需要定义
3.所有的纯虚函数都需要复写
1 #include <iostream>
2 #include <string.h>
3 #include <unistd.h>
4
5 using namespace std;
6
7 class Human{
8 private:
9 int a;
10 public:
11 virtual void eating(void) = 0;
12 virtual void wearing(void) = 0;
13 virtual void driving(void) = 0;
14 virtual ~Human() {cout <<"~Human()"<<endl;}
15 virtual Human *test (void){cout<<"Human's test"<<endl;return this;}
16
17 };
18 class Englishman : public Human{
19 public:
20 void eating(void){cout<<"use knife to eat"<<endl;}
21 void wearing(void){cout<<"wear english style"<<endl;}
22 void driving(void){cout<<"drive english car"<<endl;}
23 virtual ~Englishman(){cout<<"~Englishman()"<<endl;}
24 virtual Englishman* test(void){cout<<"Englishman's test"<<endl;return this;}
25
26
27 };
28 class Chinese : public Human{
29 public:
30 void eating(void){cout<<"use chopsticks to eat"<<endl;}
31 void wearing(void){cout<<"wear chinese style"<<endl;}
32 //void driving(void){cout<<"drive chinese car"<<endl;}
33 virtual ~Chinese(){cout<<"~Chinese()"<<endl;}
34 virtual Chinese* test(void){cout<<"Chinese's test"<<endl;return this;}
35
36 };
37 class Guangximan : public Chinese{
38 //void driving(void){cout<<"drive chinese car"<<endl;}
39 };
40 int main(int argc,char ** argv)
41 {
42 Englishman e;
43 Chinese c;
44
45 return 0;
46 }
View Code
1 #include <iostream>
2 #include <string.h>
3 #include <unistd.h>
4
5 using namespace std;
6
7 class Human{
8 private:
9 int a;
10 public:
11 virtual void eating(void) = 0;
12 virtual void wearing(void) = 0;
13 virtual void driving(void) = 0;
14 virtual ~Human() {cout <<"~Human()"<<endl;}
15 virtual Human *test (void){cout<<"Human's test"<<endl;return this;}
16
17 };
18 class Englishman : public Human{
19 public:
20 void eating(void){cout<<"use knife to eat"<<endl;}
21 void wearing(void){cout<<"wear english style"<<endl;}
22 void driving(void){cout<<"drive english car"<<endl;}
23 virtual ~Englishman(){cout<<"~Englishman()"<<endl;}
24 virtual Englishman* test(void){cout<<"Englishman's test"<<endl;return this;}
25
26
27 };
28 class Chinese : public Human{
29 public:
30 void eating(void){cout<<"use chopsticks to eat"<<endl;}
31 void wearing(void){cout<<"wear chinese style"<<endl;}
32 void driving(void){cout<<"drive chinese car"<<endl;}
33 virtual ~Chinese(){cout<<"~Chinese()"<<endl;}
34 virtual Chinese* test(void){cout<<"Chinese's test"<<endl;return this;}
35
36 };
37 int main(int argc,char ** argv)
38 {
39 Englishman e;
40 Chinese c;
41
42 return 0;
43 }
View Code
此代码运行结果为下图:
抽象类:
1.含有纯虚函数的类
2.抽象类不能有实例对象
3.若子类没有覆写所有的纯虚函数,则子类还是抽象类
抽象类界面:
1.程序分为:应用编程、类编程
2.抽象类给应用编程提供固定的接口
3.具体类由抽象类派生出来
4.具体类编为动态库
优点:
应用编程、类编程互不影响
main中包含相对固定的.h文件,此.h文件也叫做接口,具体类应该有次而来。
app
Human.h
Englishman.h Chinese.h
1 #include "Human.h"
2 //#include "Englishman.h"
3 //#include "Chinese.h"
4
5 void test_eating(Human *h)
6 {
7 h->eating();
8 }
9
10
11 int main(int argc, char **argv)
12 {
13 Human& e = CreateEnglishman("Bill", 10, "sfwqerfsdfas");//生成Englishman
14 Human& c = CreateChinese("zhangsan", 11, "beijing");//生成Chinese
15
16 Human* h[2] = {&e, &c};
17 int i;
18 for (i = 0; i < 2; i++)
19 test_eating(h[i]);
20
21 delete &e;
22 delete &c;
23
24
25 return 0;
26 }
27 main.cpp
28
29 #ifndef _HUMAN_H
30 #define _HUMAN_H
31
32 #include <iostream>
33 #include <string.h>
34 #include <unistd.h>
35
36 using namespace std;
37
38 class Human {
39 private:
40 char *name;
41
42 public:
43 void setName(char *name);
44 char *getName(void);
45 virtual void eating(void) = 0;
46 virtual void wearing(void) = 0;
47 virtual void driving(void) = 0;
48 virtual ~Human() {cout<<"~Human"<<endl;}
49
50 };
51
52 Human& CreateEnglishman(char *name, int age, char *address);
53 Human& CreateChinese(char *name, int age, char *address);
54
55 #endif
56 Human.h
57
58 #include "Human.h"
59
60 void Human::setName(char *name)
61 {
62 this->name = name;
63 }
64
65 char *Human::getName(void)
66 {
67 return this->name;
68 }
69 Human.cpp
70
71 #ifndef _ENGLISHMAN_H
72 #define _ENGLISHMAN_H
73
74 #include <iostream>
75 #include <string.h>
76 #include <unistd.h>
77
78 #include "Human.h"
79
80 using namespace std;
81
82 class Englishman : public Human {
83 private:
84 char address[100];
85 int age;
86 public:
87 void eating(void);
88 void wearing(void);
89 void driving(void);
90 Englishman();
91 Englishman(char *name, int age, char *address);
92 virtual ~Englishman();
93 };
94
95 #endif
96 English.h
97
98 #include "Englishman.h"
99
100
101 void Englishman::eating(void)
102 {
103 cout<<"use knife to eat"<<endl;
104 }
105
106 void Englishman::wearing(void)
107 {
108 cout<<"wear english style"<<endl;
109 }
110
111 void Englishman::driving(void)
112 {
113 cout<<"drive english car"<<endl;
114 }
115
116 Englishman::~Englishman()
117 {
118 cout<<"~Englishman()"<<endl;
119 }
120
121 Englishman::Englishman() {}
122 Englishman::Englishman(char *name, int age, char *address)
123 {
124 setName(name);
125 this->age = age;
126 memset(this->address, 0, 100);
127 strcpy(this->address, address);
128 }
129
130 Human& CreateEnglishman(char *name, int age, char *address)
131 {
132 return *(new Englishman(name, age, address));
133 }
134
135 English.cpp
136
137 #ifndef _CHINESE_H
138 #define _CHINESE_H
139
140 #include <iostream>
141 #include <string.h>
142 #include <unistd.h>
143
144 #include "Human.h"
145
146 using namespace std;
147
148 class Chinese : public Human{
149 public:
150 void eating(void);
151 void wearing(void);
152 void driving(void);
153 virtual ~Chinese();
154 };
155
156 #endif
157
158 Chinese.h
159
160 #include "Chinese.h"
161
162
163 void Chinese::eating(void)
164 {
165 cout<<"use chopsticks to eat"<<endl;
166 }
167
168 void Chinese::wearing(void)
169 {
170 cout<<"wear chinese style"<<endl;
171 }
172
173 void Chinese::driving(void)
174 {
175 cout<<"drive chinese car"<<endl;
176 }
177
178 Chinese::~Chinese()
179 {
180 cout<<"~Chinese()"<<endl;
181 }
182
183 Human& CreateChinese(char *name, int age, char *address)
184 {
185 return *(new Chinese());
186 }
187 Chinese.cpp
188
189 Human: main.o libHuman.so
190 g++ -o $@ $< -L./ -lHuman
191
192 %.o : %.cpp
193 g++ -fPIC -c -o $@ $<
194
195 libHuman.so : Englishman.o Chinese.o Human.o
196 g++ -shared -o $@ $^
197
198 clean:
199 rm -f *.o Human
200 Makefile
View Code
编译时使用本地库的命令:
LD_LIBRARY_PATH=./ ./Human
原文链接: https://www.cnblogs.com/yekongdebeijixing/p/12163788.html
欢迎关注
微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍
原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/191555
非原创文章文中已经注明原地址,如有侵权,联系删除
关注公众号【高性能架构探索】,第一时间获取最新文章
转载文章受原作者版权保护。转载请注明原作者出处!