Hidden, overloaded (overload), overwrite (override, override) in C + +

Source: Internet
Author: User

These concepts are sometimes remembered, but it may not be long before you forget them, or write them down. Online looking for an article good: here

1 overloading and overwriting

Features that are overloaded by member functions:

(1) The same scope (in the same class, excluding inheritance);

(2) The function has the same name;

(3) different parameters ;

(4) The virtual keyword is optional.

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.

Confusing hidden rules
It is not difficult to distinguish between overloading and overwriting, but the hidden rules of C + + increase the complexity of the problem abruptly. "Hide" here 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).

Cases:

#include <iostream.h>using namespacestd;classbase{ Public:    Virtual voidFfloatx) {cout <<"base::f (float)"<< x <<Endl;} voidGfloatx) {cout <<"base::g (float)"<< x <<Endl;} voidHfloatx) {cout <<"base::h (float)"<< x <<Endl;}}; classDerived: Publicbase{ Public:    Virtual voidFfloatx) {cout <<"derived::f (float)"<< x <<Endl;} voidGintx) {cout <<"derived::g (int)"<< x <<Endl;} voidHfloatx) {cout <<"derived::h (float)"<< x <<Endl;}};voidMainvoid) {Derived D; Base*PB = &D; Derived*PD = &D;//polymorphic---Depends on the type of object pointed toPb->f (3.14f);//derived::f (float) 3.14Pd->f (3.14f);//derived::f (float) 3.14//depends on pointer typePb->g (3.14f);//base::g (float) 3.14Pd->g (3.14f);//derived::g (int) 3 (not inherited g (float); hidden)//depends on pointer typePb->h (3.14f);//base::h (float) 3.14 (hidden)Pd->h (3.14f);//derived::h (float) 3.14}

In the above program:
(1) The function derived::f (float) is covered with base::f (float).
(2) the function derived::g (int) hides base::g (float) instead of overloading.
(3) The function derived::h (float) hides the base::h (float) instead of the overlay.

Hidden, overloaded (overload), overwrite (override, override) in C + +

Related Article

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.