- Polymorphism can be simply summed up as "one interface, many methods", the program at runtime to determine the function of the call, it is the core concept of object-oriented programming domain. Polymorphic (polymorphism), literally meaning multiple shapes.
- A virtual function is a member function that is declared as virtual in a base class and redefined in a derived class to implement a dynamic overwrite (override) of a member function.
- Classes that contain pure virtual functions are called abstract classes. Because an abstract class contains a pure virtual function that is not defined, an object of an abstract class cannot be defined.
- The function of virtual function in C + + is mainly to realize the mechanism of polymorphism. In terms of polymorphism, in short, a pointer to the parent type points to an instance of its child class, and then invokes the member function of the actual subclass through a pointer to the parent class. This technique allows the parent class to have a "multiple form" pointer, which is a generic technique. The so-called generic technology, plainly, is trying to use immutable code to implement a mutable algorithm. For example: template technology, RTTI technology, virtual function technology, either try to do at compile time resolution, or try to achieve runtime resolution.
- Any attempt to use a parent pointer to call a member function in a subclass that does not overwrite a parent class is considered illegal by the compiler, so that such a program cannot be compiled at all.
- Polymorphism refers to the same entity having multiple forms at the same time. It is an important feature of object-oriented programming (OOP). If a language supports only classes and does not support polymorphism, it can only indicate that it is object-based, not object-oriented. The polymorphism of C + + is embodied in two aspects of running and compiling. Run-time polymorphism is dynamic polymorphism, and its specific referenced objects are determined at run time. Compile-time polymorphism is static polymorphism, which can be used to determine the form of an object at compile time. Polymorphism is implemented through virtual functions in C + +.
Virtual functions and polymorphism of C + +