Differential instance analysis of overloading, overriding (overwriting) and hiding in C + +

Source: Internet
Author: User

This article mainly introduces the difference between overloading, overriding (overwriting) and hiding in C + +, which is a very important concept of C + + object-oriented programming, which can be referenced by friends.

The examples in this article describe the differences between overloading, overriding (overwriting) and hiding in C + +, which are very important concepts for C + + object-oriented programming. The specific analysis is as follows:

1. Overloading: heavy-duty translation from overload, refers to the same accessible area is declared in a number of different parameter columns (the type of the parameter, the number, the order of different) of the same name function, according to the parameter list to determine which function to call, overload does not care about the function return type.

The sample code is as follows:

Class A{public:  void Test (int i);  void Test (double i);  void Test (int i, double j);  void Test (double i, Int j);  int test (int i);         Error, non-overloaded};

The first four are overloaded functions, the last one and the first are not overloaded functions, the most important two points of overloaded functions, the function names are the same, the parameters are different.

2. Hide: hide refers to a function of a derived class that masks a base class function with the same name. Note that the base class function is hidden whenever a function of the same name, regardless of whether the argument list is the same.

The instance code is as follows:

#include <iostream>using namespace Std;class a{public:  void fun1 (int i, int j) {    cout << "a::fun1 ():" << i << "<< J << Endl;  }}; Class B:public A{public:    //Hide  void fun1 (double i) {    cout << b::fun1 (): "<< i << endl;
   }};int Main () {  b b;    B.FUN1 (5);          Call the function b.fun1 in Class B  (1, 2);        Error because the base class function is hidden by  the system ("pause");  return 0;}    

3. Rewrite: rewrite translation from override, also translated to overwrite (better), refers to the existence of a redefined function in the 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.

The instance code is as follows:

#include <iostream>using namespace Std;class a{public:  virtual void fun3 (int i) {    cout << "A::fun3 () : "<< i << Endl;  }}; Class B:public A{public:    //rewrite  virtual void fun3 (double i) {    cout << b::fun3 (): "<< i << Endl;  }}; int main () {    a A;  b b;  A * pa = &a;  PA->FUN3 (3);  PA = &b;  PA->FUN3 (5);  System ("pause");  return 0;}

The above for the virtual function of the implementation of polymorphic code, do not understand the first virtual function to realize the principle of polymorphism.

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.

Debug run the following code:

#include <iostream>using namespace Std;class a{public:  void fun1 (int i, int j) {    cout << "a::fun1 ():" << i << "" << J << Endl;  }  void fun2 (int i) {    cout << "a::fun2 ():" << i << Endl;  }  virtual void fun3 (int i) {    cout << "a::fun3 (int):" << i << Endl;  }}; Class B:public A{public:    //Hide  void fun1 (double i) {    cout << b::fun1 (): "<< i << endl;< c12/>}    //overriding  void fun3 (int i) {    cout << "b::fun3 (int):" << i << Endl;  }    Hide  void Fun3 (double i) {    cout << "B::fun3 (double):" << i << Endl;  }}; int main () {  b b;  A * pa = &b;  B * PB = &b;  PA->FUN3 (3);        Rewrite, polymorphism, call B's function  B.fun3 (ten);         Hide, call B's function  pb->fun3 (a);       Hide, call the function system of B  ("pause");  return 0;}

The output is:

B::FUN3 (int): 3b::fun3 (int): 10b::fun3 (int): 20 Press any key to continue ...

It is hoped that this article will be helpful to everyone in C + + object-oriented programming.

Differential instance analysis of 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.