The difference between overloading, overriding (overwriting), and hiding in C + +

Source: Internet
Author: User

Reprinted from: 48976097

Basic concepts:

Overloading: A function of the same name that is declared within the same accessible area with different parameter columns (type, number, and order of parameters), which determines which function to call, based on the argument list, and the overload does not care about the function return type.

1 classa{2  Public:3   voidTestinti);4   voidTestDoublei);//Overload5   voidTestintIDoublej);//Overload6   voidTestDoubleIintj);//Overload7   intTestinti);//error, non-overloaded. Note overloading does not care about function return types. 8};

Hidden: Refers to a function of a derived class that masks a base class function with the same name, noting that the base class function is hidden whenever a function of the same name, regardless of whether the argument list is the same.

1#include"stdafx.h"2#include"iostream"3 4 using namespacestd;5 6 classBase7 {8  Public:9     voidFunDouble,int) {cout <<"Base::fun (double, int)"<<Endl;}Ten }; One  A classDerive: PublicBase - { -  Public: the     voidFunint) {cout <<"derive::fun (int)"<<Endl;} - }; -  - intMain () + { - Derive PD; +Pd.fun (1);//derive::fun (int) APb.fun (0.01,1);//error C2660: "Derive::fun": function does not accept 2 parameters at  -Base *FD = &PD; -Fd->fun (1.0,1);//Base::fun (double, int); -Fd->fun (1);//Error -System"Pause"); -     return 0; in}

Override (Overwrite): refers to the existence of a redefined function in a derived class. Its function name, argument list, return value type, all must match the overridden function in the base class. Only the function body is different (within curly braces), the derived class calls the overridden function of the derived class and does not invoke the overridden function. Overridden functions in the base class must have a virtual decoration.

Note: FD is a pointer to the base class, which is a function that calls fun is the base class

1#include <iostream>2 3 using namespacestd;4 5 classBase6 {7  Public:8     Virtual voidFuninti) {cout <<"base::fun (int):"<< I <<Endl;}9 };Ten  One classDerived: PublicBase A { -  Public: -     Virtual voidFuninti) {cout <<"derived::fun (int):"<< I <<Endl;} the }; - intMain () - { - Base B; +Base * PB =NewDerived (); -Pb->fun (3);//derived::fun (int) +  ASystem"Pause"); at     return 0; -}

The difference between overloading and overriding:

(1) Scope differences: Overrides and overridden functions in different classes, overloads and overloaded functions in the same class.

(2) Parameter differences: Overrides must be the same as the overridden function parameter list, and the overloaded and overloaded function parameter lists must be different.

(3) The difference between virtual: The overridden base class must have the virtual modification, the overloaded function and the overloaded function can be modified by the virtual, or not.

The difference between hiding and overriding, overloading:

(1) differs from overloaded ranges: hidden functions and hidden functions are in different classes.

(2) The difference between the parameters: the hidden function and the hidden function argument list can be the same or different, but the function name must be the same; When the arguments are not the same, the base class function is hidden, not overridden, regardless of whether the function in the base class is virtual.

1#include"stdafx.h"2#include <iostream>3 4 using namespacestd;5 6 classBase7 {8  Public:9     Virtual voidFfloatx) {cout <<"base::f (float)"<< x <<Endl;}Ten     voidGfloatx) {cout <<"base::g (float)"<< x <<Endl;} One     voidHfloatx) {cout <<"base::h (float)"<< x <<Endl;} A }; -  - classDerived: PublicBase the { -  Public: -     Virtual voidFfloatx) {cout <<"derived::f (float)"<< x <<Endl;} -     voidGintx) {cout <<"derived::g (int)"<< x <<Endl;} +     voidHfloatx) {cout <<"derived::h (float)"<< x <<Endl;} - }; +  A intMainvoid) at { - Derived D; -Base *PB = &D; -Derived *FD = &D; -     //Good:behavior depends solely on type of the object -Pb->f (3.14f);//derived::f (float) 3.14 inFd->f (3.14f);//derived::f (float) 3.14 -  to     //Bad:behavior depends on type of the pointer +Pb->g (3.14f);//base::g (float) 3.14 -Fd->g (3.14f);//derived::g (int) 3 the  *     //Bad:behavior depends on type of the pointer $Pb->h (3.14f);//base::h (float) 3.14Panax NotoginsengFd->h (3.14f);//derived::h (float) 3.14 -  theSystem"Pause"); +     return 0; A}

(1) The function derived::f (float) is covered with base::f (float).

(2) the function derived::g (int) hides base::g (float) instead of overloading.

(3) The function derived::h (float) hides the base::h (float) instead of the overlay.

The difference between overloading, overriding (overwriting), and hiding 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.