1. Coverage of functions
Conditions of Coverage:
- The base class function must be a virtual function (declared with the virtual keyword);
- The two functions that occur in the overlay must be in the derived class and the base class, respectively;
- The function name must be exactly the same as the argument list;
2, the function of the hidden
Hidden refers to a function in a derived class that has the same name as the base class (regardless of whether the argument list is the same), thereby hiding a function of the same name from the base class in the derived class.
Two cases are hidden:
The functions of the ① derived class are exactly the same as the functions of the base class (both the function name and the argument list are the same), except that the functions of the base class do not use the virtual keyword.
The function of the ② derived class has the same name as the function of the base class, but the argument list is different, in which case the base class's functions are hidden, regardless of whether the function declaration of the base class has the virtual keyword. (Note the difference between this and the function overload, where overloading occurs in the same class)
3, EX(VC + + in- depth detailed p54)
#include <iostream.h>
Class Base
{
Public
virtual void xfn (int i)
{
cout<< "BASE::XFN (int i)" <<endl;
}
void Yfn (float f)
{
cout<< "BASE::YFN (float f)" <<endl;
}
void Zfn ()
{
cout<< "BASE::ZFN ()" <<endl;
}
};
Class Derived:public Base
{
Public
void Xfn (int i)// XFN function overriding base class
{
cout<< "DERIVED::XFN (int i)" <<endl;
}
void yfn (int c)//Hide yfn function of base class
{
cout<< "DERIVED::YFN (int c)" <<endl;
}
void Zfn ()//Hide zfn function of base class
{
cout<< "DERIVED::ZFN ()" <<endl;
}
};
void Main ()
{
Derived D;
Base *pb=&d;
Derived *pd=&d;
PB->XFN (5);
PD->XFN (5);
PB->YFN (3.14f);
PD->YFN (3.14f);
PB->ZFN ();
PD->ZFN ();
}
Operation Result:
Note: The override of a function occurs between a derived class and a base class, and two functions must be exactly the same, and both are virtual functions. This is not the case, it is hidden.
C + + polymorphism--coverage and concealment of functions