C++第二部分核心编程1(内存四区,引用,函数提高)

1.内存四区

1.1代码区

存放函数的二进制代码,由操作系统进行管理。

1.2全局区

存放全局变量,静态变量和常量。

#include<iostream>
using namespace std;


int g_a = 1;
const int c_g_a = 10;
int main()
{
	int a = 10;
	int b = 0;
	cout << "局部变量a的地址" << &a << endl;
	cout << "局部变量a的地址" << (int)&a << endl;

	cout << "局部变量b的地址" << &b << endl;
	cout << "局部变量b的地址" << (int)&b << endl;

	cout << "全局变量g_a的地址" << &g_a << endl;
	cout << "全局变量g_a的地址" << (int)&g_a << endl;

	//静态变量
	static int s_a = 10;
	cout << "静态变量s_a的地址" << &s_a << endl;
	cout << "静态变量s_a的地址" << (int)&s_a << endl;
	//常量
	//字符串常量
	cout << "字符串常量的地址为" << (int)&"hello world" << endl;

	//const修饰的常量
	//const修饰的全局变量
	//const修饰的局部变量
	cout << "const修饰的全局变量c_g_a的地址" << &c_g_a << endl;
	cout << "const修饰的全局变量c_g_a的地址" << (int)&c_g_a << endl;
	const int c_l_a = 10;
	cout << "const修饰的局部变量c_l_a的地址" << &c_l_a << endl;
	cout << "const修饰的局部变量c_l_a的地址" << (int)&c_l_a << endl;
	system("pause");
	return 0;
}

1.3 栈区

不要返回局部变量的地址。
栈区的数据由编译器管理开辟和释放。

int *func(int b)//形参数据放入栈区
{
	b = 100;
	int a = 10;//局部变量存放在栈区,数据会自动被释放
	return &a;//不能返回,会报错

}
int main()
{

	int *p = func();

	cout << *p << endl;

	system("pause");
	return 0;

}

1.4 堆区

由程序员分配释放。若不释放,则在程序结束时,由操作系统释放。
在c++中利用new在堆区开辟内存。

int *func()
{
	//利用new,将数据开辟到堆区
	//指针本质也是局部变量,放在栈区,指针保存的数据是放在堆区
	int *p=new int(10);
	return p;

}
int main()
{

	int *p = func();

	cout << *p << endl;
	

	system("pause");
	return 0;

}

1.5 new

利用new,将数据开辟到堆区。
指针本质也是局部变量,放在栈区,指针保存的数据是放在堆区。

int *func()
{
	在堆区创建整形数据
	new返回的是该数据类型的指针
	int *p = new int(10);
	return p;

}

void test01()
{
	int *p = func();
	cout << *p << endl;
	cout << *p << endl;
	堆区的数据由程序员管理开辟释放
	如果想释放堆区的数据,利用关键字delete
	delete p;
	cout << *p << endl;//会报错,已经释放了
}
void test02()
{
	创建10整形数据的数组,在堆区
	int *arr=new int[10];
	for (int i = 0; i < 10; i++)
	{
		arr[i] = i + 100;
	}
	for (int i = 0; i < 10; i++)
	{
		cout << arr[i] << endl;
	}
	释放堆区数组的时候,要加[]
	delete[]arr;

}
int main()
{

	new的基本语法

	test01();
	test02();


	system("pause");
	return 0;

}

2.引用

2.1引用

#include<iostream>
using namespace std;



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

	cout << a << endl;
	cout << b << endl;
	cout << (int)&a << endl;
	cout << (int)&b << endl;

	system("pause");
	return 0;
}

2.2引用的注意事项

int main()
{

	int a = 10;
	int b = 20;
	int &c = a;
	c = b;//做赋值操作,不是更改引用
	
		cout << a << endl;
		cout << b << endl;
		cout << c << endl;
	system("pause");
	return 0;
}

2.3 引用作函数参数

//值传递
void swap01(int num1,int num2)
{
	int temp = num1;
	num1 = num2;
	num2 = temp;
	cout << "值传递:" << endl;
	cout << "a=" << num1 << endl;
	cout << "b=" << num2 << endl;
}
//地址传递
void swap02(int *p1,int *p2)
{
	int temp = *p1;
	*p1 = *p2;
	*p2 = temp;
}
//引用传递
void swap03(int &a,int&b)
{
	int temp = a;
	a = b;
	b = temp;
}

