Example analysis of overloading, overriding (overwriting) and hiding in C + + __c++

Source: Internet
Author: User

This example describes the difference between overloading, overriding (overwriting) and hiding in C + +, which is a very important concept for C + + object-oriented programming.

The specific analysis is as follows:

1. Overload: overload from the overload translation, refers to the same accessible area has been declared several different parameter columns (parameter types, number of different order) of the same name function, based on the argument list to determine which function to invoke, overload does not care about 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, not overloaded
};

The first four are overloaded functions, the last and the first are not overloaded functions.

2. Hiding: a function of a derived class masks a base class function with the same name. Note 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
:
    //hidden
  void fun1 (double i) {
    cout << "b::fun1 ():" << I << E NDL;
  }
;
int main () {
  b b;
    B.FUN1 (5);          Call functions in Class B
  b.fun1 (1, 2);        Error, because the base class function is hidden
  system ("pause");
  return 0;
}    

3. Rewriting: rewriting translations from override to overwriting (better) refers to the existence of redefined functions in derived classes. Its function name, argument list, return value type, all must be consistent with the overridden function in the base class. Only the function body is different (in curly braces), the derived class invokes the overridden function of the derived class, and the overridden function is not invoked. The overridden function in the overridden 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
:
    //override
  virtual void fun3 (double i) {
    cout << "B::fun3 ():" << i &L t;< 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 to achieve polymorphic code, do not understand the first to see the virtual function to achieve the principle of polymorphism.

the difference between overloading and overriding:

(1) Range difference: rewrite and overridden functions in different classes, overloaded and overloaded functions are in the same class.

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

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

hiding and overriding, the difference between overloads:

(1) differs from the overload range: hidden functions and hidden functions in different classes.

(2) The difference between the parameters: the hidden function and the hidden function parameter list can be the same, but also different, but the function name must be the same; when the argument is not the same, the base class function is hidden, not overridden, regardless of whether the function in the base class is decorated by virtual.

Debug runs 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
:
    //hidden
  void fun1 (double i) {
    cout << "b::fun1 ():" << i << Endl;
  }
    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 (m);       Hide, call B's function
  system ("pause");
  return 0;
}

The output results are:

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

This article is reproduced from: http://www.jb51.net/article/54225.htm

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.