c++ 函数重载 函数模板_C ++中的函数重载

c++ 函数重载 函数模板

If any class have multiple functions with same names but different parameters then they are said to be overloaded. Function overloading allows you to use the same name for different functions, to perform, either same or different functions in the same class.

如果任何类具有名称相同但参数不同的多个函数,则称它们已重载。 函数重载使您可以对不同的函数使用相同的名称,以在同一类中执行相同或不同的函数。

Function overloading is usually used to enhance the readability of the program. If you have to perform one single operation but with different number or types of arguments, then you can simply overload the function.

函数重载通常用于增强程序的可读性。 如果您必须执行一个操作,但是要使用不同数量或类型的参数,则只需重载该函数即可。

重载函数的不同方法 (Different ways to Overload a Function)

  1. By changing number of Arguments.

    通过更改参数数量。

  2. By having different types of argument.

    通过具有不同类型的参数。

函数重载:不同数量的参数 (Function Overloading: Different Number of Arguments)

In this type of function overloading we define two functions with same names but different number of parameters of the same type. For example, in the below mentioned program we have made two sum() functions to return sum of two and three integers.

在这种类型的函数重载中,我们定义两个具有相同名称但具有相同类型参数数量的函数。 例如,在下面提到的程序中,我们制作了两个sum()函数以返回两个和三个整数的和。

// first definition
int sum (int x, int y)
{
    cout << x+y;
}

// second overloaded defintion
int sum(int x, int y, int z)
{
    cout << x+y+z;
}

Here sum() function is said to overloaded, as it has two defintion, one which accepts two arguments and another which accepts three arguments. Which sum() function will be called, depends on the number of arguments.

这里的sum()函数被称为重载,因为它有两个定义,一个定义接受两个参数,另一个定义接受三个参数。 将调用哪个sum()函数,取决于参数的数量。

int main()
{
    // sum() with 2 parameter will be called
    sum (10, 20);  

    //sum() with 3 parameter will be called
    sum(10, 20, 30);  
}

30 60

30 60

函数重载:不同的参数数据类型 (Function Overloading: Different Datatype of Arguments)

In this type of overloading we define two or more functions with same name and same number of parameters, but the type of parameter is different. For example in this program, we have two sum() function, first one gets two integer arguments and second one gets two double arguments.

在这种类型的重载中,我们定义了两个或多个具有相同名称和相同数量参数的函数,但参数类型不同。 例如,在此程序中,我们有两个sum()函数,第一个获得两个整数参数,第二个获得两个双精度参数。

// first definition
int sum(int x, int y)
{
    cout<< x+y;
}

// second overloaded defintion
double sum(double x, double y)
{
    cout << x+y;
}

int main()
{
    sum (10,20);
    sum(10.5,20.5);
}

30 31.0

30 31.0

具有默认参数的函数 (Functions with Default Arguments)

When we mention a default value for a parameter while declaring the function, it is said to be as default argument. In this case, even if we make a call to the function without passing any value for that parameter, the function will take the default value specified.

当我们在声明函数时提及参数的默认值时,它被称为默认参数。 在这种情况下,即使我们在未传递该参数任何值的情况下调用该函数,该函数也会采用指定的默认值。

sum(int x, int y=0)
{
    cout << x+y;
}

Here we have provided a default value for y, during function definition.

在此,我们在函数定义期间提供了y的默认值。

int main()
{
    sum(10);
    sum(10,0);
    sum(10,10);
}

10 10 20

10 10 20

First two function calls will produce the exact same value.

前两个函数调用将产生完全相同的值。

for the third function call, y will take 10 as value and output will become 20.

对于第三个函数调用,y将取值为10,输出将变为20。

By setting default argument, we are also overloading the function. Default arguments also allow you to use the same function in different situations just like function overloading.

通过设置默认参数,我们还将重载该函数。 默认参数还允许您在不同情况下使用同一函数,就像函数重载一样。

使用默认参数的规则 (Rules for using Default Arguments)

  1. Only the last argument must be given default value. You cannot have a default argument followed by non-default argument.

    只有最后一个参数必须被赋予默认值。 您不能在默认参数后跟非默认参数。

    sum (int x,int y);    
    sum (int x,int y=0);  
    sum (int x=0,int y);  // This is Incorrect
  2. If you default an argument, then you will have to default all the subsequent arguments after that.

    如果您默认一个参数,则此后必须默认所有后续参数。

    sum (int x,int y=0);
    sum (int x,int y=0,int z);  // This is incorrect
    sum (int x,int y=10,int z=10);  // Correct
  3. You can give any value a default value to argument, compatible with its datatype.

    您可以给任何值一个默认值作为参数,与其参数的数据类型兼容。

带有占位符参数的功能 (Function with Placeholder Arguments)

When arguments in a function are declared without any identifier they are called placeholder arguments.

当声明函数中的参数而没有任何标识符时,它们称为占位符参数。

void sum (int, int);

Such arguments can also be used with default arguments.

这样的参数也可以与默认参数一起使用。

void sum (int, int=0);

翻译自: https://www.studytonight.com/cpp/function-overloading.php

c++ 函数重载 函数模板

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值