C++ polymorphism
PolymorphismThe literal meaning is multiple forms. Polymorphism is used when there is a hierarchy between classes and classes are related through inheritance.
C++ Polymorphism means that when a member function is called, different functions will be executed depending on the type of the object calling the function.
In the following example, the base class Shape is derived into two classes, as shown below:
#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 will produce the following results:
Parent class area Parent class area
The reason for the error output is that the calling function area() is set by the compiler to the version in the base class. This is the so-called static polymorphism, or static linking - Function calls are prepared before program execution. This is sometimes called early binding because the area() function is set during program compilation.
But now, let us modify the program slightly. In the Shape class, place the keyword virtual before the declaration of area(), 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 the previous example code is compiled and executed, it will produce the following results:
Rectangle class area Triangle class area
At this time, the compiler looks at the contents of the pointer, not its type. Therefore, since the addresses of objects of class tri and rec are stored in *shape, the respective area() functions are called.
As you can see, each subclass has an independent implementation of the function area(). This is how Polymorphism is generally used. With polymorphism, you can have several different classes, all with functions with the same name but different implementations, and the parameters of the functions can even be the same.
Virtual function
Virtual function is a function declared in the base class using the keyword virtual. When you redefine a virtual function defined in a base class in a derived class, you tell the compiler not to statically link to the function.
What we want is that at any point in the program, the function to be called can be selected based on the type of object being called. This operation is called dynamic linking, or late binding Certainly.
Pure virtual function
You may want to define a virtual function in the base class so that you can redefine the function in the derived class to better apply to the object, but you have another problem in the base class: If a meaningful implementation of virtual functions cannot be given, pure virtual functions will be used at this time.
We can rewrite the virtual function area() in the base class 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, and the above virtual function is pure virtual function.