First, heavy load (overload)
The function name is the same, but its parameter table column number or order, the type is different. But it cannot be judged by the return type.
(1) The same scope (in the same scope);
(2) The function has the same name;
(3) different parameters;
(4) The virtual keyword is optional.
(5) The return value can be different;
Second, rewrite (also known as overwrite override)
Refers to a derived class that re-defines a virtual function of a base class, characterized by:
(1) Not in the same scope (the derived class and the base class, respectively);
(2) The function has the same name;
(3) the same parameters;
(4) The base class function must have the virtual keyword and cannot have static.
(5) The return value is the same (or covariant), otherwise the error; <--the concept of covariant is the first time I know ...
(6) The access modifier for the overriding function can be different. Although virtual is private, it is also possible to rewrite the overridden public,protected in a derived class.
Three, redefine (also become hidden)
(1) Not in the same scope (the derived class and the base class, respectively);
(2) The function has the same name;
(3) The return value can be different;
(4) different parameters. At this point, the function of the base class is hidden, regardless of the virtual keyword (note not being confused with overloading and overwriting).
(5) The parameter is the same, but 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).
#include <iostream>#include<complex>using namespacestd;classbase{ Public: Virtual voidAintx) {cout <<"base::a (int)"<<Endl; } //overload the base::a (int) function Virtual voidADoublex) {cout <<"base::a (Double)"<<Endl; } Virtual voidbintx) {cout <<"base::b (int)"<<Endl; } voidCintx) {cout <<"base::c (int)"<<Endl; }};classDerived: Publicbase{ Public: //redefine the base::a () function voidA (complex<Double> x) {cout <<"derived::a (complex)"<<Endl; } //override the base::b (int) function voidbintx) {cout <<"derived::b (int)"<<Endl; } //redefine the BASE::C () function voidCintx) {cout <<"derived::c (int)"<<Endl; }};intMain () {Base B; Derived D; Base* PB =NewDerived; // ----------------------------------- //B.A (1.0);//base::a (Double)D.A (1.0);//derived::a (complex)Pb->a (1.0);//base::a (double), this is redefine the base::a () function//Pb->a (complex<double> (1.0, 2.0)); //clear the annotation and has a try// ----------------------------------- //B.B (Ten);//base::b (int)D.B (Ten);//derived::b (int)Pb->b (Ten);//derived::b (int), this is the virtual function// ----------------------------------- //Delete PB; return 0;}
From here you can see:
The second function in the 1.Base Class A is an overload of the first
Function B in the 2.Derived class is an override of function B in the base class, which uses the virtual function attribute.
Function A in the 3.Derived class is the hiding of function A in base tear, which is redefined.
The 4.PB pointer is a pointer to the base type, but it actually points to a derived space, where the processing of the PD call function (polymorphism) depends on whether the function is overridden (virtual function attribute), and if not, the base class is still called.
5. Polymorphism only works if the derived class type is indirectly pointed to by a base class pointer or base class reference.
6. Because function C of the base class is not defined as virtual virtual function, the function C of the derived class is a redefinition of base::c ().
Reprint Address: http://www.wutianqi.com/?p=3171
C + + overloading, overriding, redefining