Reload rewriting and redefinition

Source: Internet
Author: User

Overload: a function with the same name and different parameters can appear in a class or scope. overloading can be divided into function overloading and operator loading.

Function overload instance:

# Include <iostream> using namespace STD; // compare the size of two int types, and return the maximum number int max (int A, int B) {int C; C = (A> B )? A: B; return C ;}// compare the size of two double types, and return the maximum number of double max (double A, double B) {double C; C = (A> B )? A: B; return C ;}// compare the two characters and return the largest char max (char a, char B) {char C; C = (A> B )? A: B; return C;} void main () {cout <"Maximum int type:" <max (123, 23) <Endl; cout <"Maximum double:" <max (1.23, 2.3) <Endl; cout <"Maximum character:" <max ('A ', 'Z') <Endl; System ("pause ");}


Execution result:


Note:

The above program uses three max functions. The three functions have the same name and different function parameters. This is the application of function overloading.


Operator Overloading: It is to redefine existing operators and assign them another function to adapt to different data types.


As for operator overloading, I have already written some blogs. For more information, see:

Design the string class and use operators to overload http://blog.csdn.net/u010105970/article/details/30253771 in the string class

Using a friend function http://blog.csdn.net/u010105970/article/details/30025343 in Operator Overloading

Use Operator Overloading to implement the four arithmetic http://blog.csdn.net/u010105970/article/details/30022851 of the plural

Http://blog.csdn.net/u010105970/article/details/29855667 for addition operations of complex numbers using Operator Overloading

Design the ctime class to overload http://blog.csdn.net/u010105970/article/details/30460881 with operators in the ctime class

Heavy Load http://blog.csdn.net/u010105970/article/details/31395025

Input and Output http://blog.csdn.net/u010105970/article/details/31402615 of the plural by reloading the input and output operators

Realize the operation http://blog.csdn.net/u010105970/article/details/31734497 between the plural and the plural in the self-defined type conversion function of the plural class

Reload http://blog.csdn.net/u010105970/article/details/31745291 in the ctime class


Use Function overloading in the class:

# Include <iostream> using namespace STD; Class Parent {public: void func () {cout <"Void func ()" <Endl ;} void func (int I) {cout <"Void func (INT I)" <Endl;} void func (int I, Int J) {cout <"Void func (int I, Int J)" <Endl ;}}; int main () {parent P; // defines a Class Object p. func (); p. func (1); p. func (1, 2); System ("pause ");}

Execution result:


Note:

This example shows that function Overloading is not only applicable to scopes, but also to classes.


Rewrite: When a subclass inherits a parent class, and the names of methods in the subclass and the methods in the parent class are identical in number and type, this method in the subclass overwrites the method in the parent class.


Sample Code:

# Include <iostream> using namespace STD; Class Parent // parent class {public: void func () {cout <"Void func ()" in the parent class <Endl ;} virtual void func (int I) {cout <"Void func (INT I)" <Endl;} virtual void func (int I, Int J) {cout <"Void func (int I, Int J)" <Endl ;}}; class child: Public parent {public: Virtual void func (int I, int J) {cout <"Void func (int I, Int J)" <Endl;} virtual void func (int I, Int J, int K) {cout <"Void func (int I, Int J, int K) in the subclass" <Endl ;}; void main () {Child C; C. func (1, 2); C. func (1, 2, 3); System ("pause ");}

Execution result:


Note:

1. It must be in inheritance. 2. The method name, number of parameters, and parameter type must be the same. 3. The J return value type can be the same as the parent class or different from the parent class, however, the return value type must be a subclass of the parent class. For example, the return value type of the parent class is 4. The access permission of the method rewritten by the derived class cannot be lower than that of the base class 5. The exception thrown by the derived class should be equal to or smaller than the base class

6. If the parent class does not contain the virtual keyword, the relation between the parent and child is redefined (static link encoding)

7. If the parent class has a virtual keyword, the relationship between the parent and child is called virtual function rewriting. In this case, polymorphism occurs (Dynamic Link encoding is delayed)

Zookeeper

Reload rewriting and redefinition

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.