Inheritance and masking
Inheritance:
The purpose of the pure virtual function is to allow the Derived class to inherit only the function interface.
The purpose of impure virtual function is to let Derived class inherit the interface and default implementation of this function.
The purpose of a non-virtual (normal) function is to make the Derived class inherit the function interface and a mandatory implementation. If a member function is a non-virtual function, it means that it does not intend to have different behaviors in the Derived class.
Cover:
The "name masking rules" based on scopes also apply to class inheritance. Both virtual and non-virtual functions are applicable.
Example:
The names in the Derived class mask the names in the Base class.
Class Base
{
Private:
Int x;
Public:
Virtual void mf1 () = 0;
Virtual void mf1 (int );
Virtual void mf2 ();
Void mf3 ();
Void mf3 (double );
};
Class Derived: public Base
{
Public:
Virtual void mf1 ();
Void mf3 ();
Void mf4 ();
};
Derived d;
Int x;
D. mf1 (); // No problem. Call Derived: mf1;
D. mf1 (x); // error, because Derived: mf1 hides Base: mf1
D. mf2 (); // No problem. Call Base: mf2
D. mf3 (); // No problem. Call Derived: mf3
D. mf3 (x) // error, because Derived: mf3 hides Base: mf3
You can use the using declarative or transfer function to make the hidden name goodbye to the day.
Example:
Use the using declarative Method to Solve the masking problem.
Class Derived: public Base
{
Public:
Using Base: mf1; // Let all the things named mf1 and mf3 in the Base class
Using Base: mf3; // visible in Derived scope.
Virtual void mf1 ();
Void mf3 ();
Void mf4 ();
};
Derived d;
Int x;
D. mf1 (); // No problem. Call Derived: mf1;
D. mf1 (x); // No problem now. Call Base: mf1
D. mf2 (); // No problem. Call Base: mf2
D. mf3 (); // No problem. Call Derived: mf3
D. mf3 (x) // No problem now. Call Base: mf3
The using declaration can also be used to reload functions in the Base class in the Derived class. (do not forget that the original meaning overload can only reload the functions in the current class ).