Summary of C ++ overload, overrride, and redefine

Source: Internet
Author: User

When I opened the Forum last night, I saw a friend asking a question about the virtual function. Because of the headache, I got up at noon today and watched it again.

Question Portal: http://www.cppleyuan.com/viewthread.php? Tid = 7923

 

Some features of C ++ have not been used for a long time, resulting in a little unfamiliar, so I checked some information and reviewed it.

 

Let's look at several concepts:

Overload, override, or overwrite, and redefine)

(PS: Third, I'm not sure whether it should be called redefine in English. If you have any questions, please leave a message. Thank you)

I. Overload)
The function name is the same, but the number or sequence of its parameter table columns are different in type. However, it cannot be determined by the return type.
(1) the same scope (in the same scope );
(2) The function name is the same;
(3) parameters are different;
(4) virtual keywords are optional.
(5) return values can be different;

2. Rewrite (also known as override)
It refers to the virtual function of the base class re-defined by the derived class. The features are as follows:
(1) Not in the same scope (both the derived class and the base class );
(2) The function name is the same;
(3) The parameters are the same;
(4) The base-class function must have the virtual keyword and cannot have the static keyword.
(5) the return value is the same (or co-variant); otherwise, an error is returned. <-- the concept of co-variant is the first time that I know...

(6) The access modifier of the rewrite function can be different. Although the virtual is private, it is also possible to rewrite the derived class to public and protected.

Iii. Redefinition (also hidden)
(1) Not in the same scope (both the derived class and the base class );
(2) The function name is the same;
(3) return values can be different;
(4) parameters are different. In this case, functions of the base class will be hidden regardless of whether there is a virtual keyword (Be sure not to be confused with overload or overwrite ).
(5) The parameters are the same, but the basic functions do not have the virtual keyword. In this case, the function of the base class is hidden (do not confuse with overwrite ).

 

OK. Here is an example.CodeThe code for the Forum question has been modified to facilitate understanding:

1 # Include <iostream>
2 # Include <complex>
3 Using Namespace STD;
4
5 Class Base
6 {
7 Public :
8 Virtual Void A ( Int X) {cout < " Base: A (INT) " <Endl ;}
9 // Overload the base: A (INT) Function
10 Virtual Void A ( Double X) {cout < " Base: A (double) " <Endl ;}
11 Virtual Void B ( Int X) {cout < " Base: B (INT) " <Endl ;}
12 Void C ( Int X) {cout < " Base: C (INT) " <Endl ;}
13 };
14
15 Class Derived: Public Base
16 {
17 Public :
18 // Redefine the base: A () function
19 Void A (complex < Double > X) {cout <" Derived: A (complex) " <Endl ;}
20 // Override the base: B (INT) Function
21 Void B ( Int X) {cout < " Derived: B (INT) " <Endl ;}
22 // Redefine the base: C () function
23 Void C (Int X) {cout < " Derived: C (INT) " <Endl ;}
24 };
25
26 Int Main ()
27 {
28 Base B;
29 Derived D;
30 Base * pb = New Derived;
31 // ----------------------------------- //
32 B. (1.0 ); // Base: A (double)
33 D. ( 1.0 ); // Derived: A (complex)
34 Pb-> ( 1.0 ); // Base: A (double), this is redefine the base: A () function
35 // Pb-> A (complex <double> (1.0, 2.0 )); // Clear the annotation and have a try
36 // ----------------------------------- //
37 B. B ( 10 );// Base: B (INT)
38 D. B ( 10 ); // Derived: B (INT)
39 Pb-> B (10 ); // Derived: B (INT), this is the virtual function
40 // ----------------------------------- //
41 Delete Pb;
42
43 Return 0 ;
44 } Here we can see:

1. The second function a in the base class is an overload of the first function.

2. function B in the derived class overrides function B in the base class, that is, the virtual function feature is used.

3. function a in the derived class hides function a from the tears of base, that is, it is redefined.

4. the PB pointer is a pointer to the base type, but it actually points to a derived space. The processing (polymorphism) of the functions called by the PD depends on whether to rewrite the function (virtual function feature) if no, the base class is still called.

5. polymorphism takes effect only when the base class pointer or base class reference points indirectly to the derived class type.

6. Because Function C of the base class is not defined as a virtual function, Function C of the derived class is a redefinition of base: C.

Online references:

1. http://sns.linuxpk.com/blog-6583-17085.html

2. http://www.cnblogs.com/xd502djj/archive/2010/09/22/1832912.html

3. http://blog.sina.com.cn/s/blog_6ae7d6b00100pb4v.html

4. http://topic.csdn.net/u/20110227/23/42d93b05-03b1-460b-8521-707117ce5600.html

5. http://www.cnblogs.com/realyan/archive/2011/07/14/2106339.html

6. http://topic.csdn.net/u/20081023/18/122ac3bd-8ad2-4e6e-8624-090f22c82139.html

Personal blog original link: http://www.wutianqi.com /? P = 3171

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.