Overload, overlay, and hide in C + + introduction _c language

Source: Internet
Author: User

A few days ago when the interview was asked about C + + coverage, hidden, the concept of basic answer, only answer how to use the pointer to achieve polymorphism, there are also omissions. Finally Huff. Back after the search on the internet to learn some, did this summary. Some of the text borrowed from someone else's blog, look no offense.

• concept

I. Overloading (OVERLOAD)
The function name is the same, but the number or order of the parameter table columns is different from the type. But it cannot be judged by the type of return.
(1) The same range (in the same scope);
(2) The function name is the same;
(3) different parameters;
(4) virtual keyword is optional.
(5) The return value can be different;

Second, rewrite (also known as covering override)
refers to a derived class redefining a virtual function of a base class, characterized by:
(1) Not in the same scope (in the derived class and the base class, respectively);
(2) The function name is the same;
(3) the same parameter;
(4) The base class function must have the virtual keyword and cannot have static.
(5) The return value is the same (or covariant), otherwise the error; The concept of <--covariant this is the first time I know ...

(6) The access modifiers of the overriding function can be different. Although virtual is private, overriding rewriting as public,protected in a derived class is also possible

Third, redefine (also hidden)
(1) Not in the same scope (in the derived class and the base class, respectively);
(2) The function name is the same;
(3) The return value can be different;
(4) different parameters. At this point, the function of the base class is hidden regardless of the virtual keyword (note that the overload and overlay are not confused).
(5) The parameter is the same, but the base class function has no virtual keyword. At this point, the functions of the base class are hidden (note that you are confused with overrides).

• examples

#include <iostream> using namespace std;
  Class Sparent {public:sparent () {};
  Sparent (const sparent &p) {cout << "parent copy construct" << Endl;
    int add (int a,int b) {cout << "parent int add" << Endl;
  return a + B;
    Double Add (double a,double b) {cout << "parent double add" << Endl;
  return a + B;
    virtual int dec (int a,int b) {cout << "parent int Dec" << Endl;
  return a-b;
}
};
  Class Schild:public Sparent {public://using sparent::add;
    Float Add (float a,float b) {cout << "child float Add" << endl;
  return a + B;
    int Dec (int A, int b) {cout << "child int Dec" << Endl;
  return a-b;
}
};
  int main () {/* Test overload */sparent parent;
  Parent.add (3,5);
  Parent.add ((Double) 3, (double) 5);
  cout << Endl; /* Test coverage/Schild *pchild = (Schild *) New Sparent (),/* base class strong to subclass ... Danger ..., with dynamic_cast.It's not going to change either. Pchild->dec (10,3);
  Sparent *pparent = new Schild ();
  Pparent->dec (11,3);
  cout << Endl;
  /* Test Hidden * * Schild child;
  Child.add ((int) 3, (int) 5);
  cout << Endl;
  /* Test Function Table */((sparent *) NULL)->add (4,6);
  ((Schild *) NULL)->add (4,6);
  int a = 0;
   ((Schild *) &a)->add (4,6);
  cout << Endl;
  /* Test function address/* ((sparent) child). Add ((int) 4, (int) 8); Child.

  Sparent::add (3,5);
return 0; }

Output results:

Parent int Add
parent double add

parent int Dec child
int Dec child

float Add

parent int add
child Float Add child
float add

parent copy construct
parent int add
parent int add
press <RETURN> to close window Mouth...

• Understanding
int sparent::add (int a,int b) and double Sparent::add (double a,double b) are overloaded

int sparent::add (int a,int b) and double Sparent::add (double a,double b) are hidden by the float Schild (float Schild::add b) in the Quilt class A,float

int sparent::d ec (int a,int b) The int schild in the quilt Class Schild::d ec (int a,int B) Overlay

• Test

1. Overload test, easy to understand, skip.
2. Cover Test. The DEC function has the same name as the same parameter in the base class, subclasses, and is called a virtual function.

Schild *pchild = (Schild *) New Sparent () creates a base class object whose function table should be

Sparent *pparent = new Schild (); Create a subclass object whose function table should be

Visible from the above function table, when overwriting occurs, the function name of the subclass overwrites the base class function with the same name (which is why the overlay is called). So we can use a base class pointer that points to subclasses to implement polymorphism. But the focus is only one
Is exactly who the function table is pointing to (regardless of what type of pointer is converted). So the output is the parent class, subclass. This is a run-time polymorphism.

3. Hide Test

int sparent::add (int a,int b) and double Sparent::add (double a,double b) are covered by the float Schild (float Schild::add b) in the Quilt class a,float, which is Because they have the same name and are in different scopes (base classes, subclass scopes are different). Child.add ((int) 3, (int) 5); In this line of code, the compiler looks for the add function in a subclass and finds only one (the base class's Add (int a,int b) is compiled to bypass the hidden rule), and it is found to be applicable based on implicit type conversions. If the conversion cannot be implicit, the compilation is not possible. Hidden reason: Prevents implicit type conversions from causing an error. For example, an int can also be converted to char if the base class has a function add (char A,char b), and a subclass also has a function add (double a,double b). Programmers want to implicitly convert int to double in subclasses, but the compiler might tune the base class. This also prevents some libraries, or encapsulated base classes, from being bothered by programmers.

Like the code above, if you really need a function of the base class, you can use the using Sparent:add. The Add function field of the base class is extended to the subclass, which forms the overload.

4. function table Test

Above we talked about the function table, which is set at compile time, the program is loaded into memory when it runs. This means that we can call the function directly through the address. So ((Schild *) NULL)->add (4,6); This code is also able to run through. There are also people on the Internet to directly call the address of the function table. But this kind of code is not safe and not standard, there is a bigger problem. When a member variable needs to be invoked in a member function, the False object pointer definitely cannot find the member variable table and accesses the illegal memory directly.

5. function Address Test

With hidden, covered, how do we call the hidden, overwritten function? Here are two ways:

((sparent) child). Add ((int) 4, (int) 8);
Child. Sparent::add (3,5);

The first is a less efficient approach. In fact, it generates a temporary base class variable from the copy constructor to invoke the base class's add function.
The second type is accessed by:: Specify the domain. This method is the compiler based on the domain directly found the function address of the base class, with the function table does not have much relationship.

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.