Polymorphism literally means multiple forms. Polymorphism is used when there is a hierarchy between classes and the classes are associated through inheritance.
C++ polymorphism means that when calling a member function, different functions will be executed according to the type of the object in which the function is called.
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); } }; // The main function of the programint main( ) { Shape *shape; Rectangle rec(10,7); Triangle tri(10,5); //Storing the address of the rectangle shape = &rec; // Call the area function area of the rectangle shape->area(); //Storing the address of the triangle shape = &tri; // Call the area function area of the triangle shape->area(); return 0; }
When the above code is compiled and executed, it produces the following results:
Parent class area
Parent class area
The reason for the error output is that the call function area() is set to the version in the base class by the compiler, which is calledStatic polymorphism,orStatic links- Function calls are ready before the program is executed. Sometimes this is also called early binding, because the area() function is set during program compilation.
But now, let's make a little modification to the program, and put the keyword virtual before the declaration of area() in the Shape class, 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 compiling and executing the previous instance code, it produces the following results:
Rectangle class area
Triangle class area
At this pointer's content, not its type. Therefore, since the addresses of objects of the tri and rec classes are stored in *shape, their respective area() functions are called.
As you can see, each subclass has an independent implementation of the function area() . This is the general way of using polymorphism. With polymorphism, you can have multiple different classes, all with the same name but with different implementations, and the parameters of the function can even be the same.
Virtual functions
Virtual functions are functions declared using the keyword virtual in the base class. When redefining a virtual function defined in a base class in a derived class, the compiler is told not to statically link to the function.
What we want is that at any point in the program, we can select the called function based on the type of object called. This operation is called dynamic linking, or late binding.
Pure virtual functions
You may want to define a virtual function in the base class so that redefining the function in the derived class will be better suited for objects, but you cannot give meaningful implementations of virtual functions in the base class, so you will use pure virtual functions 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 virtual function above is a pure virtual function.
The above is the detailed analysis of C++ polymorphism and virtual functions. For more information about C++ polymorphism and virtual functions, please pay attention to my other related articles!