2021-01-27 The seventh day of clocking in and learning C++


One, reference in C++

1. Basic use of references

Alias ​​variables
grammar:数据类型 &别名 = 原名

Example

#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;

}

Output result
Insert picture description here

2. Cautions for citation

  • Reference must be initialized

  • After the reference is initialized, it cannot be changed

Example

#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;

}

Output result
Insert picture description here

3. Quote as function parameter

When the function is passed parameters, you can use the technique of reference to make the formal parameters modify the actual parameters,
Can simplify the pointer to modify the actual parameter

(Similar to address delivery)

Example

#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;

}

Output result
Insert picture description here

4. Reference as function return value

References can exist as the return value of a function
[Note] Do not return local variable references.
Usage: function calls are used as lvalues

Example

#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;

}

The essence of reference: It is a pointer constant implemented in C++

Pointer constant: the point of the pointer cannot be changed, but the value of the pointer can be changed

Constant reference:Mainly used to modify formal parameters to prevent misoperation
In the function list, you can add const to modify the formal parameters to prevent the formal parameters from changing the actual parameters. After
adding const, it becomes read-only and cannot be modified (similar to address transfer)

Two, function improvement

1. Function default parameters

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

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

Function declaration and function implementation can only have one default parameter

Example

#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. 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:返回值类型 函数名 (数据类型) { }

Example

#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. Parameter overload

Function names can be the same to improve reusability

Function overloading satisfies the condition

  • In the same scope

  • Same function name

  • The function parameter types are different or the number is different or the order is different

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

Example

#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;

}

Notes on function overloading

  1. When quoted as overload condition
  2. Function overloading encounters function default parameters

Example

#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;

}

[Note] The learning course is-Dark Horse Program C++ Tutorial

Guess you like

Origin blog.csdn.net/qq_42616280/article/details/113244708