[C++] Function improvement

1. Function default parameters

In C++, the parameters in the parameter list of the function can have default values.

grammar:返回值类型 函数名 (参数= 默认值){}

1. If a position parameter has a default value, then from this position, from left to right, there must be a default value

2. If the function declaration has a default value, it cannot have default parameters when the function is implemented [the declaration and implementation can only have a default parameter]

#include <iostream>
using namespace std;

//1. 如果某个位置参数有默认值,那么从这个位置往后,从左向右,必须都要有默认值
int func(int a, int b = 10, int c = 10) 
{
	return a + b + c;
}


//2. 如果函数声明有默认值,函数实现的时候就不能有默认参数【声明和实现只能有一个有默认参数】
int func2(int a = 10, int b = 10);
int func2(int a, int b) 
{
	return a + b;
}

int main(void) 
{

	cout << "ret = " << func(20, 20, 20) << endl;
	cout << "ret = " << func(20, 20) << endl;
	cout << "ret = " << func(100) << endl;


	cout << "ret = " << func2(100, 100) << endl;
	cout << "ret = " << func2(100) << endl;
	cout << "ret = " << func2() << endl;

	system("pause");

	return 0;
}

2. Function placeholder parameters

There can be a placeholder parameter in the formal parameter list of a function in C++, which is used as a placeholder, and the position must be filled when the function is called

grammar: 返回值类型 函数名 (数据类型){}

At this stage, the placeholder parameter of the function is of little significance, but this technique will be used in later courses

#include <iostream>
using namespace std;

//函数占位参数 
void func(int a, int) 
{
	cout << "this is func1" << endl;
}

//占位参数也可以有默认参数
void func2(int a, int=10)
{
	cout << "this is func2" << endl;
}

int main(void) 
{

	func(10, 10); //占位参数必须填补

	func2(12,12);
	func2(12);
	system("pause");

	return 0;
}

3. Function overloading

3.1 Overview of Function Overloading

Role: The function name can be the same to improve reusability

Function overloading satisfies the conditions:

  • In the same scope

  • Same function name

  • Function parameters of different types or a different number or a different order

Note: The return value of a function cannot be used as a condition for function overloading

#include <iostream>
using namespace std;

//函数重载需要函数都在同一个作用域下
void func()
{
	cout << "func 的调用!" << endl;
}

void func(int a)
{
	cout << "func (int a) 的调用!" << endl;
}

void func(double a)
{
	cout << "func (double a)的调用!" << endl;
}

void func(int a, double b)
{
	cout << "func (int a ,double b) 的调用!" << endl;
}

void func(double a, int b)
{
	cout << "func (double a ,int b)的调用!" << endl;
}

//函数返回值不可以作为函数重载条件
//int func(double a, int b)
//{
//	cout << "func (double a ,int b)的调用!" << endl;
//}


int main(void) {

	func();
	func(10);
	func(3.14);
	func(10, 3.14);
	func(3.14, 10);

	system("pause");

	return 0;
}

3.2 Precautions for Function Overloading

Reference as overload condition

Function overloading encounters function default parameters

#include <iostream>
using namespace std;

//函数重载注意事项
//1、引用作为重载条件

void func(int &a)
{
	cout << "func (int &a) 调用 " << endl;
}

void func(const int &a)
{
	cout << "func (const int &a) 调用 " << endl;
}


//2、函数重载碰到函数默认参数


//void func2(int a, int b = 10)
//{
//	cout << "func2(int a, int b = 10) 调用" << endl;
//}

void func2(int a)
{
	cout << "func2(int a) 调用" << endl;
}

int main(void) 
{

	int a = 10;
	func(a); //调用无const
	func(10);//调用有const


	//func2(10); //碰到默认参数产生歧义,需要避免

	system("pause");

	return 0;
}

Guess you like

Origin blog.csdn.net/Zhouzi_heng/article/details/113569503