C++ overloading, rewriting, redefinition

Overload (overload): The function name is the same, the parameter list is different, and override only exists inside the class.

Rewrite (override), also called coverage. The subclass redefines the virtual function (virtual) with the same name and parameters in the parent class. between inheritance relationships. C++ uses virtual functions to implement polymorphism.

Rewrite features: 

          1 The overridden function cannot be static. Must be virtual

          2 Overridden functions must have the same type, name and parameter list

          3 The access modifiers of the rewritten functions can be different . Although the virtual method of the parent class is private, it is also possible to rewrite it as public or protected in the derived class.

          This is because member functions modified by virtual, no matter whether they are private/protect/public, will be uniformly placed in the virtual function table. When deriving the parent class, the subclass will inherit the virtual function standard with the same offset address (the same offset address refers to the offset between the bottom of each virtual function and the VPTR pointer),

           Therefore, subclasses are allowed to override these virtual functions

Redefining (redefining), also known as hiding. The subclass redefines a non-virtual function with the same name in the parent class (the parameter list can be different)

Guess you like

Origin blog.csdn.net/gp18391818575/article/details/110875871