8.2 reload, overwrite, and hide member functions
Features of member functions being overloaded:
(1) the same range (in the same class );
(2) The function name is the same;
(3) parameters are different;
(4) virtual keywords are optional.
Override refers to the function of a derived class that overwrites the base class function. The features are as follows:
(1) different scopes (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.
# 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); // base: F (INT) 42
Pb-> F (3.14f); // base: F (float) 3.14
Pb-> G (); // derived: G (void)
}
"Hide" means that the function of the derived class shields the 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, functions of the base class will be hidden regardless of whether there is a virtual keyword (Be sure not to confuse them with the overload (the overloaded range is in the same class )).
(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 function of the base class is hidden (do not confuse with overwrite (in the derived class and the base class, overwrite must have virtual )).
Class base
{
Public:
Virtual void F (float X) {cout <"base: F (float)" <x <Endl ;}// Overwrite
Void g (float X) {cout <"base: G (float)" <x <Endl ;}// hide
Void H (float X) {cout <"base: H (float)" <x <Endl ;}// hide
};
Class derived: public Base
{
Public:
Virtual void F (float X) {cout <"derived: F (float)" <x <Endl ;}
Void g (int x) {cout <"derived: G (INT)" <x <Endl ;}
Void H (float X) {cout <"derived: H (float)" <x <Endl ;}
};
Int _ tmain (INT argc, _ tchar * argv [])
{
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 pointer
Pb-> G (3.14f); // base: G (float) 3.14// Isn't it hidden? (the parent class only calls its own and does not know about its son)
Pd-> G (3.14f); // derived: G (INT) 3 (surprise! The son hides the parent class, so he calls his own class)
// Bad: behavior depends on type of the pointer
Pb-> H (3.14f); // base: H (float) 3.14 (surprise !) // Isn't it hidden? (the parent class only calls its own and does not know about its son)
Pd-> H (3.14f); // derived: H (float) 3.14 (the son hides the parent class, so he calls his own)
}
Differences between coverage and hiding:
In the case of coverage, when the base class calls the function to be overwritten, it directly calls the subclass and its own is covered.
When hidden, the base class still calls its own function when calling the hidden function, and directly calls its own function when calling the subclass.