C++ 中的多态性
"多态性"一词表示具有多种形式。通常,当类具有层次结构且它们通过继承关联时,就会出现多态性。
C++ 多态性意味着对成员函数的调用将导致执行不同的函数,具体取决于调用该函数的对象类型。
考虑以下示例,其中一个基类由另外两个类派生 -
#include <iostream> using namespace std; class Shape { protected: int width, height; public: Shape( int a = 0, int b = 0){ width = a; height = b; } int area() { cout << "Parent class area :" << width * height << endl; return width * height; } }; class Rectangle: public Shape { public: Rectangle( int a = 0, int b = 0):Shape(a, b) { } int area () { cout << "Rectangle class area :" << width * height << endl; return (width * height); } }; class Triangle: public Shape { public: Triangle( int a = 0, int b = 0):Shape(a, b) { } int area () { cout << "Triangle class area :" << (width * height)/2 << endl; return (width * height / 2); } }; // 程序的主函数 int main() { Shape *shape; Rectangle rec(10,7); Triangle tri(10,5); // 存储 Rectangle 的地址 shape = &rec; // 调用矩形 area。 shape->area(); // 存储 Triangle 的地址 shape = &tri; // 调用三角形 area。 shape->area(); return 0; }
当编译并执行上述代码时,它会产生以下结果 -
Parent class area :70 Parent class area :50
输出错误的原因是,函数 area() 的调用被编译器设置为基类中定义的版本。这被称为函数调用的静态解析,或静态链接——函数调用在程序执行之前就已确定。有时,这也被称为早期绑定,因为 area() 函数是在程序编译期间设置的。
现在,让我们对程序稍作修改,在 Shape 类中 area() 的声明前加上关键字 virtual,使其如下所示 -
#include <iostream> using namespace std; class Shape { protected: int width, height; public: Shape( int a = 0, int b = 0){ width = a; height = b; } virtual int area() { cout << "Parent class area :" << width * height << endl; return width * height; } }; class Rectangle: public Shape { public: Rectangle( int a = 0, int b = 0):Shape(a, b) { } int area () { cout << "Rectangle class area :" << width * height << endl; return (width * height); } }; class Triangle: public Shape { public: Triangle( int a = 0, int b = 0):Shape(a, b) { } int area () { cout << "Triangle class area :" << (width * height)/2 << endl; return (width * height / 2); } }; // 程序的主函数 int main() { Shape *shape; Rectangle rec(10,7); Triangle tri(10,5); // 存储 Rectangle 的地址 shape = &rec; // 调用矩形 area。 shape->area(); // 存储 Triangle 的地址 shape = &tri; // 调用三角形 area。 shape->area(); return 0; }
经过这一小段修改后,当编译并执行前面的示例代码时,它会产生以下结果 -
Rectangle class area :70 Triangle class area :25
这次,编译器会查看指针的内容,而不是它的类型。因此,由于 tri 和 rec 类对象的地址存储在 *shape 中,因此会调用相应的 area() 函数。
如您所见,每个子类都有各自的 area() 函数实现。这就是多态性的一般用法。不同的类具有相同名称的函数,甚至相同的参数,但实现不同。
虚函数
虚函数是基类中使用关键字 virtual 声明的函数。在基类中定义一个虚函数,而在派生类中定义另一个版本,会向编译器发出信号,表明我们不希望该函数与静态链接。
我们真正想要的是在程序中任何给定位置,根据调用该函数的对象类型来选择要调用的函数。这种操作称为动态链接,或后期绑定。
纯虚函数
您可能希望在基类中包含一个虚函数,以便可以在派生类中重新定义该函数以适应该类的对象,但您无法在基类中为该函数提供任何有意义的定义。
我们可以将基类中的虚函数 area() 更改为以下内容 -
class Shape { protected: int width, height; public: Shape(int a = 0, int b = 0) { width = a; height = b; } // 纯虚函数 virtual int area() = 0; };
= 0 告诉编译器该函数没有函数体,上述虚函数将被称为纯虚函数。