C + + polymorphism--coverage and concealment of functions

Source: Internet
Author: User

 

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

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.