Deep analysis of C + + function overload

Source: Internet
Author: User
Tags abs function definition function prototype
In C + +, we can also integrate functions with the same function to a function, without having to write several functions of different function names, which is called the overload of the function. The following is a detailed analysis of the function overload in C + +, the need for friends can come to refer to the next

When we open bottles and jars, we often encounter the embarrassment of finding the right tool because of different bottle sizes. So sometimes in order to open a bottle, home to prepare a variety of bottle opener. Also open a bottle, why so troublesome? So someone invented the multifunctional bottle opener, whether beer bottle soda bottle or Cork wine bottle can easily open.

However, the problem of bottle opener will also occur in the program design. For example, we want to write a function to find the absolute value of a number, but integers, floating-point numbers, double numbers have absolute values, but the function that they write the return value type is different. Like what:

Copy Code code as follows:


int iabs (int a);


float fabs (float a);


Double Dabs (double A);


Is this a bit of a way to have a variety of bottle opener feeling? Can we do a multifunctional bottle opener in the program design, and give all the data type absolute value to abs this function?

In C + +, we can also integrate functions of the same function into a function, without having to write several functions with different function names, which is called the Chóng (overload) load of functions. The essence of overloading is that multiple functions share the same function name.

Let's take a look at an instance of a function Overload: (Program 6.3)

Copy Code code as follows:


#include "iostream.h"


int abs (int a);//function prototype when parameter is integer data


Float ABS (float a);//function prototypes when the parameter is floating-point data


Double abs (double A);//function prototype when parameter is double data


int Main ()


{


int a=-5,b=3;


float c=-2.4f,d=8.4f;


double e=-3e-9,f=3e6;


cout << "a=" <<abs (a) <<endl << "b=" <<abs (b) <<endl;//the results returned by the output function


cout << "c=" <<abs (c) <<endl << "d=" <<abs (d) <<endl;


cout << "e=" <<abs (e) <<endl << "f=" <<abs (f) <<endl;


return 0;


}


int abs (int a)//function definition


{


cout << "int abs" <<endl;//shows which function was run


return (A>=0?A:-A);//If A is greater than or equal to zero, returns a, or a.


}


Float ABS (float a)


{


cout << "float ABS" <<endl;


return (A>=0?A:-A);


}


Double abs (double A)


{


cout << "Double abs" <<endl;


return (A>=0?A:-A);


}


Run Result:
int ABS


int ABS


a=5


b=3


Float ABS


Float ABS


c=2.4


d=8.4


Double ABS


Double ABS


e=3e-009


f=3e+006


The results of operation show that the ABS function can handle three different data types. So how can we create a "multi-function tool" for ourselves?

In fact, writing an overloaded function is not a hassle. First, we have to tell the computer that there are many definitions of the same function name, so we have to write a number of function prototypes for the same function name (such as the second to fourth line of program 6.3), and secondly, we need to correspond to these function prototypes, Write the definition of these functions separately (for example, after the main function body of program 6.3, the definition of three ABS functions).

But how does a computer identify these "tools" that are used in different environments?
if we don't know which tool to use in our daily life, we'll put each tool on it and try it, if only one tool fits, then we can definitely use it. But if there are two or two more tools available, we can't tell which is the right one to use.

The computer practice is similar to ours. A computer relies on the number of parameters in the parameter table, the data type and the order of the parameters in the function declaration to determine which function to run. Therefore, when the overloaded function parameter table is identical, the computer can not determine which function should run, so the program is wrong.

When we understand how the computer recognizes overloaded functions, we find that to write an overloaded function there are some places to pay attention to, that is: in overloaded functions, the parameters of any two functions in the parameter list, the data type and order of the parameters can not be exactly the same. For example, int func (int A,char b) and float func (int c,char D) cannot be overloaded because their number of arguments, the type and order of the parameters are exactly the same, even if the formal parameter names are different and the return value types are different.

When you invoke an overloaded function, you may not find an entirely appropriate function. At this point, you need to convert the data type. Since this approach can lead to data loss or inconsistent data types, and this situation is avoided as far as possible after full consideration of the problem, this issue is no longer discussed here. Interested readers can refer to other C + + reference materials.

Algorithm time: Overloaded functions
in a sense, overloaded functions are convenient for users of functions. In the previous section, we knew that if all the functions were written, it would be as easy to complete a program as a building block. However, if there are too many functions with similar names, then a lot of "bricks" may not be easy to put together. The user of a function doesn't have to worry about these little details when the creator of the function takes into account that a slightly different function should be run in different situations. However, the function name of the overloaded function should conform to its function, and if the function overload is completely different, it will greatly affect the readability of the program.

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.