黑马:函数高级(95~98)

本文介绍了C++中函数的三种关键特性:默认参数,允许函数在调用时可选传递部分参数;占位参数,要求调用时必须提供所有参数;以及函数重载,通过不同的参数列表实现同名函数的多种功能。文中通过实例详细解释了这些特性的使用和注意事项,并强调了返回值不能作为重载依据。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.函数默认参数

在c++中,函数的形参列表中的形参可以有默认值

#include<iostream>
using namespace std;
#include<string>
#include<vector>
#include<deque>
#include<iterator>
#include<list>
#include<algorithm>  //标准算法头文件
#include<numeric>
#include<map>
#include<set>
#include<utility>
#include<fstream>
#include<string>
#include<ctime>
#include<stack>
#include<queue>

int func(int a, int b=20, int c=30) {
	return a + b + c;
}

//注意事项

//1.如果某个位置已经有了默认参数,那么之后的参数都必须有默认值
//   (int a,int b=10)       可以
//   (int a=10,int b,int c) 不行

//2.如果函数声明有默认参数,函数实现就不能有默认参数,下面的代码看起来没问题,但运行会报错
//声明和实现只能有一个有默认参数
int func2(int a=10, int b=10);
int func2(int a=10, int b=10) {
	return a + b;
}


int main(int argc,char**argv) {  


	cout<<func(10)<<endl;
	//如果是cout<<func(10,30) 就会是10+30+30=70 


	system("pause");
	return  0;
}




2.函数占位参数

作用:用来占位,调用函数时必须填补该位置

#include<iostream>
using namespace std;
#include<string>
#include<vector>
#include<deque>
#include<iterator>
#include<list>
#include<algorithm>  //标准算法头文件
#include<numeric>
#include<map>
#include<set>
#include<utility>
#include<fstream>
#include<string>
#include<ctime>
#include<stack>
#include<queue>

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

int main(int argc,char**argv) {  


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


	system("pause");
	return  0;
}




3.函数重载

作用:函数名可以相同,提高复用性

函数重载满足条件:

同一个作用域下

函数名称相同

函数参数类型不同 或者 个数不同 或者 顺序不同

注意:函数的返回值不可以作为函数重载的条件

#include<iostream>
using namespace std;
#include<string>
#include<vector>
#include<deque>
#include<iterator>
#include<list>
#include<algorithm>  //标准算法头文件
#include<numeric>
#include<map>
#include<set>
#include<utility>
#include<fstream>
#include<string>
#include<ctime>
#include<stack>
#include<queue>

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(int argc,char**argv) {  

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


	system("pause");
	return  0;
}




4.函数重载注意事项

引用作为重载条件

函数重载碰到函数默认参数

#include<iostream>
using namespace std;
#include<string>
#include<vector>
#include<deque>
#include<iterator>
#include<list>
#include<algorithm>  //标准算法头文件
#include<numeric>
#include<map>
#include<set>
#include<utility>
#include<fstream>
#include<string>
#include<ctime>
#include<stack>
#include<queue>

//1.引用作为重载的条件
void func(int &a) {          //      int &a=10 不合法
	cout << "func(int &a)的调用" << endl;
}

void func(const int& a) {   //       const int &a=10 合法(前面所学的知识!)
	cout << "func(const int &a)的调用" << endl;
}


//2.函数重载碰到默认参数
void func2(int a) {    
	cout << "func(int a)的调用" << endl;
}

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


int main(int argc,char**argv) {  

	int a = 10; 
	func(a);  //a是变量,可读可写,会调用上面的代码,不会走const

	func(10); //调用const,原因看void注释

	// func2(10); 上面的也可以调用,下面的也可以调用,出现二义性,这行会报错
	
	


	system("pause");
	return  0;
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值