• C++中的多态


    多态之前需要的知识: 

    C/C++ 中的指针[非常全面]

    C++中的继承 

     

    多态性

    在深入本章之前,您应该对指针和类继承有一个正确的理解。如果你不是很清楚下列任何一个表达的意思,你应该复习这部分。

    指向基类的指针

    类继承的一个关键特性是指向派生类的指针与指向其基类的指针是类型兼容的。多态性是利用这个简单但强大和多用途特性的艺术。

    关于矩形(rectangle)和三角形(triangle)类的示例可以使用指针重写,并考虑到以下特性:

    1. // 指向基类的指针 || pointers to base class
    2. #include
    3. using namespace std;
    4. class Polygon {
    5. protected:
    6. int width, height;
    7. public:
    8. void set_values (int a, int b)
    9. { width=a; height=b; }
    10. };
    11. class Rectangle: public Polygon {
    12. public:
    13. int area()
    14. { return width*height; }
    15. };
    16. class Triangle: public Polygon {
    17. public:
    18. int area()
    19. { return width*height/2; }
    20. };
    21. int main () {
    22. Rectangle rect;
    23. Triangle trgl;
    24. Polygon * ppoly1 = ▭
    25. Polygon * ppoly2 = &trgl;
    26. ppoly1->set_values (4,5);
    27. ppoly2->set_values (4,5);
    28. cout << rect.area() << '\n';
    29. cout << trgl.area() << '\n';
    30. return 0;
    31. }

    函数 main 声明了两个指向 Polygon (基类)的指针(命名为 ppoly1和 ppoly2)。它们分别分配 rect 和 trgl 的地址,这两个地址是 Recangle 和 Triangle 类型的对象。这样的分配是有效的,因为矩形和三角形都是从Polygon派生的类。

    解引用 ppoly1和 ppoly2(使用 ppoly1-> 和 ppoly2->)是有效的,并允许我们访问它们的指向对象的成员。例如,下面两个语句在前面的示例中是等效的:

    ppoly1->set_values (4,5);
    rect.set_values (4,5); 


    但是因为 ppoly1和 ppoly2的类型都是指向 Polygon 的指针(而不是指向矩形或三角形的指针) ,所以只能访问从 Polygon 继承的成员,而不能访问派生类 Recangle 和 Triangle 的成员。这就是为什么上面的程序使用 rect 和 trgl 直接访问这两个对象的面积成员,而不是使用指针; 指向基类的指针不能访问派生类特有的area()。

    如果 area() 是 Polygon 的成员而不是它的派生类的成员,那么成员area()可以通过指向 Polygon 的指针来访问,但问题是 Recangle 和 Triangle 实现了不同版本的 area,因此不存在一个可以在基类中实现的通用版本。

    虚成员

    虚成员是可以在派生类中重新定义的成员函数,同时通过引用保留其调用属性。函数成为 虚函数的语法是在其声明之前使用 Virtual 关键字:

    1. // 虚成员 || virtual members
    2. #include
    3. using namespace std;
    4. class Polygon {
    5. protected:
    6. int width, height;
    7. public:
    8. void set_values (int a, int b)
    9. { width=a; height=b; }
    10. virtual int area ()
    11. { return 0; }
    12. };
    13. class Rectangle: public Polygon {
    14. public:
    15. int area ()
    16. { return width * height; }
    17. };
    18. class Triangle: public Polygon {
    19. public:
    20. int area ()
    21. { return (width * height / 2); }
    22. };
    23. int main () {
    24. Rectangle rect;
    25. Triangle trgl;
    26. Polygon poly;
    27. Polygon * ppoly1 = ▭
    28. Polygon * ppoly2 = &trgl;
    29. Polygon * ppoly3 = &poly;
    30. ppoly1->set_values (4,5);
    31. ppoly2->set_values (4,5);
    32. ppoly3->set_values (4,5);
    33. cout << ppoly1->area() << '\n';
    34. cout << ppoly2->area() << '\n';
    35. cout << ppoly3->area() << '\n';
    36. return 0;
    37. }

     

    • 在此示例中,所有三个类(Polygon、 Recangle 和 Triangle)具有相同的成员: width、 height 和 set _ value 和 area 函数。
    • 成员函数区域已在基类中声明为虚函数,因为它稍后将在每个派生类中重新定义。非虚成员也可以在派生类中重新定义,但是派生类的非虚成员不能通过基类的引用访问: 例如,如果在上面的示例中从area()的声明中删除了virtual,那么对area()的所有三次调用都将返回零,因为在所有情况下,基类的版本都会被调用。
    • 因此,virtual关键字实际上是允许从指针适当地调用与基类名相同的派生类的成员,而且更精确地说,当指针的类型是指向派生类对象的基类的指针时,如上例所示。
    • 声明或继承虚函数的类称为多态类。
    • 注意,尽管 Polygon 的一个成员是虚拟的,但它是一个常规类,甚至可以实例化一个对象(poly) ,它有自己的成员area()定义,总是return 0;。

    抽象基类

    抽象基类与前面示例中的 Polygon 类非常相似。它们是只能用作基类的类,因此允许具有没有定义的虚成员函数(称为纯虚函数)。语法是将它们的定义替换为 = 0(一个等号和一个零) :

    一个抽象的基础 Polygon 类可以是这样的:

    1. // 抽象的基类 || abstract class CPolygon
    2. class Polygon {
    3. protected:
    4. int width, height;
    5. public:
    6. void set_values (int a, int b)
    7. { width=a; height=b; }
    8. virtual int area () =0;
    9. };

     
    注意,这个area没有定义; 它已经被 = 0替换,这使它成为一个纯虚函数。包含至少一个纯虚函数的类称为抽象基类。

    因此,Polygon的最后一个抽象基类版本不能用来声明如下对象:

     Polygon mypolygon; // 如果Polygon是抽象基类则不工作


    但是,抽象基类并非完全无用。它可以用来创建指向它的指针,并利用它的所有多态能力。例如,下列指针声明将是有效的:

    Polygon * ppoly1;
    Polygon * ppoly2; 

     


    当指向派生(非抽象)类的对象时,它实际上可以被解引用:

    1. // 抽象基类 || abstract base class
    2. #include
    3. using namespace std;
    4. class Polygon {
    5. protected:
    6. int width, height;
    7. public:
    8. void set_values (int a, int b)
    9. { width=a; height=b; }
    10. virtual int area (void) =0;
    11. };
    12. class Rectangle: public Polygon {
    13. public:
    14. int area (void)
    15. { return (width * height); }
    16. };
    17. class Triangle: public Polygon {
    18. public:
    19. int area (void)
    20. { return (width * height / 2); }
    21. };
    22. int main () {
    23. Rectangle rect;
    24. Triangle trgl;
    25. Polygon * ppoly1 = ▭
    26. Polygon * ppoly2 = &trgl;
    27. ppoly1->set_values (4,5);
    28. ppoly2->set_values (4,5);
    29. cout << ppoly1->area() << '\n';
    30. cout << ppoly2->area() << '\n';
    31. return 0;
    32. }

    输出结果:

    20
    10
    


    在本例中,使用唯一类型的指针(Polygon *)引用不同但相关类型的对象(派生),并且每次都调用适当的成员函数,因为它们是虚拟的。这在某些情况下非常有用。例如,抽象基类 Polygon 的成员甚至可以使用特殊指针 this 来访问正确的虚拟成员(基类指针指向不同的派生类对象,就会调用相应对象的重写虚函数的函数,其实this指针就是指向不同的派生对象的虚表(虚表中维持着相应对象的重写的继承基类的虚函数),括号内的内容可以忽视!!!),即使 Polygon 本身没有这个函数的实现:

    1. // 可以从抽象基类 调用纯虚成员
    2. #include
    3. using namespace std;
    4. class Polygon {
    5. protected:
    6. int width, height;
    7. public:
    8. void set_values (int a, int b)
    9. { width=a; height=b; }
    10. virtual int area() =0;
    11. void printarea()
    12. { cout << this->area() << '\n'; }
    13. };
    14. class Rectangle: public Polygon {
    15. public:
    16. int area (void)
    17. { return (width * height); }
    18. };
    19. class Triangle: public Polygon {
    20. public:
    21. int area (void)
    22. { return (width * height / 2); }
    23. };
    24. int main () {
    25. Rectangle rect;
    26. Triangle trgl;
    27. Polygon * ppoly1 = ▭
    28. Polygon * ppoly2 = &trgl;
    29. ppoly1->set_values (4,5);
    30. ppoly2->set_values (4,5);
    31. ppoly1->printarea();
    32. ppoly2->printarea();
    33. return 0;
    34. }

     输出结果:

    20
    10

    虚拟成员和抽象类赋予 C + + 多态特性,这对于面向对象的项目非常有用。当然,上面的例子是非常简单的用例,但是这些特性可以应用于对象数组或动态分配的对象:

     

    1. // 动态分配 与 多态
    2. #include
    3. using namespace std;
    4. class Polygon {
    5. protected:
    6. int width, height;
    7. public:
    8. Polygon (int a, int b) : width(a), height(b) {}
    9. virtual int area (void) =0;
    10. void printarea()
    11. { cout << this->area() << '\n'; }
    12. };
    13. class Rectangle: public Polygon {
    14. public:
    15. Rectangle(int a,int b) : Polygon(a,b) {}
    16. int area()
    17. { return width*height; }
    18. };
    19. class Triangle: public Polygon {
    20. public:
    21. Triangle(int a,int b) : Polygon(a,b) {}
    22. int area()
    23. { return width*height/2; }
    24. };
    25. int main () {
    26. Polygon * ppoly1 = new Rectangle (4,5);
    27. Polygon * ppoly2 = new Triangle (4,5);
    28. ppoly1->printarea();
    29. ppoly2->printarea();
    30. delete ppoly1;
    31. delete ppoly2;
    32. return 0;
    33. }

    输出结果:

    20
    10

    参考地址:https://cplusplus.com/doc/tutorial/polymorphism/ 

  • 相关阅读:
    QT基础教学(QT对象间的关系)
    【最小生成树的用法总结】
    文举论金:黄金原油全面走势分析策略独家指导
    shapely 笔记:STR TREE
    机器学习实验------密度聚类方法之DB-Scan
    ES6------04let经典案例实现
    学生问的一道CSS3媒体查询,实现响应式设计的题
    SAP SALV14 增强SALV使SALV支持列级别、行级别、单元格级别的编辑模式切换
    含文档+PPT+源码等]精品基于PHP实现的网上买卖管理系统[包运行成功]计算机PHP毕业设计项目源码
    Unity-协同程序
  • 原文地址:https://blog.csdn.net/weixin_58045467/article/details/127711047