Hiding and overwriting of C + + functions

Source: Internet
Author: User

Transferred from: http://bbs.csdn.net/topics/390082114

See the hidden and covered functions in the high-quality C + + code:

overriding refers to a subclass function overriding a base class function
Within different classes (in subclasses and parent classes, respectively).
Same name as the same parameter.
The base class must have the virtual keyword before the function name.
Hiding a function that refers to a derived class hides a function with the same name as the base class
If a derived class function has the same name as a base class function, but the arguments are different, the base class function is hidden regardless of whether there is a virtual modification before the base class function.
If the derived class function has the same name as the base class function, the parameters are the same, but the base class function is not virtual-modified before the base class function is hidden.

1 classBase2 {3  Public:4     Virtual    voidFfloatx) {cout <<"base::f (float)"<< x <<Endl;}5     voidGfloatx) {cout <<"base::g (float)"<< x <<Endl;}6     voidHfloatx) {cout <<"base::h (float)"<< x <<Endl;}7 };8 9 classDerived: PublicBaseTen { One  Public: A     Virtual    voidFfloatx) {cout <<"derived::f (float)"<< x <<Endl;} -     voidGintx) {cout <<"derived::g (int)"<< x <<Endl;} -     voidHfloatx) {cout <<"derived::h (float)"<< x <<Endl;} the};

The Derived::f (float x) function overrides the base class Base::f (float x) function. The child parent has the same name as the same parameter, and the parent class has the virtual keyword
The derived::g (int x) function hides the base class Base::g (float x) function. Different parameters with the same name, regardless of whether or not the virtual modification
The Derived::f (float x) function hides the base class Base::f (float x) function. Same name with same parameter, and base class has no virtulal decoration

Hiding and overwriting of C + + 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.