Function overloading in C ++ syntax

Source: Internet
Author: User

Function overloading means that the same function name can correspond to multiple functions. For example, you can define multiple function implementations for the function name Add (). This function is used to calculate the sum of two operands. One function is to calculate the sum of two int-type numbers, the other is to calculate the sum of the two floating-point numbers, and the other is to calculate the sum of the two plural numbers. Each implementation corresponds to a function body. These functions have the same name, but their parameters have different types. This is the concept of function overloading. Function Overloading is particularly important in applications of classes and objects.

Function overloading requires the compiler to uniquely determine which function code should be executed when a function is called, that is, which function is used for implementation. When determining the function implementation, the number and type of function parameters must be distinguished. This means that during function overloading, functions with the same name are required to have different numbers of parameters or different parameter types. Otherwise, the overload cannot be implemented.

Different overload functions of parameter types

The following is an example of an overloaded function with different parameter types:

# Include
Int add (INT, INT );
Double add (double, double );

Void main ()
{
Cout <}

Int add (int x, int y)
{
Return X + Y;
}

Double add (double A, double B)
{
Return A + B;
}

In this program, the main () function calls two functions with the same name as Add. The add () function at the front corresponds to the function implementation of the sum of two int numbers, the next add () function corresponds to the implementation of two double-type number summation functions. This is the function overload.

The output result of the above program is:

15

15.5

Different overload functions on the number of parameters

The following is an example of an overloaded function with different numbers of parameters:

# Include
Int min (int A, int B );
Int min (int A, int B, int C );
Int min (int A, int B, int C, int D );

Void main ()
{
Cout <}

Int min (int A, int B)
{
Return}

Int min (int A, int B, int C)
{
Int T = min (A, B );
Return min (T, C );
}

Int min (int A, int B, int C, int D)
{
Int T1 = min (A, B );
Int t2 = min (c, d );
Return min (T1, T2 );
}

This program contains function overloading. The function name Min pair should have three different implementations. The function differentiation varies according to the number of parameters. In the three function implementations, the number of parameters is 2, 3, and 4, respectively. Different function implementations are selected based on the number of real parameters when a function is called.

Function Overloading is widely used in classes and objects, especially in class polymorphism. In the future, we will encounter more function overloading in different types, especially in combination with the inheritance of classes and pointer types, these are all frequently used in VC programming.

Related Article

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.