C++_基本语法笔记_函数相关

函数的默认参数

函数定义时,对形参进行赋值
语法:int function (int a, int b = 10)

  • 如果正常传入实参值,那么形参的默认参数将失效。(实参不够时,优先往前用)
int func(int a, int b = 20 ,int c = 30)
{
	return a + b +c;
}

void main()
{	
	// 30优先补充在第二个形参
	cout << func(10,30) << endl;	//70  = 10 + 30 + 30
	
	system("pause");
}
  • 默认参数后面,不能存在一般的参数:int function (int a = 10, int b)是错误的

  • 函数的 声明 和函数 定义 不能同时使用默认参数(编译器不知道按哪个默认参数,有二义性)

int func(int a = 10,int b = 20);	//声明

int func(int a = 20, int b = 30)	//定义
{
	return a + b;
}
// 虽然没有语法报错,但是不能成功运行

占位参数

形参列表可以用占位参数来占位,调用函数时候需要填补位置。

void func(int a,int )	//只写数据类型,不写形参
{
	cout << "say hello" << endl;
}
int main()
{
	func(10,20);		
	//第二个参数具体传去哪里,暂时不深究。
	system("pause");

}

函数重载

同一个函数名,使用不同的参数,就可以实现重名函数调用

void func()
{

	cout << "function1调用" <<  1111 << endl;
}

void func(int a, int b)
{
	cout << "function2调用" << a + b << endl;
}

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

void func(double a, int b)
{
	cout << "function4调用" << a * b + 1000 << endl;
}


void main()
{
	func();				//1111					与其他  参数个数不同
	func(1, 2);			//3  1+2	
	func(3, 2.2);		//6.6 3*2.2				与上一个参数类型不同
	func(2.2, 3);		//1006.6	6.6+1000	与上一个参数顺序不同

}

满足条件

  • 同一个作用域下
  • 函数命名一致
  • 参数的 类型 / 个数 / 顺序,任一不同即可重载(返回值不同,不可以重载)

函数重载的注意事项

  • 引用也可以作为重载条件
void function(int& a)	//int &a = 10 不合法
{
	cout << "function(int& a)调用" << endl;
}

void function(const int& a)	//const int &a = 10 合法
{
	cout << "function(const int& a)调用" << endl;
}

void main()
{
	int a = 10;
	const int b = 20;
	function(a);		// function(int& a)调用  
	// 因为a是可读可写的变量,而不是const常量
	
	function(10);		// function(const int& a)调用
	// 因为...请看上面函数定义后面的注释
	
	function(b);		// function(const int& a)调用  
	// 所以,其实变量是否可读写(是否常量),也是可以进行重载的条件
}
  • 函数重载遇到默认参数

在重载中,数参数个数的时候,会减掉默认参数,所以认为下面两个函数都可以调用

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

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

void main()
{
	funccc(10);	//不能编译通过,因为:
	//既可以调用第一个(参数个数相同)
	//也可以调用第二个(有默认参数,只输入第一个参数,所以参数个数也相同)
}

函数重载中尽量少用默认参数

构造函数和析构函数

类似生命周期钩子,系统自动调用,没有返回值也没有int,double的类型。

  • 对象P在函数test_gouzao_xigou中被创建,并且随着函数调用结束,对象P消亡,所以会运行析构函数~person。
class person
{
public:
	int age;
	string name;

	//构造函数
	person()
	{
		cout << "函数开始运行" << endl;
		age = 10;
		name = "qwe";
	}
	//析构函数
	~person()
	{
		cout << "函数运行结束" << endl;
	}

	string getName()
	{	
		return name;
	}
};

void test_gouzao_xigou()
{
	person p;
	string pname;
	pname = p.getName();
	cout<< pname <<endl;
}

int main(){
	test_gouzao_xigou();
	system("pause");
}


/*
函数开始运行
qwe
函数运行结束
*/
  • 如果对象p在main函数中创建,不会执行到析构函数~person,因为main中停在system(“pause”)这一步,对象并没有被释放。
class person
{
public:
	int age;
	string name;

	//构造函数
	person()
	{
		cout << "函数开始运行" << endl;
		age = 10;
		name = "qwe";
	}
	//析构函数
	~person()
	{
		cout << "函数运行结束" << endl;
	}

	string getName()
	{	
		return name;
	}
};

int main(){
	person p;
	cout << p.getName()<< endl;
	system("pause");
}

/*
函数开始运行
qwe
*/
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值