(一)参考原文链接:C++多态
(二)
抽象类
在介绍抽象类之前,我们先介绍一下纯虚函数。
1.纯虚函数
在基类中仅仅给出声明,不对虚函数实现定义,而是在派生类中实现。这个虚函数称为纯虚函数。普通函数如果仅仅给出它的声明而没有实现它的函数体,这是编译不过的。纯虚函数没有函数体。
纯虚函数需要在声明之后加个=0;
class <基类名>
{
virtual <类型><函数名>(<参数表>)=0; ......
};
2.抽象类
含有纯虚函数的类被称为抽象类。抽象类只能作为派生类的基类,不能定义对象,但可以定义指针。在派生类实现该纯虚函数后,定义抽象类对象的指针,并指向或引用子类对象。
1)在定义纯虚函数时,不能定义虚函数的实现部分;
2)在没有重新定义这种纯虚函数之前,是不能调用这种函数的。
抽象类的唯一用途是为派生类提供基类,纯虚函数的作用是作为派生类中的成员函数的基础,并实现动态多态性。继承于抽象类的派生类如果不能实现基类中所有的纯虚函数,那么这个派生类也就成了抽象类。因为它继承了基类的抽象函数,只要含有纯虚函数的类就是抽象类。纯虚函数已经在抽象类中定义了这个方法的声明,其它类中只能按照这个接口去实现。
3.接口和抽象类的区别
1)C++中我们一般说的接口,表示对外提供的方法,提供给外部调用。是沟通外部跟内部的桥梁。也是以类的形式提供的,但一般该类只具有成员函数,不具有数据成员;
2)抽象类可以既包含数据成员又包含方法。

(三)示例

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
38
39 int main(int argc, char **argv)
40 {
41 //Human h; /*抽象类不能实例化对象*/
42 //Englishman e; /*子类没有覆写完全部的存虚函数,则子类也是抽象类,则不能实例化对象*/
43 Chinese c;
44
45 return 0;
46 }
(四)在子类中没有编写存虚函数的,可在派生类中编写,这样可以编译通过

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 guangxi car"<<endl; }
39 };
40
41 int main(int argc, char **argv)
42 {
43 //Human h;
44 Englishman e;
45 Guangximan g;
46
47 return 0;
48 }

来源:https://www.cnblogs.com/luxiaoguogege/p/9699830.html
