Latest web development tutorials

C ++ polymorphism

Polymorphism literally means that a variety of forms.When the association between the class through inheritance between class hierarchy, and will be used polymorphism.

When C ++ member function calls polymorphic mean, will to perform different functions depending on the type of object the function is called.

The following example, the base class Shape is derived for the two categories, as follows:

#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); 
      }
};
// 程序的主函数
int main( )
{
   Shape *shape;
   Rectangle rec(10,7);
   Triangle  tri(10,5);

   // 存储矩形的地址
   shape = &rec;
   // 调用矩形的求面积函数 area
   shape->area();

   // 存储三角形的地址
   shape = &tri;
   // 调用三角形的求面积函数 area
   shape->area();
   
   return 0;
}

When the above code is compiled and executed, it produces the following results:

Parent class area
Parent class area

The cause of the error is output, call the function area () is set as the base class compiler version, this is calledstatic polymorphism, or static link- function called before the implementation of the program is ready. Sometimes it is also calledearly binding because the area () function during program compilation has been set up.

But now, let the program slightly modified in the Shape class, the first area () statement place the keywordvirtual, as follows:

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;
      }
};

After modification, when compiled and executed in front of the example code, it will produce the following results:

Rectangle class area
Triangle class area

In this case, the compiler is a pointer to see the content, rather than its type. Accordingly, since the address storage tri and rec class objects in * shape, and it will call the respective area () function.

As you can see, each class has a sub-function area () to achieve independence. This ispolymorphic general use.With polymorphism, you can have several different classes, all with the same name but a parameter of the function, the function can even have different implementations of the same.

Virtual function

Virtual function is declared using the keyword virtualin the base class function. When redefine base class virtual function defined in a derived class, it tells the compiler not statically linked to the function.

What we want is any point in the program can be selected according to the type of object function calls invoked, this operation is calleddynamic linking, or late binding.

Pure virtual function

You might want to define a virtual function in the base class, so derived class redefine the function better suited for the object, but you can not give a meaningful implementation of virtual functions in the base class, this time there will use pure virtual function.

We can base class virtual function area () to read as follows:

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 tells the compiler that the function has no body, above the virtual function is apure virtual function.