Original address: https://qunxinghu.github.io/2016/09/08/C++%20%E4%B8%89%E5%A4%A7%E7%89%B9%E6%80%A7%E4%B9%8B%E5%A4%9A%E6%80%81/
Concept polymorphism (POLYMORPHISN)
Polymorphism is a technique that allows you to set a parent object to be equal to one or more of his child objects, and after assignment, the parent can operate differently depending on the attributes of the child object currently assigned to it. Simply put: A pointer to the parent class type (one interface, multiple methods) is allowed to be assigned to the pointers of the child class type.
C + + supports two kinds of polymorphism: compile-time polymorphism, run-time polymorphism.
A, compile-time polymorphism (static polymorphism): implemented by overloaded functions
B, run-time polymorphism (dynamic polymorphism): virtual function implementation.
The role of polymorphism
So what is the role of polymorphism, encapsulation can make code modular, inheritance can extend existing code, their purpose is to code reuse. The purpose of polymorphism is to reuse the interface . In other words, regardless of what is passed to the object of that class, functions can be called through the same interface to adapt to the implementation of the respective object.
Usage of polymorphism
Run-time polymorphism:
The most common usage is to declare a pointer to a base class, use it to point to any subclass object, invoke the corresponding virtual function, and implement a different method depending on the sub-class to which it is directed. If you do not use a virtual function, you will always be restricted to the base class function itself, but not to the overridden function in the subclass, when invoking the corresponding function using the base class pointer. Because there is no polymorphism, the address of the function call will be certain, and the fixed address will always be called to the same function, which will not implement an interface, the purpose of a variety of methods.
Non-run-time polymorphism:
Implemented by function overloading
Nutshell:
- Dynamic polymorphism may occur with virtual
- (no virtual) call is called by the original type
Confusing hidden rules
shadowing refers to a function of a derived class that masks a base class function with the same name as the following rule:
- If a function of a derived class has the same name as a function of a base class, the parameters are different. At this point, the functions of the base class will be hidden, regardless of the virtual keyword (note that overloading is in the same class and hiding involves derived classes and base classes).
- If a function of a derived class has the same name as a function of the base class, and the parameters are the same, the base class function does not have the virtual keyword. At this point, the function of the base class is hidden (be careful not to confuse the overlay with the virtual keyword).
C + + virtual functions
virtual function: It is a member function that allows the redefinition of its subclasses, and subclasses redefine the virtual function of the parent class, and can implement the dynamic overwrite (override) of the member function.
pure virtual function: is a virtual function declared in a base class that is not defined in the base class, but requires that any derived class define its own implementation method. The method of implementing a pure virtual function in a base class is to add "= 0" after the function prototype
virtual void funtion()=0
abstract class: A class that contains a pure virtual function is called an abstract class. Abstract classes cannot be instantiated because they contain pure virtual functions that are not defined.
The function of pure virtual function:
- To facilitate the use of polymorphic features, we often need to define virtual functions in the base class.
- In many cases, the base class itself generates objects that are unreasonable. For example, animals as a base class can be derived from tigers, peacocks and other sub-categories, but the animals themselves generated objects are obviously unreasonable.
In order to solve the above problem, the concept of pure virtual function is introduced, the function is defined as pure virtual function (method: virtual ReturnType Function()= 0; ), then the compiler requirements must be rewritten in the derived class to achieve polymorphism. A class that contains a purely virtual function is called an abstract class, and it cannot produce an object. This is a good solution to both of these problems.
Example code:
#Include<iostream>UsingNamespaceStdBase class objectClass base{PublicWith the virtual keyword, run-time polymorphicVirtualvoidF(float x) {cout<<"Base::f (float)" << x <<Endl }No Viratul keyword, run-time polymorphism does not occurvoidG(float x) {cout<<"Base::g (float)" << x <<Endl }voidH(float x) {cout<<"Base::h (float)" << x <<Endl }};Class Derived:Public base{PublicVirtualvoidF(float x) {cout<<"Derived::f (float)" << x <<EndlPolymorphic, overwrite}The child class has the same name as the function of the parent class, without the virtual keyword, or hiddenvoidG(int x) {cout<<"Derived::g (int)" << x <<EndlHiddenvoidH(float x) {cout<<"Derived::h (float)" << x <<EndlHidden}};IntMain(void) {Derived D;Subclass Base *PB = &d;Base class pointer to subclass Derived *PD = &d;The sub-class pointer points to itselfGood:behavior depends solely on type of the object Pb->f (3.14f); //Derived::f (float) 3.14 call subclass, polymorphic pd->f (3.14f); //Derived::f (float) 3.14 call subclass //Bad:behavior depends on type of the pointer pb->g (3.14f); //Base::g (float) 3.14 No polymorphism, call its own pd->g (3.14f); //derived::g (int) 3 No polymorphism, call own //Bad:behavior depends on type of the pointer pb->h (3.14f); //Base::h (float) 3.14 No polymorphism, call its own pd->h (3.14f); //Derived::h (float) 3.14 No polymorphism, call own return 0;}
Polymorphism of C + + three major features