int main()
{
	int a = 10;
	int b = 20;
	//swap01(a, b);
	//cout << "值传递:" << endl;
	//cout << "a=" << a << endl;
	//cout << "b=" << b << endl;
	//swap02(&a, &b);
	//cout << "地址传递:" << endl;
	//cout << "a=" << a << endl;
	//cout << "b=" << b << endl;
	swap03(a, b);
	cout << "a=" << a << endl;
	cout << "b=" << b << endl;
	system("pause");
	return 0;	
}

2.4引用做函数的返回值

不要反悔局部变量的引用.

int& test01()
{

	int a = 10;
	return a;

}

int& test02()
{

	static int a = 10;//静态变量,存放在全局区,全局区的数据在程序结束后系统释放
	return a;

}


int main()
{
	int &ref = test01();
	cout << ref << endl;//第一次结果正确,因为编译器做了保留
	cout << ref << endl;//第二次做了错误,因为a的内存已经释放

	int &ref2 = test02();
	cout << ref2 << endl;
	cout << ref2 << endl;
	test02() = 1000;//如果函数的返回值是引用,这个函数调用可以作为左值
	cout << ref2 << endl;
	cout << ref2 << endl;
	cout << ref2 << endl;
	system("pause");
	return 0;
}

2.5引用的本质

引用的本质在c++内部实现是一个指针常量,值可以改,地址不可改。

void func(int& ref)
{

	ref = 100;//ref是引用,转化为*ref=100

}

int main()
{
	int a = 10;
	int &ref = a;   //int * const ref=&a;指针常量是指针指向不可以改,也说明为什么引用不可更改
	ref = 20;//内部发现ref是引用,自动转化为*ref=20

	cout << "a=" << a << endl;
	cout << "ref=" << ref << endl;
	func(ref);
	cout << "a=" << a << endl;
	cout << "ref=" << ref << endl;


	system("pause");
	return 0;
}

2.6常量引用

作用:常量引用主要用于修饰形参,防止误操作。


void show( const int &val) // 加上const防止修改形参而影响实参,这里会报错,不让执行
{
	 val = 10;
	cout << "val=" << val << endl;
	
}

int main()
{
	int a = 10;
	 int &ref = 10;//不能这样,引用必须引用一块合法的内存空间
	const int &ref = 10;//加上const后,编译器将代码修改,int temp=10;const int& ref=temp;

	ref = 20;//加入const后,不可修改

	int a = 100;
	show(a);
	cout << "a=" << a<< endl;

	system("pause");
	return 0;
}

3.函数高级

3.1函数提高

#include<iostream>
using namespace std;
//函数的默认参数
//如果我们自己传入了数据,就用自己的数据,如果没用,就用默认值

//int func(int a, int b = 20, int c = 40); //若函数声明有了默认参数,则函数实现就不能有默认参数

int func(int a, int b=20, int c=40)//一个位置有了默认参数,则后面都要有。
{
	return a + b + c;
}
int main()
{

	cout << func(10,40) << endl;;




	system("pause");
	return 0;
}

3.2函数占位参数

占位参数可以有默认参数,比如int =10。

void func(int a, int)//占位参数
{
	cout << "it is me" << endl;
}

int main()
{
	func(10, 10);//那里占位了,这里得补上

	system("pause");
	return 0;
}```
## 3.3函数重载
int func(int a, int b)
{
	return a + b;
}
int func(int a)
{
	return a;
}
int main()
{

	cout<<func(10, 10);
	cout <<func(10);

	system("pause");
	return 0;
}

## 3.4函数重载注意事项
//引用作为重载条件
void func(int &a)
{
	cout << "func()调用" << endl;
}
void func(const int &a)
{
	cout << "func1()调用" << endl;
}
函数重载遇到默认参数
void func2(int a,int b=10)
{
	cout << "func2(int a,int b)调用" << endl;
}
void func2(int a,double b=10)
{
	cout << "func1()调用" << endl;
}

int main()
{
	int a = 0;
	//const int b=0;

	 func(0);
	func2(10);
	func2(10, 20);
	



	system("pause");
	return 0;
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值