目录

C++ Polymorphism

polymorphism这个词意味着有许多形式。 通常,当存在类的层次结构并且它们通过继承相关时,会发生多态性。

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()的调用由编译器设置为基类中定义的版本。 这称为函数调用的static resolution ,或static linkage - 在执行程序之前修复函数调用。 这有时也称为early binding因为在编译程序期间设置了area()函数。

但是现在,让我们在程序中进行一些修改,然后在Shape类中使用关键字virtual声明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()函数。

如您所见,每个子类都有一个单独的函数区域实现()。 这就是通常使用polymorphism方式。 您有不同的类具有相同名称的功能,甚至相同的参数,但具有不同的实现。

虚函数

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

我们想要的是在程序中的任何给定点选择要调用的函数,使其基于调用它的对象的类型。 这种操作称为dynamic linkagelate binding

纯虚函数 (Pure Virtual Functions)

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

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

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告诉编译器该函数没有主体,上面的虚函数将被称为pure virtual function

<上一篇.C++ 重载
C++ Abstraction.下一篇>
↑回到顶部↑
WIKI教程 @2018