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 :" <<endl;
         return 0;
      }
};
class Rectangle: public Shape {
   public:
      Rectangle( int a = 0, int b = 0):Shape(a, b) { }
      
      int area () { 
         cout << "Rectangle class area :" <<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 :" <<endl;
         return (width * height / 2); 
      }
};

// Main function for the program
int main() {
   Shape *shape;
   Rectangle rec(10,7);
   Triangle  tri(10,5);

   // store the address of Rectangle
   shape = &rec;
   
   // call rectangle area.
   shape->area();

   // store the address of Triangle
   shape = &tri;
   
   // call triangle area.
   shape->area();
   
   return 0;
}


编译并执行上述代码时,会产生以下结果 :

Parent class area :
Parent class area :


输出错误的原因是调用函数area()的一部分由编译器设置为基类中定义的版本.这称为函数调用的静态解析,或静态链接  - 在执行程序之前修复函数调用.这有时也被称为早期绑定,因为在编译程序期间设置了area()函数.

但是现在,让我们稍微修改一下编程并在Shape类中使用关键字虚拟声明area(),使其看起来像这样 :

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 :" <<endl;
         return 0;
      }
};


在稍作修改之后,当编译并执行前面的示例代码时,它会产生以下结果 :

Rectangle class area
Triangle class area


这一次,编译器会查看内容指针而不是它的类型.因此,由于tri和rec类的对象地址以* shape存储,因此调用相应的area()函数.

如您所见,每个子类都有一个单独的实现对于功能区().这就是通常使用多态性的方式.你有不同的类,具有相同名称的功能,甚至相同的参数,但具有不同的实现.

虚函数

A virtual function是使用关键字 virtual 声明的基类中的函数.在基类中定义一个虚函数,在派生类中使用另一个版本,向编译器发出信号,告知我们不希望这个函数有静态链接.

我们想要的是选择要在程序中的任何给定点调用的函数,以基于调用它的对象的类型.这种操作称为动态链接,或后期绑定.

纯虚函数

您可能希望在基类中包含虚函数,以便可以在派生类中重新定义它以适合该类的对象,但是您可以为函数提供有意义的定义在基类中.

我们可以将基类中的虚函数区域()更改为以下 :

class Shape {
   protected:
      int width, height;

   public:
      Shape(int a = 0, int b = 0) {
         width = a;
         height = b;
      }
      
      // pure virtual function
      virtual int area() = 0;
};


= 0告诉编译器该函数没有正文,上面的虚函数将被称为纯虚函数.