Writing high-quality code-differentiate the differences between Overloading, Overriding, and Hiding

Source: Internet
Author: User

■ Overload: The same function name is used in the same scope, but the number of function parameters or functions of different types are used.

For example:

# Include <iostream>
Using namespace std;
Class printer {
Public:
Void print (int data ){};
Void print (float data ){};
Void print (const char & pStr, size_t size ){};
// Other code
};
Class stringPrinter: public printer {
Public:
/* Using printer: print; * // introduce the print function declaration in the base class to the scope of the derived class.

// Function definition without print (int) in the scope of the derived class (hidden by print (const string &) in the derived class)
Void print (const string & str ){};
// Other code
};
Int main (){
StringPrinter myPrinter;
MyPrinter. print (2011 );
Return 0;
}

■ Rewrite: Re-implement the virtual functions in the base class in the derived class, that is, customize the base class virtual functions in the derived class.

Note:

1) function rewriting has nothing to do with the access level (public, private, and protected) (but generally, the rewrite function of a derived class should have the same access level as the corresponding function of the base class)

For example:

Class CWorker {

Public:

Virtual void Working (){...}

};

------------------------------------

Class CDriver: public CWorker {

Private:

Virtual void Working (){......}

};

2) const may be the rewriting of the virtual member function (the constant attribute is part of the function signature)

For example:

Class CDriver: public CWorker {

Private:

Virtual void Working () const {...} // The Working () function of the base class is not overwritten.

};

3) The rewrite function must have the same return type as the original function (the return type of the function is not part of the function signature, and the return type must be the same)

For example:

Class CDriver: public CWorker {

Private:

Virtual bool Working () const {...} // error: the return type of the rewrite virtual function is different.

};

■ Hide: a function in a derived class shields non-virtual functions with the same name in the base class. When calling a member function of the class, the compiler looks up the function definition step by step along the inheritance chain of the class, if it is found, it will stop, that is, the compiler's function search will not be able to reach the base class, implementing the hidden)

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.