The difference between overload, override, overwrite in C + +

Source: Internet
Author: User

Overload (Heavy): in a C + + program, several functions that are semantically and functionally similar can be represented by the same name, but the parameters or return values are different (including the type, the order differs), which is the function overload.
(1) The same range (in the same class);
(2) The function has the same name;
(3) different parameters;
(4) The virtual keyword is optional.

Override (Overwrite): Refers to a derived class function that overrides 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 .

Overwrite (override): 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).

Override is a function in which multiple function prototypes are identical and implemented in different scopes. In C + +, it often occurs in the inheritance of classes. When a method in a base class is a virtual or pure virtual function (the access permission must be public or protected, of course, because the private function is not virtual from the C + + design perspective), The re-implementation of the method in its subclasses belongs to override. When used, it is possible to determine which method is called by a pointer to a base class or a reference to the specific object to which the function is polymorphic.

Overload: For a member function of a non-virtual type in a base class, if its subclass also declares a function with the same name as the function, then the function in the base class (perhaps a series of functions, if the function has overloads in the base class) is hidden and can be called through the domain resolution operator. However, according to the C + + design idea, the non-virtual type member function in the base class does not need to be modified in the subclass, so if this kind of hiding occurs in the subclass, it is necessary to change the function in the base class to virtual type, then override the

While overload refers to the same scope, multiple functions have the same name, but the number and type of parameters are different (of course, the same number and type, if the order is also possible), because the mechanism of function overloading is in C + + in the signature of the function is related to its parameters, not like in C, only with the function name.

In summary, one of the biggest differences between override and overload is the difference in scope and whether the function prototype is the same.

Overloaded (overload) override of function (override) hidden

A function of a derived class is exactly the same as a function of a base class (both the function name and the argument list are the same), except that the functions of the base class do not have the virtual keyword. Some time the base class functions will be hidden, not overwritten.

A function of a derived class has the same name as a function of the base class, but the argument list is different, in which case the function of the base class is hidden, regardless of whether the function declaration of the base class has the virtual keyword. Note This is the difference between overloading and overloading, which occurs in the same class.

#include <stdio.h>#include<iostream.h>classParent { Public:      voidF ()//when you have the same function in a subclass, it is called an override{printf ("parent.f () \ n"); }      Virtual voidG ()//Overrid (overwrite) when the same function exists in a subclass{printf ("parent.g () \ n"); }      intADD (intXinty) {returnX +y; }         floatADD (floatXfloatY//have the same function in the same class, overload (overload) the Add function    {          returnX +y;    }  }; classchildone:parent {//overriding (overwrite) parent class functions    voidF () {printf ("childone.f () \ n"); }      //Overwrite (override) The parent virtual function, mainly to implement Polymorphic    voidG () {printf ("childone.g () \ n");  }  }; voidMain () {Childone childone;//= new Childone (); parent* p = (Parent *) &Childone; //call PARENT.F ()P->F (); //Implementing PolymorphicP->G (); Parent* P2 =NewParent (); //overloading (overload)printf"%d\n", P2->add (1,2)); printf ("%f\n", P2->add (3.4f,4.5f)); DeleteP2; }

The difference between overload, override, overwrite 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.