2021-01-27 打卡学习C++第七天


一、C++中的引用

1、引用的基本使用

给变量起别名
语法:数据类型 &别名 = 原名

示例

#include<iostream>
using namespace std;

int main()
{
    
    
	int a = 10;
	int &b = a;
	cout << "a = " << a << endl;
	cout << "b = " << b << endl;

	a = 100;
	cout << "a = " << a << endl;
	cout << "b = " << b << endl;

	b = 200;
	cout << "a = " << a << endl;
	cout << "b = " << b << endl;
	
	system("pause");
	return 0;

}

输出结果
在这里插入图片描述

2、引用注意事项

  • 引用必须初始化

  • 引用在初始化后,不可以改变

示例

#include<iostream>
using namespace std;

int main()
{
    
    
	//int &a;//错误,引用必须初始化,展示是谁的别名
	int a = 10;
	int b = 20;
	int &c = a;

	//引用 在初始化后不可更改
	c = b;//这是正确的,因为这是赋值操作

	cout << "a = " << a << endl;
	cout << "b = " << b << endl;
	cout << "c = " << c << endl;

	system("pause");
	return 0;

}

输出结果
在这里插入图片描述

3、引用做函数参数

函数传参时,可以利用引用的技术让形参修饰实参,
可以简化指针修改实参

(类似地址传递)

示例

#include<iostream>
using namespace std;

//1、值传递
void swap01(int a, int b)
{
    
    
	int temp = a;
	a = b;
	b = temp;
	cout << "swap01a = " << a << endl;
	cout << "swap01b = " << b << endl;
}

//2、地址传递
void swap02(int *a, int *b)
{
    
    
	int temp = *a;
	*a = *b;
	*b = temp;
	cout << "swap02 a = " << *a << endl;
	cout << "swap02 b = " << *b << endl;
}

//3、引用传递
void swap03(int &a, int &b)
{
    
    
	int temp = a;
	a = b;
	b = temp;
	cout << "swap03 a = " << a << endl;
	cout << "swap03 b = " << b << endl;
}

int main()
{
    
    
	int a = 10;
	int b = 20;
	swap01(a, b);
	cout << "01 a = " << a << endl;
	cout << "01 b = " << b << endl;

	swap02(&a, &b);
	cout << "02 a = " << a << endl;
	cout << "02 b = " << b << endl;

	swap03(a, b);
	cout << "03 a = " << a << endl;
	cout << "03 b = " << b << endl;

	system("pause");
	return 0;

}

输出结果
在这里插入图片描述

4、引用做函数返回值

引用是可以作为函数的返回值存在的
【注意】 不要返回局部变量引用
用法:函数调用作为左值

示例

#include<iostream>
using namespace std;

//不要返回局部变量的引用
int& test01()  //相当于用引用的方式返回
{
    
    
	int a = 10;
	return a;
}

int& test02()
{
    
    
	static int a = 10;//静态变量存放在全局区,不会自动释放
	return a;
}

int main()
{
    
    
	int &ref = test01();
	cout << "ref = " << ref << endl;//第一次结果正确,以为编译器对其值进行保留
	//cout << "ref = " << ref << endl;//第二次结果错误,因为该值已被释放

	int &ref02 = test02();
	cout << "ref02 = " << ref02 << endl;//不论再次调用多少次,都会正常执行
	cout << "ref02 = " << ref02 << endl;

	system("pause");
	return 0;

}

引用的本质:在C++内部实现是一个指针常量

指针常量:指针的指向不可以改变,但是指针的值可以改变

常量引用:主要用来修饰形参,防止误操作
在函数列表中,可以加const修饰形参,防止形参改变实参
加上const 之后变为只读,不可以修改(类似地址传递处)

二、函数提高

1、函数默认参数

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

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

函数声明和函数实现只能有一个默认参数

示例

#include<iostream>
using namespace std;

int func(int a, int b, int c );  //函数声明

int main()
{
    
    
	cout << "不给默认值传入参数";
	cout << func(10) << endl;

	cout << "给默认值传入参数";
	cout << func(10,20) << endl;
	
	system("pause");
	return 0;

}

//函数实现
//如果自己在主函数中传入参数,则就用传入的参数,如果没有传入,则用默认值
int func(int a, int b = 10, int c = 30) //如果给了b默认参数,则b右边的参数都得有默认参数
{
    
    
	return a + b + b;
}

2、函数占位参数

C++中函数的形参列表里可以有占位参数,用来做占位,调用函数时必须填补该位置

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

示例

#include<iostream>
using namespace std;

//占位参数
void func1(int a, int  )
{
    
    
	cout << "hello world" << endl;
}
//占位参数可以有默认值
void func2(int a, int = 10)
{
    
    
	cout << "hello world" << endl;
}

int main()
{
    
    
	func1(10, 1);//占了位就必须往里面传数
	func2(10); //占位参数有默认值时,可以不传入数据

	
	system("pause");
	return 0;

}

3、参数重载

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

函数重载满足条件

  • 同一个作用域下

  • 函数名称相同

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

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

示例

#include<iostream>
using namespace std;

//1、func函数都在全局域
//2、函数名不同
//参数类型不同    符合函数重载条件
void func()
{
    
    
	cout << "func 的调用" << endl;
}

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

int main()
{
    
    
	
	func();
	func(10);
	system("pause");
	return 0;

}

函数重载的注意事项

  1. 引用作为重载条件时
  2. 函数重载碰到函数默认参数

示例

#include<iostream>
using namespace std;

//1、引用作为重载条件
void func(int &a) //int &a=10;是不合法的语法,所以腐女吧(10)无法调用此函数
{
    
    
	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 << "func(int a,int b) 的调用" << endl;
}

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

int main()
{
    
    
	int a = 10;
	func(a); 
	func(10);

	//func2(10);//当函数重载有默认参数时,出现两个够可以调用的情况,会出错

	system("pause");
	return 0;

}

【注释】 学习课程为-黑马程序C++教程

猜你喜欢

转载自blog.csdn.net/qq_42616280/article/details/113244708