C ++ overload, overwrite, and hide

Source: Internet
Author: User

Features of member functions being overloaded:
(1) the same range (in the same class );
(2) The function name is the same;
(3) parameters are different;
(4) virtual keywords are optional.
Override refers to the function of a derived class that overwrites the base class function. The features are as follows:
(1) different scopes (located in the derived class and the base class respectively );
(2) The function name is the same;
(3) The parameters are the same;
(4) basic functions must have virtual keywords.
"Hide" means that the function of the derived class shields the base class function with the same name as it,
The rules are as follows:
(1) If the function of the derived class has the same name as the function of the base class, but the parameter is different. In this case, functions of the base class will be hidden regardless of whether there is any virtual keyword (Be sure not to confuse them with overload ).
(2) If the function of the derived class has the same name and parameter as the function of the base class, but the base class function does not have the virtual keyword. (The only difference between this and overwrite is that there is no virtual) at this time, the base class function is hidden (be careful not to confuse with overwrite ).
In the following example:
(1) The Derived: f (float) function overwrites Base: f (float ).
(2) The Derived: g (int) function hides the Base: g (float) instead of the overload.
(3) The Derived: h (float) function hides Base: h (float) instead of overwrite.
 
# Include <iostream. h>

Class Base {
Public:
Virtual void f (floatx) {cout <"Base: f (float)" <x <endl ;}
Void g (floatx) {cout <"Base: g (float)" <x <endl;
Void h (floatx) {cout <"Base: h (float)" <x <endl ;}
};

Class Derived: publicBase {
Public:
Virtual void f (floatx) {cout <"Derived: f (float)" <x <endl ;}
Void g (intx) {cout <"Derived: g (int)" <x <endl ;}
Void h (floatx) {cout <"Derived: h (float)" <x <endl ;}
};
Void main (void ){
Derived d;
Base * pb = & d;
Derived * pd = & d;

// Good: behavior depends solely on type of the object
Pb-> f (3.14f); // Derived: f (float) 3.14a
Pd-> f (3.14f); // Derived: f (float) 3.14

// Bad: behavior depends on type of the pointer
Pb-> g (3.14f); // Base: g (float) 3.14
Pd-> g (3.14f); // Derived: g (int) 3 (surprise !)

// Bad: behavior depends on type of the pointer
Pb-> h (3.14f); // Base: h (float) 3.14 (surprise !)
Pd-> h (3.14f); // Derived: h (float) 3.14
 
 
Summary:
(1) overload selects the version of the function to be called Based on the function parameter list, while polymorphism selects the virtual function version to be called based on the actual type of the runtime object, the implementation of polymorphism is implemented by overwriting the virtual functions of the base class in the derived class. If the derived class does not override the virtual functions of the base class, the derived class automatically inherits the virtual function version of the base class. At this time, no matter whether the base class Pointer Points to the object is a base type or a derived type, the base class version of the virtual function is called; if the derived class overwrites the override of the virtual function of the base class, the virtual function version to be called is selected based on the actual type of the object at runtime, for example, if the base class Pointer Points to an object of the derived type, the virtual function version of the derived class is called to realize polymorphism. This can be said to be the relationship between polymorphism and coverage.
(2) The purpose of using polymorphism is to declare the function as virtual in the base class and overwrite the virtual function version of the base class in the derived class, at this time, the function prototype is consistent with the base class, that is, the same parameter type with the same name. If you add a new function version to a derived class, you cannot dynamically call the new function version of the derived class through the base class pointer, this new function version is only used as an overloaded version of the derived class. The overload is only valid in the current class.
(3) The heavy load is static and the polymorphism is dynamic. Further, the overload is irrelevant to the object type actually pointed to by the pointer, And the polymorphism is related to the object type actually pointed to by the pointer. If the base class pointer calls the overloaded version of the derived class, the C ++ compiler considers it illegal. The C ++ compiler only considers that the base class pointer can only call the overloaded version of the base class, the overload is only valid within the namespace scope of the current class. inheritance will lose the feature of the overload. Of course, if the base class pointer calls a virtual function at this time, then, it dynamically selects the virtual function version of the base class or the virtual function version of the derived class for specific operations. This is determined by the object type actually pointed to by the base class pointer, therefore, overloading is irrelevant to the object type actually pointed to by the pointer, and polymorphism is related to the object type actually pointed to by the pointer.
(4) virtual functions can also be overloaded, but the overload can only be effective within the current namespace scope.

 

 

From Fan Sheng Mu Hui

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.