C++ 中的默认参数


2022年8月31日, 学习电子教程
2261

在本教程中,我们将通过一些示例学习 C++ 默认参数及其工作原理。

我们主要可以为 C++ 编程中的函数 参数提供默认值。假设您调用一个带有默认参数的函数而没有传递任何参数,那么将使用默认参数。如果函数在被调用时传递了参数,则默认参数将被忽略。

如果调用函数 未能为参数提供值,编译器将自动分配函数声明中提供的默认参数指定的值,并且如果传递了任何值,则默认值将被覆盖。

C++ 中默认参数的工作原理

现在让我们看看 C++ 中默认参数的工作原理。这里我们考虑四种情况

  1. 未传递参数
    Default Arguments

    当函数 **test()** 接收到调用时,它会使用两个默认参数。

  2. 所有参数都已传递
    Default Arguments

    当调用 **test(3, 3.6)** 时,两个参数的默认值都将被覆盖,结果是 **i = 3** 且 **f = -3.6**。

  3. 只传递第一个参数
    Default Arguments

    当调用 **test(3)** 时,第一个参数更改为 3,第二个参数设置为其默认值。

  4. 只传递第二个参数
    Default Arguments

    当传递 **temp(3.6)** 时,函数行为不当,因为在不传递第一个参数的情况下无法传递第二个参数。

    结果,3.6 被用作第一个参数。因为第一个参数是 int,所以实际传递的值是 3。

默认参数的示例


#include <iostream>

using namespace std;

// first define the default arguments
void display(char = '*', int = 3);

int main() {
    int count = 5;

    cout << "No argument passed: ";
    // *, 3 will be parameters
    display();

    cout << "First argument passed: ";
    // #, 3 will be parameters
    display('#');

    cout << "Both arguments passed: ";
    // $, 5 will be parameters
    display('$', count);

    return 0;
}

void display(char c, int count) {
    for (int i = 1; i <= count; ++i) {
        cout << c;
    }
    cout << endl;
}

输出

No argument passed: ***
First argument passed: ###
Both arguments passed: $$$$$

让我们看看上面的程序是如何工作的 

  • **display()** 在未传递任何参数的情况下被调用。在这种情况下,**display()** 使用默认值 **c = '*'** 和 **n = 1**。 
  • **display('#')** 在传递单个参数的情况下被调用。在这种情况下,第一个字符更改为 **'#'**。第二个默认参数 **n = 1** 保持不变。
  • 在传递两个参数的情况下,调用 **display('#', count)**,此时不使用默认参数。

1)    一个非常简单的 C++ 示例,展示了如何使用默认参数。在这种情况下,我们不需要编写三个求和函数,因为通过对第三个和第四个参数使用默认值,只有一个函数会起作用。


// CPP Program mainly to demonstrate the  Default Arguments
#include <iostream>

using namespace std;

// A function with default arguments,
// it can be called with
// 2 arguments or 3 arguments or 4 arguments.
int sum(int x, int y, int z = 0, int w = 0) //here we will be assigning default values to z,w as 0
{
    return (x + y + z + w);
}

// Driver Code
int main() {
    // Statement 1
    cout << sum(10, 15) << endl;

    // Statement 2
    cout << sum(10, 15, 25) << endl;

    // Statement 3
    cout << sum(10, 15, 25, 30) << endl;
    return 0;
}

输出

25
50
80

简单解释:由于在语句 1 中只传递了两个值,变量 z 和 w 的默认值为 0。语句 2 传递了三个值,因此 z 的值被数字 25 覆盖。在语句 3 中,z 和 w 的值分别被 25 和 30 替换,因为传递了四个值。

2)    如果用默认参数重载函数,我们必须确保它们对编译器没有歧义,否则它肯定会抛出错误。上面程序的修改版本如下


#include <iostream>

using namespace std;

//A function that accepts default arguments and can be called with
// 2 arguments or 3 arguments or 4 arguments.
int sum(int x, int y, int z = 0, int w = 0) {
    return (x + y + z + w);
}
int sum(int x, int y, float z = 0, float w = 0) {
    return (x + y + z + w);
}
// Driver Code
int main() {
    cout << sum(10, 15) << endl;
    cout << sum(10, 15, 25) << endl;
    cout << sum(10, 15, 25, 30) << endl;
    return 0;
}

输出

Error
prog.cpp: In function 'int main()':
prog.cpp:17:20: error: call of overloaded 
'sum(int, int)' is ambiguous
  cout << sum(10, 15) << endl; 
                    ^
prog.cpp:6:5: note: candidate: 
int sum(int, int, int, int)
 int sum(int x, int y, int z=0, int w=0) 
     ^
prog.cpp:10:5: note: candidate: 
int sum(int, int, float, float)
 int sum(int x, int y, float z=0, float w=0)

3. 构造函数也可以包含默认参数。默认构造函数可能没有参数,也可能带有默认参数。

C++ 代码,展示了如何在构造函数中使用默认参数。

#include <iostream>
using namespace std;
class A {
public:
    int sum = 0;
    A(); // default constructor with no argument
    A(int x = 0); // default constructor with one
                        // arguments
    
};

简单解释:这里显示的默认构造函数没有参数,其中一个有一个默认参数。带有参数的默认构造函数有一个默认参数 x,其值为 0。

默认参数的优点

  • 当我们想要扩展现有函数的功能时,只需向函数添加另一个默认参数即可实现。
  • 它有助于减小程序大小。
  • 它提供了一种直接有效的编程方法。
  • 使用默认参数可以提高程序的一致性。

默认参数的缺点

  • 它会延长执行时间,因为编译器必须用默认值替换函数调用中省略的参数。