Overloading, overwriting, and hiding of class member functions in C + + (transferred from the Cattle network)

Source: Internet
Author: User

Problem

The overloaded, overridden, and hidden differences between class member functions are described correctly.
[] A. Overrides refer to the same name in the same class, with different parameters
[] B. Overloading refers to a derived class function overriding a base class function, with the same function, with the same parameters, the base class function must have the virtual keyword
[] C. A derived class function is the same as a base class function, but a different parameter will "hide" the parent class function
[] D. Functions with the same name and same parameters as the base class without the virtual keyword "Hide" the parent class function

Answer

C, D

Analytical

A option: The same class, with the same name and different parameters, is overloaded instead of overwritten.
B option: Describes overrides, not overloads.
The CD option is correct. Note: Hiding and overwriting are both for the relationship between the base class and the derived class member functions, and the overloads are the relationships of the member functions in the same class.

Overloading, overwriting, and hiding

1. Features that are overloaded with member functions:

(1) The same range (in the same class);

(2) The function has the same name;

(3) different parameters;

(4) The virtual keyword is optional.

2. Overrides refer to a derived class function overriding a base class function, characterized by:

(1) different ranges (in the derived and base classes, respectively);

(2) The function has the same name;

(3) the same parameters;

(4) The base class function must have the virtual keyword.

3. "Hidden" refers to a function of a derived class that masks a base class function with the same name as the following rule:

(1) If the function of the derived class has the same name as the function of the base class, but the parameters are different. At this point, the function of the base class is hidden, regardless of the virtual keyword (Note that it is not confused with overloading).

(2) If the function of the derived class has the same name as the function of the base class, and the parameters are the same, the base class function does not have the virtual keyword. At this point, the function of the base class is hidden (be careful not to confuse the overlay)

Example
classPeople { Public://Hide: Refers to the function of a derived class mask base class function    //Hide Rule 1:    //1) function name same && parameter different    //2) virtual does not affect    voidGetid_different_params () {cout <<"People::getid_different_params"<< Endl;}Virtual voidGetname_different_params () {cout <<"People::getname_different_params"<< Endl;}//Hide Rule 2:    //1) same function name && same parameter    //2) No virtual    voidGetphone_same_params () {cout <<"People::getphone_same_params"<< Endl;}//Overwrite rule:    //1) same function name && same parameter    //2) with virtual    Virtual voidGetaddress_same_params () {cout <<"People::getaddress_same_params"<< Endl;}};classChildren: PublicPeople { Public://Hide: Refers to the function of a derived class mask base class function    //Hide Rule 1:    //1) function name same && parameter different    //2) virtual does not affect    voidGetid_different_params (int) {cout <<"Children::getid_different_params (int)"<< Endl;}Virtual voidGetname_different_params (int) {cout <<"Children::getname_different_params (int)"<< Endl;}//Hide Rule 2:    //1) same function name && same parameter    //2) No virtual    voidGetphone_same_params () {cout <<"Children::getphone_same_params"<< Endl;}//Overwrite rule:    //1) same function name && same parameter    //2) with virtual    Virtual voidGetaddress_same_params () {cout <<"Children::getaddress_same_params"<< Endl;}};voidTest1 () {/*children::getid_different_params (int)children::getname_different_params (int)Children::getphone_same_paramsChildren::getaddress_same_params    */Children *c =NewChildren (); C->getid_different_params (1); C->getname_different_params (1);    C->getphone_same_params (); C->getaddress_same_params ();/*People::getid_different_paramsPeople::getname_different_paramsPeople::getphone_same_paramsChildren::getaddress_same_params//Not override    */People *p =NewChildren ();    P->getid_different_params ();    P->getname_different_params ();    P->getphone_same_params (); P->getaddress_same_params ();}

Overloading, overwriting, and hiding of class member functions in C + + (transferred from the Cattle network)

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.