What is the difference between overloading (overload), overriding (override, also called "Overwrite") and redefinition (redefine) in C + +?

Source: Internet
Author: User

1) overload (overload):

The function name is the same, but its parameter table column number or order, the type is different. But it cannot be judged by the return type.

A the same range (in the same class)

b functions have the same name and different parameters

C Virtual keyword is optional

d The return value can be different;

2) override (override override) refers to a derived class function that overrides a base class function, characterized by:

A different range, located in the base class and the derived class, respectively

The B function has the same name and the same parameters

C base class functions must have the virtual keyword and cannot have static

D return the same value (or covariance), otherwise error;

the access modifiers for the E override function can be different. Although virtual is private , overrides in derived classes are overwritten as public , and protected is also possible

3) redefine (hide redefine) refers to a function of a derived class that masks a base class function with the same name, characterized by:

A is not in the same scope (both the derived and base classes, respectively)

The B function has the same name

c The return value can be different

D rule:

If the function of the derived class and the function of the base class have the same name, but the parameters are different, at this point, the function of the base class is hidden, regardless of the virtual one;

If a function of a derived class has the same name as a function of the base class, and the parameters are the same, but the base class function has no vitual keyword, the function of the base class is hidden.

PS: Polymorphism can be divided into static polymorphism (method overloading, a class) and dynamic polymorphism (method overrides, behavior between classes that have inheritance relationships). In turn, polymorphism can be implemented by overloading and overwriting.


Example:

Class Base {private: <span style= "color: #ff0000;" >virtual</span> void Display () {cout<< "base display ()" <<endl;} void Say () {cout<< "base say ( ) "<<endl; }public:void exec () {display (); Say ();} void F1 (String a) {cout<< "Base F1 (String)" <<endl;} void F1 (int a) {cout<< "Base F1 (int)" <<endl;} <span style= "color: #ff0000;" >overload, two F1 functions are overloaded inside the Base class </span>};class derivea:public base{public:void display () {cout<< ' Derivea display () "<<endl; }//<span style= "color: #ff0000;" >override, the display in the base class is a virtual function and is therefore overridden </span> void F1 (int a,int b) {cout<< Derivea F1 (int,int) <<endl;} <span style= "color: #ff0000;" The &GT;REDEFINE,F1 function is not a virtual function in the base class, so the redefinition </span> Void Say () {cout<< "Derivea say ()" <<endl;}//<span style= "COLOR: #ff0000;" >redefine, ibid. </span>};class deriveb:public base{public:void F1 (int a) {cout<< "DERIVEB F1 (int)" << Endl }//<span style="Color: #ff6666;" >redefine, redefining </span>};int main () {Derivea A; Base *b=&a; B->exec (); Display (): Version of Derivea call (polymorphism)//say (): Version of Base called (allways) <span style= "COLOR: #ff0000 ;" The function display in >b is covered by Class A, but say is still its own </span>. A.exec (); Same result as last statement A.say (); DERIVEB C; C.F1 (1); Version of DERIVEB called}

Execution Result:

Derivea display ()
Base display ()
Derivea display ()
Base display ()
Derivea display ()
DERIVEB F1 (int)

Reference Links:

Http://blog.163.com/[email protected]/blog/static/111352259201102441934870/

Http://www.cnblogs.com/kevinGaoblog/archive/2012/08/25/2656989.html


What is the difference between overloading (overload), overriding (override, also called "Overwrite") and redefinition (redefine) in C + +?

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.