c++--overloading, overwriting, and hiding

Source: Internet
Author: User

First, overload

Occurs in the same class, when method A is defined in the same class, and then the method b,b and a are defined with the same name, but the parameters are different, then the B overloads the method A.

class test{public:    void  A ();     void A (int);}

Second, cover

Occurs in the subclass and parent class, the method A is defined in the parent class, and it is the virtual type, and the method A is defined again in the subclass, with the same parameters as the parent class. This is called a subclass of method A that overrides method A in the parent class.

class Base {public:    virtualvoid fun (int);} class D:publicbase{public:   void Fun (int);}

Third, hidden

Occurs in the subclass and parent class, if method A is defined in the parent class, the method A with the same name is defined again in the self-class, but the parameters are different, and this is the method in the parent class that is masked out.

For example

class test{public:    void fun ();} class D: public test{public:    void fun (int );}
New d;t->fun (); // OKt->fun (k); // Error

That is, the method in the derived class must not be called by the base class, which is actually not related to hiding, just the effect of class inheritance and polymorphism.

d dd;d.fun (); // errorD.fun (N); // OK

The above meaning is called by the derived class in the parent class, has been masked method, will error

The way to solve the problem is to use the using

class D: public test{public:    ...     .. using Test:fun;}

c++--overloading, overwriting, and hiding

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.