What is polymorphism in C++?
This article introduces what is polymorphism in C++. The content is very detailed. Interested friends can use it for reference. I hope it can be helpful to you.
What is polymorphism?
As the name implies, it is the multiple forms of the same thing in different scenes.
The following will be described in detail.
Static polymorphism
The function overloading we talked about before is a simple static polymorphism.
Int Add (int left, int right) {return left + right;} double Add (double left, int right) {return left + right;} int main () {Add (10,20); / / Add (10.0,20.0); / / this is a problem code Add (10.00.20); / / normal code return 0;}
As you can see, static polymorphism is completed by the compiler during compilation, and the compiler will choose to call the appropriate function according to the argument type. If there is an appropriate function to call, it will issue a warning or an error. It is relatively simple and does not make much introduction.
Dynamic polymorphism
What is dynamic polymorphism? Dynamic polymorphism: this is obviously a set of antonyms with static polymorphism, which determines which class's virtual function to call according to the object pointed to by the reference (pointer) of the base class when the program is running.
I went to school in Lintong, Xi'an, so I lifted a chestnut with the bus here:
Class TakeBus {public: void TakeBusToSubway () {cout