Inheritance and masking

Source: Internet
Author: User

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 ).

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.