《C++起航之离港篇》学习笔记

《C++起航之离港篇》学习笔记

1. C++语言引用
1.1 引用是什么

举个栗子,一个人姓名叫 罗XX,外号叫 萝卜头儿

引用就是变量的别名
符号:&

不能只有别名,引用不能单独存在

1.2 引用的应用
// *********************
//	基本数据类型的引用
// *********************
#include <iostream>
using namespace std;
int main(void)
{
	int a = 3;
	int &b = a;   // 引用必须初始化,a 的别名称为 b

	b = 10;   // 对别名的操作,其实就是对本身的操作
	cout << a << endl;
	return 0;
}

// *********************
//	结构体类型的引用
// *********************
#include <iostream>
using namespace std;

typedef struct
{
	int x;
	int y;
}Coor;   // 结构体 Coor 定义坐标
int main(void)
{
	Coor c1;
	Coor &c = c1;   // c1 结构体的别名为 c

	c.x = 10;
	c.y = 20;
	cout << c1.x << " " << c1.y << endl;
	return 0;
}
// *********************
//	指针类型的引用
//  类型  *&指针引用别名 = 指针;
// *********************
#include <iostream>
using namespace std;
int main(void)
{
	int a = 10;
	int *b = &a;   // & 取地址,指针 b 指向 a
	int *&c = b;  // 指针的引用, b 的别名为 c
	*c = 20;

	cout << a << endl;
	return 0;
}
// *********************
//	引用作为函数参数
// *********************
// C 语言
void fun(int *a, int *b)
{
	int c = 0;
	c = *a;
	*a = *b;
	*b = c;
}
int x = 10, y = 20;
fun(&x, &y)

// C++ 语言
void fun(int &a, int &b)  // 传入形参取别名
{
	int c = 0;
	c = a;
	a = b;
	b = c;
}
int x = 10, y = 20;
fun(x, y)
3. 关键字Const

作用:控制变化

// 常用数据类型
	const int x = 5;   // x 为常量
// 指针
	int x = 5;
	const int *p = &x;
	*p =3;  // 错误,*p 为常量
	x= 3;   // 正确,编译可通过

	int x = 3;
	int y = 5;
	int *const p = &x;
	p = &y;  // 错误, p 只能对 x 取地址,不能再指向他人
	*p =10;  // 正确,可赋值,可修改 x 值
// 引用
	int x = 3;
	int y = 5;
	int const &z = x;
	z=10;  // 错误,不能给常量赋值
	x= 10;  // 正确
// 函数
#include <iostream>
using namespace std;
void fun(const int &a, const int &b);

int main(void)
{
	int x = 3;
	int y = 5;
	fun(x,y);   // 错误,a, b 均为常量
	
	system("pause");
	return 0;
}

void fun(const int &a, const int &b)
{
	a = 10;
	b = 20;
}
4. 函数新特性
4.1 函数参数默认值

规则:有默认值的参数必须在参数表的最右端
声明函数时定义默认值,定义函数时不设置默认值
无实参使用默认值,否则用实参覆盖默认值

#include <stdlib.h>
#include <iostream>
using namespace std;

void fun(int i = 30, int j = 20, int k = 10);  // 从后面往前面一次赋值

int main (void)
{
	fun();
	fun(100);
	fun(100,200);
	fun(100,200,300);
	system("pause");
	return 0;
}

void fun(int i, int j, int k);
{
	cout << i << " " <<  j  << " " << k << endl;
}
4.2 函数重载

名称相同,参数可辨
前提:在相同的作用域内,用同一函数名定义的多个函数,参数类型和个数是不同的
好处:例如,求最大值,目的相同,但所求对象的类型、范围不同

#include <iostream>
#include <stdlib.h>
using namespace std;

void fun(int i = 30, int j = 20, int k = 10);  
void fun(double i, double j);

int main (void)
{
	fun(1.1,2.2);  // 调用第二个函数
	fun(1,2);  // 调用第一个函数,k采用默认值 

	system("pause");
	return 0;
}

void fun(int i, int j, int k);
{
	cout << i << " " <<  j  << " " << k << endl;
}
void fun(double i, double j);
{
	cout << i << " " <<  j  << endl;
}
4.3 内联函数

目的:节省调用时间
关键字:inline
思考:为什么不把所有函数都设置为内联函数?
原因:(1)内联编译是建议性的,由编译器决定;
(2)逻辑简单,调用频繁的函数使用内联,for循环等复杂的不能使用;
(3)递归函数无法使用内联函数。

#include <iostream>
#include <stdlib.h>
using namespace std;

inline void fun(int i = 30, int j = 20, int k = 10);  // 关键字:inline
inline void fun(double i, double j);   // 仅在函数声明处添加关键字,其他均相同

int main (void)
{
	fun(1.1,2.2);  // 调用第二个函数
	fun(1,2);  // 调用第一个函数,k采用默认值 

	system("pause");
	return 0;
}

void fun(int i, int j, int k);
{
	cout << i << " " <<  j  << " " << k << endl;
}
void fun(double i, double j);
{
	cout << i << " " <<  j  << endl;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

la_fe_

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值