多态按字面的意思就是多种形态。当类之间存在层次结构,并且类之间是通过继承关联时,就会用到多态。 C++ 多态意味着调用成员函数时,会根据调用函数的对象的类型来执行不同的函数。

下面的实例中,基类 Shape 被派生为两个类,如下所示:

  1. #include <iostream>
  2. using namespace std;
  3. class Shape
  4. {
  5. protected:
  6. int width, height;
  7. public:
  8. Shape(int a = 0, int b = 0)
  9. {
  10. width = a;
  11. height = b;
  12. }
  13. int area()
  14. {
  15. cout << "Parent class area" << endl;
  16. return 0;
  17. }
  18. };
  19. class Rectangle : public Shape
  20. {
  21. public:
  22. Rectangle(int a = 0, int b = 0) : Shape(a, b) {}
  23. int area()
  24. {
  25. cout << "Rectangle class area: " << width * height << endl;
  26. return (width * height);
  27. }
  28. };
  29. class Triangle : public Shape
  30. {
  31. public:
  32. Triangle(int a = 0, int b = 0) : Shape(a, b) {}
  33. int area()
  34. {
  35. cout << "Triangle class area: " << width * height / 2 << endl;
  36. return (width * height / 2);
  37. }
  38. };
  39. int main()
  40. {
  41. Shape *shape;
  42. Rectangle rec(10, 7);
  43. Triangle tri(10, 5);
  44. shape = &rec; // 存储矩形的地址
  45. shape->area(); // 调用矩形的求面积函数 area
  46. // rec.area(); //这样子输出是正确的 Rectangle class area
  47. shape = &tri; // 存储三角形的地址
  48. shape->area(); // 调用三角形的求面积函数 area
  49. // tri.area(); //这样子输出是正确的 Triangle class area
  50. return 0;
  51. }
  52. /*
  53. Parent class area
  54. Parent class area
  55. */

导致错误输出的原因是,调用函数 area() 被编译器设置为基类中的版本,这就是所谓的静态多态,或静态链接
函数调用在程序执行前就准备好了。有时候这也被称为早绑定,因为 area() 函数在程序编译期间就已经设置好了。
但现在,让我们对程序稍作修改,在 Shape 类中,area() 的声明前放置关键字 virtual,如下所示:

  1. #include <iostream>
  2. using namespace std;
  3. class Shape
  4. {
  5. protected:
  6. int width, height;
  7. public:
  8. Shape(int a = 0, int b = 0)
  9. {
  10. width = a;
  11. height = b;
  12. }
  13. + virtual int area() // ➕ ✅
  14. {
  15. cout << "Parent class area:" << endl;
  16. return 0;
  17. }
  18. };
  19. class Rectangle : public Shape
  20. {
  21. public:
  22. Rectangle(int a = 0, int b = 0) : Shape(a, b) {}
  23. int area()
  24. {
  25. cout << "Rectangle class area: " << width * height << endl;
  26. return (width * height);
  27. }
  28. };
  29. class Triangle : public Shape
  30. {
  31. public:
  32. Triangle(int a = 0, int b = 0) : Shape(a, b) {}
  33. int area()
  34. {
  35. cout << "Triangle class area: " << width * height / 2 << endl;
  36. return (width * height / 2);
  37. }
  38. };
  39. int main()
  40. {
  41. Shape *shape;
  42. Rectangle rec(10, 7);
  43. Triangle tri(10, 5);
  44. shape = &rec; // 存储矩形的地址
  45. shape->area(); // 调用矩形的求面积函数 area
  46. shape = &tri; // 存储三角形的地址
  47. shape->area(); // 调用三角形的求面积函数 area
  48. return 0;
  49. }
  50. /*
  51. Rectangle class area: 70
  52. Triangle class area: 25
  53. */

此时,编译器看的是指针的内容,而不是它的类型。因此,由于 rec 和 tri 类的对象的地址存储在 shape 中,所以会调用各自的 area() 函数。
正如您所看到的,每个子类都有一个函数 area() 的独立实现。这就是*多态
的一般使用方式。有了多态,您可以有多个不同的类,都带有同一个名称但具有不同实现的函数,函数的参数甚至可以是相同的。

虚函数

虚函数是在基类中使用关键字 virtual 声明的函数。在派生类中重新定义基类中定义的虚函数时,会告诉编译器不要静态链接到该函数。
我们想要的是在程序中任意点可以根据所调用的对象类型来选择调用的函数,这种操作被称为 动态链接,或后期绑定

纯虚函数

您可能想要在基类中定义虚函数,以便在派生类中重新定义该函数更好地适用于对象,但是您在基类中又不能对虚函数给出有意义的实现,这个时候就会用到纯虚函数。
我们可以把基类中的虚函数 area() 改写如下:

  1. class Shape {
  2. protected:
  3. int width, height;
  4. public:
  5. Shape( int a=0, int b=0)
  6. {
  7. width = a;
  8. height = b;
  9. }
  10. virtual int area() = 0; // pure virtual function
  11. };

= 0 告诉编译器,函数没有主体,上面的虚函数是纯虚函数