Overload, override and hide

xiaoxiao2021-03-06  35

Overload and coverage

Member function is overloaded:

(1) The same range (in the same class);

(2) The function name is the same;

(3) Different parameters;

(4) Virtual keywords can be available.

The overlay is a basic class function that is specifically class function, feature:

(1) Different scope (located in derived class and base class);

(2) The function name is the same;

(3) The parameter is the same;

(4) The base class function must have a Virtual keyword.

"Hidden" is a function of a function that is born. The rules are as follows: (1) If the derived class function is the same name of the base class, the parameters are different. At this point, the function of the base class will be hidden, whether there is a Virtual keyword, and not to confuse the overload. (2) If the derived class function is the same name, and the parameters are the same, the base class function does not have a Virtual keyword. At this point, the function of the base class is hidden (notes confusing with overlay).

In the following example program: (1) Function Derived :: F (FLOAT) Overwrite Base :: F (Float). (2) Function Derived :: G (int) Hide Base :: g (float) instead of overload. (3) Function Derived :: h (float) Hide Base :: h (float) instead of overwriting.

#include

Class base {public: Virtual void f (floatx) {cout << "Base :: f (float) << x << endl;} void g (floatx) {cout <<" Base :: g (float) << x << endl; void h (floatx) {cout << "Base :: h (float) << x << endl;}};

Class Derived: PUBLICBASE {public: Virtual Void F (FLOATX) {cout << "Derived :: F (FLOAT) << x << endl;} void g (intx) {cout <<" Derived :: g (int << x << Endl;} void h (floatx) {cout << "Derived :: h (float) << x << endl;}}; void main (void) {Derived D; base * Pb = & D; Derived * Pd = & D; // Good: Behavior Depends Solely on Type of the Object Pb-> f (3.14F); //derived ::f(float )3.14 PD-> f (3.14f); / /Derived::f(float )3.14

// Bad: Behavior Depends on Type of the Point PB-> g (3.14F); //base::g(float) 3.14 PD-> g (3.14F); // Derived :: G (int) 3 ( Surprise!) // Bad: Behavior Depends on Type of the Pointer PB-> h (3.14f); //base :: h(float) 3.14(Surprise!) PD-> H (3.14f); // Derived: : h (float) 3.14

转载请注明原文地址:https://www.9cbs.com/read-58706.html

New Post(0)