目录
本阶段主要针对C++面向对象编程技术做详细讲解,探讨C++中的核心和精髓。
1 内存分区模型
C++程序在执行时,将内存大方向划分为4个区域
- 代码区:存放函数体的二进制代码,有操作系统进行管理的
- 全局区:存放全局变量和静态变量以及常量
- 栈区:由编译器自动分配释放,存放函数的参数值、局部变量等
- 堆区:由程序员分配和释放,若程序员不释放,程序结束时由操作系统回收
内存四区的意义:
不同区域存放的数据,赋予不同的生命周期,给我们更大的灵活编程。
1.1 程序运行前
在程序编译后,生成了exe可执行程序,未执行该程序前分为两个区域
代码区:
存放CPU执行的机器指令
代码区是共享的,共享的目的是对于频繁被执行的程序,只需要在内存中由一份代码即可
代码区是只读的,使其只读的原因是防止程序意外地修改了它的指令
全局区:
全局变量和静态变量存放在此
全局区还包含了常量区,字符串常量和其他常量也存放在此
该区域的数据在程序结束后由操作系统释放
#include<iostream>
using namespace std;
/*
全局区
全局变量、静态变量、常量
*/
//创建全局变量
int g_a = 10;
int g_b = 10;
//const修饰的全局变量,全局常量
const int c_g_a = 10;
const int c_g_b = 10;
int main() {
//创建普通局部变量
int a = 10;
int b = 10;
//创建const修饰的局部常量
const int c_l_a = 10;
const int c_l_b = 10;
cout << "局部变量a的地址为:" << (int)&a << endl;
cout << "局部变量b的地址为:" << (int)&b << endl;
cout << "全局变量a的地址为:" << (int)&g_a << endl;
cout << "全局变量b的地址为:" << (int)&g_b << endl;
//静态变量 在普通变量前面加static,属于静态变量
static int s_a = 10;
static int s_b = 10;
cout << "静态变量a的地址为:" << (int)&s_a << endl;
cout << "静态变量b的地址为:" << (int)&s_b << endl;
//常量
//字符串常量
cout << "字符串常量的地址为:" << (int)&"hello world" << endl;
//const修饰的变量
//const修饰的全局变量 const修饰的局部变量
cout << "全局常量 c_g_a的地址为:" << (int)&c_g_a << endl;
cout << "全局常量 c_g_a的地址为:" << (int)&c_g_a << endl;
cout << "局部常量 c_l_a的地址为:" << (int)&c_l_a << endl;
cout << "局部常量 c_l_a的地址为:" << (int)&c_l_b << endl;
system("pause");
return 0;
}
总结:
- C++中在程序运行前分为全局区和代码区
- 代码区特点是共享和只读
- 全局区中存放全局变量、静态变量、常量
- 常量区中存放const修饰的全局常量和字符串常量
1.2 程序运行后
栈区:
由编译器自动分配释放,存放函数的参数值、局部变量等
注意事项:不要返回局部变量的地址,栈区开辟的数据由编译器自动释放
#include<iostream>
using namespace std;
/*
栈区数据的注意事项---不要返回局部变量的地址
栈区的数据由编译器管理和释放
*/
int* func(int b)//形参数据也会放在栈区
{
b = 100;
int a = 0;//局部变量 存放在栈区,栈区的数据在函数执行完后自动释放
return &a;//返回局部变量的地址
}
int main() {
//接受func函数的返回值
int * p = func(1);
cout << *p << endl;//第一次可以打印正确的数字,是因为编译器做了保留
cout << *p << endl;//第二次这个数据就不再保留了
system("pause");
return 0;
}
堆区:
由程序员分配释放,若程序员不释放,程序结束时由操作系统回收
在C++中主要利用new在堆区开辟内存
#include<iostream>
using namespace std;
/*
堆区
*/
int* func()
{
//利用new关键字 可以将数据开辟到堆区
//指针 本质也是局部变量,放在栈上,指针保存的数据是放在堆区
int* p = new int(10);
return p;
}
int main() {
//在堆区开辟区域
int* p = func();
cout << *p << endl;
system("pause");
return 0;
}
1.3 new操作符
C++中利用new操作符在堆区开辟数据
堆区开辟的数据,由程序员手动开辟,手动释放,释放利用操作符delete
语法:new 数据类型
利用new创建的数据,会返回该数据对应的类型的指针
#include<iostream>
using namespace std;
/*
new操作符
*/
//1.new得基本语法
int* func()
{
//在堆区创建整型数据
//new返回是该数据类型得指针
int* p = new int(10);
return p;
}
void test01()
{
int* p = func();
cout << *p << endl;
cout << *p << endl;
cout << *p << endl;
//堆区的数据 由程序员管理开辟,程序员管理释放
//如果想释放堆区得数据,利用关键字delete
delete p;
//cout << *p << endl;//内存已经被释放,在此访问就是非法操作,会报错
}
//2.在堆区利用new开辟数组
void test02()
{
//创建10整型数据得数组,在堆区
int* arr = new int[10];//10代表数组有10个元素
for (int i = 0; i < 10; i++)
{
arr[i] = i + 100;//元素赋值 100~109
}
for (int i = 0; i < 10; i++)
{
cout << arr[i] << endl;//打印
}
//释放堆区数组
//释放数组的时候 要加[]才可以
delete[] arr;
}
int main() {
test01();
test01();
system("pause");
return 0;
}
2 引用
2.1 引用的基本使用
作用:给变量起别名
语法:数据类型 &别名 = 原名
#include<iostream>
using namespace std;
/*
引用的基本语法
*/
int main() {
int a = 10;
int& b = a;
cout << "a = " << a << endl;
cout << "b = " << b << endl;
b = 100;
cout << "a = " << a << endl;
cout << "b = " << b << endl;
system("pause");
return 0;
}
2.2 引用的注意事项
- 引用必须初始化
- 引用在初始化后,不可以改变
#include<iostream>
using namespace std;
/*
引用的注意事项
*/
int main() {
int a = 10;
//1.引用必须初始化
//int& b;//错误,未初始化
int& b = a;
//2.引用在初始化之后,不可以改变
int c = 100;
b = c;//赋值操作,而不是更改引用
system("pause");
return 0;
}
2.3 引用做函数参数
作用:函数传参时,可以利用引用的技术让形参修饰实参
优点:可以简化指针修改实参
#include<iostream>
using namespace std;
/*
引用做函数参数
交换函数
*/
//1.值传递
void mySwap01(int a, int b)
{
int temp = a;
a = b;
b = temp;
}
//2.地址传递
void mySwap02(int* a, int* b)
{
int temp = *a;
*a = *b;
*b = temp;
}
//3.引用传递
int mySwap03(int& a, int& b)
{
int temp = a;
a = b;
b = temp;
}
int main() {
int a = 10;
int b = 20;
mySwap01(a, b);
cout << "值传递:" << endl;
cout << "a = " << a << endl;
cout << "b = " << b << endl;
mySwap02(&a, &b);
cout << "地址传递:" << endl;
cout << "a = " << a << endl;
cout << "b = " << b << endl;
mySwap03(a, b);
cout << "引用传递:" << endl;
cout << "a = " << a << endl;
cout << "b = " << b << endl;
system("pause");
return 0;
}
总结:通过引用参数产生的效果同按地址传递是一样的。引用的语法更加清楚简单。
2.4 引用做函数返回值
作用:引用是可以作为函数的返回值存在的
注意:不要返回局部变量引用
用法:函数调用作为左值
#include<iostream>
using namespace std;
/*
引用做函数返回值
*/
//1.不要返回局部变量的引用
int& test01()
{
int a = 10;//局部变量存放在四区中的 栈区
return a;
}
//2.函数的调用可以作为左值
int& test02()
{
static int a = 10;//静态变量 存放在全局区,全局区上的数据在程序结束之后释放
return a;
}
int main() {
int& ref = test01();
cout << "ref = " << ref << endl;//第一次结果正确,是因为编译器锁了保留
cout << "ref = " << ref << endl;//第二次结果错误,因为a的内存已经被释放
int& ref2 = test02();
cout << "ref2 = " << ref2 << endl;
cout << "ref2 = " << ref2 << endl;
test02() = 1000;
cout << "ref2 = " << ref2 << endl;
cout << "ref2 = " << ref2 << endl;
system("pause");
return 0;
}
2.5 引用的本质
本质:引用的本质在C++内部实现是一个指针常量
#include<iostream>
using namespace std;
/*
引用的本质在C++内部实现是一个指针常量
*/
//发现是引用,转换为int* const ref = &a
void func(int& ref)
{
ref = 100;//ref是引用,转换为*ref = 100
}
int main() {
int a = 10;
//自动转换为 int* const ref = &a; 指针常量是指针指向不可改,也说明为什么引用不可改
int& ref = a;
ref = 20;//内部发现ref是引用,自动帮我们转换为:*ref = 20;
cout << "a:" << a << endl;
cout << "ref:" << ref << endl;
func(a);
system("pause");
return 0;
}
总结:C++推荐用引用技术,因为语法方便,引用本质是指针常量,但是所有的指针操作编译器都帮我们做了。
2.6 常量引用
作用:常量引用主要用来修饰形参,防止误操作
在函数形参列表中,可以加const修饰形参,防止形参改变实参。
#include<iostream>
using namespace std;
/*
常量引用
使用场景:用来修饰形参,防止误操作
*/
//打印数据函数
void showValue(const int& val)
{
//val = 1000;//错误,const防止误操作
cout << "val = " << val << endl;
}
int main() {
//int& ref = 10;//错误,引用必须引一块合法的内存空间
//const int& ref = 10;//加上const之后 编译器将代码修改 int temp = 10; const int& ref = temp;
//ref = 20;//错误,加入const之后变为只读,不可以修改
int a = 100;
showValue(a);
system("pause");
return 0;
}
3 函数提高
3.1 函数默认参数
在C++中,函数的形参列表中的形参是可以有默认值的。
语法:返回值类型 函数名 (参数 = 默认值){}
#include<iostream>
using namespace std;
/*
函数默认参数
如果自己传入了数据就会使用自己的数据
如果没有传入数据,就会使用默认数据
注意事项:
1.如果某个位置已经有了默认参数,那么从这个位置往后,从左到右都必须有默认值
2.如果函数声明有默认参数,函数实现就不能有默认参数
*/
int func(int a, int b = 20, int c = 30)
{
return a + b + c;
}
int main() {
cout << func(10, 20, 30) << endl;
cout << func(10) << endl;
cout << func(10, 30) << endl;
system("pause");
return 0;
}
3.2 函数占位参数
C++中函数的形参列表里可以有占位参数,用来做占位,调用函数时必须填补该位置
语法:返回值类型 函数名 (数据类型){}
#include<iostream>
using namespace std;
/*
函数占位参数
占位参数也可以有默认参数
*/
void func(int a, int)
{
cout << "This is a func" << endl;
}
int main() {
func(10, 10);//占位参数必须填补
system("pause");
return 0;
}
3.3 函数重载
3.3.1 函数重载概述
作用:函数名可以相同,提高复用性。
函数重载满足条件:
- 同一个作用域下
- 函数名称相同
- 函数参数 类型不同 或者 个数不同 或者 顺序不同
注意:函数的返回值不可以作为函数重载的条件
#include<iostream>
using namespace std;
/*
函数重载
作用:函数名可以相同,提高复用性。
*/
//函数重载满足条件:
//1.同一个作用域下
//2.函数名称相同
//3.函数参数 类型不同 或者 个数不同 或者 顺序不同
void func()
{
cout << "func的调用" << endl;
}
void func(int a)
{
cout << "func(int a)的调用" << endl;
}
void func(double a)
{
cout << "func(double a)的调用" << endl;
}
void func(double a,int b)
{
cout << "func(double a,int b)的调用" << endl;
}
void func(int b, double a)
{
cout << "func(int b, double a)的调用" << endl;
}
int main() {
func();
func(1);
func(3.14);
func(3.14, 10);
func(1, 3.14);
system("pause");
return 0;
}
3.3.2 函数重载注意事项
- 引用作为重载条件
- 函数重载碰到函数默认参数
#include<iostream>
using namespace std;
/*
函数重载注意事项
1.引用作为重载条件
2.函数重载碰到函数默认参数
*/
void func(int& a)//int &a = 10;不合法
{
cout << "func(int& a)的调用" << endl;
}
void func(const int& a) // const int& a = 10; 合法
{
cout << "func(const int& a)的调用" << endl;
}
void func2(int a, int b=10)
{
cout << "func2(int a, int b=10)的调用" << endl;
}
void func2(int a)
{
cout << "func2(int a)的调用" << endl;
}
int main() {
int a = 10;
func(a);//调用func(int& a)
func(10);//调用func(const int& a)
func2(10);//当函数碰到默认参数出现二义性,报错,尽量避免
system("pause");
return 0;
}