"Overload" is translated as overload, overload, and heavy load, exceeding the standard load. "Override" is translated as "reset" and "overload" to make the original effect ineffective.
Let's talk about it first.Overload)In daily life, we often need to clean things, such as washing cars and washing clothes. Although we didn't explicitly say that we would wash our car by using a car wash method or by using a laundry method, but no one will wash a car by using the laundry method. Otherwise, the car will be out of stock when it is finished. We don't need to be so clear about it. This means heavy load. Functions with the same name with different parameter columns (different parameter types, numbers, and sequences) in the same accessible area, the program determines which function to call based on different parameter columns. This mechanism is called overload,
The reload does not care about the return value type of the function. Here, the meaning of "heavy" is different from that of "heavy". It means "repetition" and "Overlap. For example, in the same accessible area:
① Double calculate (double );
② Double calculate (double, double );
③ Double calculate (double, INT );
④ Double calculate (INT, double );
⑤ Double calculate (INT );
⑥ Float calculate (float );
7. Float calculate (double );
Six functions with the same name calculate, ① ② ③ ④ ⑤ any two of them constitute heavy load, and ⑥ and VII can also constitute heavy load, but ① and 7's cannot constitute heavy load, because ① and 7's parameters are the same.
Override)It refers to a function that has been redefined in a derived class. Its function name, parameter column, and return value type must be the same as the function that should be overwritten in the parent class.Strict consistencyOnly the function body is used to override and override functions.
(The part in curly braces) are different. When a derived class object calls a function of the same name in the subclass, it automatically calls the override version in the subclass instead of the replaced function version in the parent class, this mechanism is called overwrite.
The following describes the differences between overload and overwrite from the perspective of member functions.
Features of member functions being overloaded include:
1) Same range (In the same class);
2) The function name is the same;
3) parameters are different;
4) virtual keywords are optional.
The covered features include:
1) different ranges (It is located in the derived class and the base class respectively.);
2) The function name is the same;
3) The parameters are the same;
4) basic functions must have virtual keywords.
For example, in the following program:
# Include <iostream. h>
Class base
{
Public:
Void F (int x) {cout <"base: F (INT)" <x <Endl ;}
Void F (float X) {cout <"base: F (float)" <x <Endl ;}
Virtual void g (void) {cout <"base: G (void)" <Endl ;}
};
Class derived: public Base
{
Public:
Virtual void g (void) {cout <"derived: G (void)" <Endl ;}
};
Void main (void)
{
Derived D;
Base * pb = & D;
Pb-> F (42); // running result: Base: F (INT) 42
Pb-> F (3.14f); // running result: Base: F (float) 3.14
Pb-> G (); // running result: derived: G (void)
}
The base: F (INT) and base: F (float) functions are overloaded with each other, while base: G (void) is overwritten by derived: G (void.
Hiding is a function in a derived class that shields base class functions with the same name. The rules are as follows:
1) if the function of the derived class has the same name as the function of the base class, but the parameter is different. At this time,Whether or not there are virtual keywords, The base class function will be hidden (Be sure not to confuse with overload).
2) If the function of the derived class has the same name and parameter as the function of the base class, but the base class function does not have the virtual keyword. In this case, the base class function is hidden.(Do not confuse with overwrite).
For example, in the following program:
# Include <iostream. h>
Class base
{
Public:
VirtualVoid
F (float X) {cout <"base: F (float)" <x <Endl ;}
Void g (float X) {cout <"base: G (float)" <x <Endl ;}
Void H (float X) {cout <"base: H (float)" <x <Endl ;}
};
Class derived: public Base
{
Public:
VirtualVoid
F (float X) {cout <"derived: F (float)" <x <Endl ;}// after being inherited, virtual can be dispensable, but it is best to have it. After inheritance, it is also a virtual function.
Void g (int x) {cout <"derived: G (INT)" <x <Endl ;}
Void H (float X) {cout <"derived: H (float)" <x <Endl ;}
Using Base: G; // This statement is used to reference the hidden part of the parent class.
};
Analysis shows that:
1) The derived: F (float) function overwrites base: F (float ).
2) The derived: G (INT) function hides base: G (float). Note that this function is not overloaded.
3) The derived: H (float) function hides base: H (float) instead of overwrite.
After reading the preceding example, you may not understand the differences between hiding and overwriting, because we have discussed the superficial phenomena, implementation methods, and situations. Next, we will analyze what is the difference between coverage and hiding in applications. In the following program, BP and DP point to the same address. The running result should be the same, but this is not the case.
Void main (void)
{
Derived D;
Base * pb = & D;
Derived * Pd = & D;
// Good: behavior depends solely on type of the object
Pb-> F (3.14f); // running result: derived: F (float) 3.14
Pd-> F (3.14f); // running result: derived: F (float) 3.14
// Bad: behavior depends on type of the pointer
Pb-> G (3.14f); // running result: Base: G (float) 3.14
Pd-> G (3.14f); // running result: derived: G (INT) 3
// Bad: behavior depends on type of the pointer
Pb-> H (3.14f); // running result: Base: H (float) 3.14
Pd-> H (3.14f); // running result: derived: H (float) 3.14
}
Please note that F () functions are covered, while G () and H () functions are hidden. From the preceding running results, we can note that when the base class pointer and the derived class pointer are used to call the F () function, the system will execute the derived class function f (), instead of F () of the base class, this is actually the "interface" function. In the hidden mode, when the base class pointer and the derived class pointer are used to call the function f (), the system will differentiate it. When the base class pointer is called, the system will execute the F () of the base class (), when the pointer of a derived class is called, the system "hides" The F () of the base class and executes the F () of the derived class. This is the "hidden" principle.
========================================================== ======================================
-------------------------------------------------------------------
-------------------------------------------------------------------
Overload)
This is easy to understand.
The parameters must be different and have nothing to do with virtual.
Override)
Same name, same parameter, with virtual
Overwrite and understand functions such as show ()
A sent B
If show () in B overwrites show () in ()
But there are still two show () in B, and both Class A pointer and Class B object call can only call the show () of Class B ();
The show () function inherited from Class A is actually overwritten? The promise is wrong. In this case, the show () inherited by Class A can be called on Class B objects ();
Program code:
# Include <iostream>
Using namespace STD;
Class
{
Public:
Virtual void show ()
{
Cout <A <Endl;
}
Int;
};
Class B: public
{
Public:
Void show ()
{
A: Show ();
// Explicitly call the "show () function inherited from Class A" in your class"
// When a function of a class is explicitly pointed out like this,
// The Compiler processing method is as follows: First, check whether a: Show () is found in your class. If yes, call.
// If it cannot be found, call the function in the class explicitly specified (that is, Class A). Of course, A: Show () can be found in Class B ()
// The base class indicates that this function is a virtual function.
}
Int B;
};
Int main ()
{
A;
A. A = 3;
A. Show ();
B;
B. B = 4;
B. Show ();
B. A: Show (); // display the show () function inherited by Class A in the current class"
Return 0;
}
Summary:
Virtual, overwrite.
Actually, it is accurate. Generally speaking, Class B still has two show () methods, but only show () inherited by Class A can be called explicitly. [Class Name :: virtual function name]
Whether it is the pointer of the base class A (B; A * P = & B; P-> show () or the object of the derived class (B; B. show () can only call the show () function of Class B.
Bytes -----------------------------------------------------------------------------------------------------------------------
Hide hide
This is a little troublesome...
What is hiding?
1. No virtual
2. Different parameters with the same name, regardless of whether there is virtual
Program code:
Class
{
Public:
Void show () {}; // Id 1
Void rose (int A) {}// number 2
};
Class B: public
{
Public:
Void show () {}; // 3
Void rose (int A, int B) {}; // number 4
};
//
Show () and rose () in Class B obviously hide Show () and rose () of Class ()
Hidden understanding:
In Class B, there are actually two show () and two rose ();
But why not reload it? You will think like this, but I can tell you that because two show () and two rose () in Class B can not be called by Class B objects.
// ---- 1 ----//
Numbers 1 and 2, even if they exist in Class B, can only be called through the pointer of Class A, rather than through class B objects, such:
Program code:
A * P = new B;
P-> show ();
P-> rose (3 );
P-> rose (3, 5); // Error
// ---- 2 ----//
Number 3 and programming 4 can only be called through class B objects, but not through class A pointers, such:
Program code:
B;
B. Show ();
B. Rose (3, 5 );
B. Rose (4); // Error