C++进修——C++核心编程

内存分区模型

  C++程序在执行时,将内存大方向划分为4个区域

  • 代码区:存放函数体的二进制编码,由操作系统进行管理
  • 全局区:存放全局变量和静态变量以及常量
  • 栈区:由编译器自动分配释放,存放函数的参数值,局部变量等
  • 堆区:由程序员分配和释放,若程序员不释放,程序结束时由操作系统回收

  内存4区意义

  • 不同的区域存放不同的数据,赋予不同的生命周期,给我们更大的灵活编程

程序运行前

  在程序编译后,生成了exe可执行程序,未执行该程序前分为两个区域

  代码区

    存放CPU执行的机器指令

    代码区是共享的,共享的目的是对于频繁被执行的程序,只需要在内存中有一份代码即可

    代码区是只读的,使其只读的原因是防止程序意外的修改了它的指令

  全局区

    全局变量和静态变量存放在此

    全局区还包括了常量区,字符串常量和其他常量也存放在此

    该区域的数据在程序结束之后由操作系统释放

示例:

int g_a = 10;
int g_b = 10;

const int c_g_a = 10;
const int c_g_b = 10;

int main(){
   
	//局部变量不在全局区
	int a = 10;
	int b = 10;

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

	cout << (int)&g_a << endl;
	cout << (int)&g_b << endl;

	static int s_a = 10;
	static int s_b = 10;

	cout << (int)&s_a << endl;
	cout << (int)&s_b << endl;

	cout << (int)&"hello world" << endl;
	cout << (int)&"hello world1" << endl;

	cout << (int)&c_g_a << endl;
	cout << (int)&c_g_b << endl;

	//局部常量不在全局区中
	const int c_l_a = 10;
	const int c_l_b = 10;

	cout << (int)&c_l_a << endl;
	cout << (int)&c_l_b << endl;

	system("pause");
	return 0;
}

程序运行后

  栈区

    由编译器自动释放,存放函数的参数值,局部变量等

    注意事项:不要返回局部变量的地址,栈区开辟的数据由编译器自动释放

示例:

int * func(){
   
	int a = 10;
	return &a;
}

int main(){
   
	int *p = func();

	cout << *p << endl;
	cout << *p << endl;

	system("pause");
	return 0;
}

  注意:

    x64函数的调用通常使用寄存器来传递参数和返回值,如果想要达到x86的效果,可以在两次输出之前输入system("pause")

  堆区

    由程序员分配释放,若程序员不能释放,程序结束时由操作系统回收

    在C++中主要利用new在堆区开辟内存

示例:

int* func() {
   
	int* a = new int(10);
	return a;
}

int main() {
   
	int* p = func();

	cout << *p << endl;
	cout << *p << endl;

	system("pause");
	return 0;
}

new操作符

  C++中利用new操作符在堆区开辟数据

  堆区开辟的数,由程序员手动开辟,手动释放,释放利用操作符delete

  语法:new 数据类型

  利用new创建的数据,会返回该数据对应的类型的指针

基本语法:

int* func() {
   
	int* a = new int(10);
	return a;
}

int main() {
   
	int* p = func();

	cout << *p << endl;
	cout << *p << endl;

	delete p;

	system("pause");
	return 0;
}

示例:

int main() {
   
	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;

	system("pause");
	return 0;
}

引用

引用的基本使用

  作用:给变量起别名

  语法数据类型 &别名 = 原名

示例:

int main(){
   
	int a = 10;
	int &b = a;

	cout << a << endl;
	cout << b << endl;

	b = 100;

	cout << a << endl;
	cout << b << endl;

	system("pause");
	return 0;
}

引用注意事项

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

示例:

int main(){
   
	int a = 10;
	int b = 20;

	//错误,引用必须初始化
	//int &c;

	//一旦初始化后,就不可以改变
	int &c = a;

	//这是赋值操作,不是更改引用	
	c = b;

	cout << a << endl;
	cout << b << endl;
	cout << c << endl;

	system("pause");
	return 0;
}

引用做函数参数

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

示例:

void mySwap01(int a, int b) {
   
	int temp = a;
	a = b;
	b = temp;
}

void mySwap02(int* a, int* b) {
   
	int temp = *a;
	*a = *b;
	*b = temp;
}

void mySwap03(int& a, int& b) {
   
	int temp = a;
	a = b;
	b = temp;
}

int main() {
   
	int a = 10;
	int b = 20;

	mySwap01(a, b);

	cout << a << endl;
	cout << b << endl;

	mySwap02(&a, &b);

	cout << a << endl;
	cout << b << endl;

	mySwap03(a, b);

	cout << a << endl;
	cout << b << endl;

	system("pause");
	return 0;
}

引用做函数返回值

  作用:引用是可以作为函数的返回值存在的

  注意:不要返回局部变量引用

  用法:函数调用作为左值

示例:

int& test01(){
   
	int a = 10;
	return a;
}

int& test02(){
   
	static int a = 20;
	return a;
}

int main(){
   
	int& ref = test01();

	cout << ref << endl;
	cout << ref << endl;

	int& ref2 = test02();

	cout << ref2 << endl;
	cout << ref2 << endl;

	test02() = 1000;

	cout << ref2 << endl;
	cout << ref2 << endl;

	system("pause");
	return 0;
}

引用的本质

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

示例:

void func(int& ref){
   
	ref = 100;
}

int main(){
   
	int a = 10;

	int& ref = a;
	ref = 20;

	cout << a << endl;
	cout << ref << endl;

	func(a);
	
	system("pause");
	return 0;
}

常量引用

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

  在函数形参列表中,可以加const修饰形参,防止形参改变实参

示例:

void showValue(const int& v){
   
	//v += 10;
	cout << v << endl;
}

int main(){
   
	//引用本身需要一个合法的内存空间,因此运行错误
	//int& ref = 10;

	//加入const就不会报错
	const int& ref = 10;

	cout << ref << endl;

	int a = 10;
	showValue(a);

	system("pause");
	return 0;
}

函数提高

函数默认参数

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

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

示例:

int func(int a,int b = 10,int c = 10){
   
	return a + b + c;
}

//如果某个位置参数有默认值,那么从这个位置往后,从左向右,必须都要有默认值
//如果函数声明有默认值,函数实现的时候就不能有默认参数

int func2(int a = 10,int b = 10);
int func2(int a,int b){
   
	return a + b;
}

int main(){
   
	cout << func(20,20) << endl;
	cout << func(100) << endl;

	system("pause");
	return 0;
}

函数占位参数

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

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

  在现阶段函数的占位参数存在意义不大,但是后面会用到该技术

示例:

void func(int a,int){
   
	cout << "func" << endl;
}

int main(){
   
	//占位符必须填补
	func(10,10);

	system("pause");
	return 0;
}

函数重载

函数重载概述

  作用:函数名可以使用,提高复用性

  函数重载满足条件

  • 同一个作用于下
  • 函数名称相同
  • 函数参数类型不同 或者 个数不同 或者 顺序不同

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

示例:

void func(){
   
	cout << "func" << endl;
}
void func(int a){
   
	cout << "int a" << endl;
}

void func(double a){
   
	cout << "double a" << endl;
}

void func(int a,double b){
   
	cout << "double b" << endl;
}

void func(double a,int b){
   
	cout << "int b" << endl;
}

//函数返回值不可以作为函数重载条件
/*int func(double a,int b){
	cout << "double a" << endl;
}*/

int main(){
   
	func();
	func(10);
	func(3.14);
	func(10,3.14);
	func(3.14,10);

	system("pause");
	return 0;
}

函数重载注意事项

  • 引用作为重载条件
  • 函数重载碰到函数默认参数

示例:

void func(int &a){
   
	cout << "&a" << endl;
}

void func(const int &a){
   
	cout << "const" << endl;
}

void func2(int a,int b = 10){
   
	cout << "a,b = 10" << endl;
}

void func2(int a){
   
	cout << "a" << endl;
}

int main(){
   
	int a = 10;

	//调用无const
	func(a);

	//调用有const
	func(10);

	//碰到默认参数产生歧义,需要避免
	//func2(10);

	system("pause");
	return 0;
}

类和对象

  C++面向对象的三大特性为:封装、继承、多态

  C++认为万事万物皆为对象,对象上有其属性和行为

  例如

    人可以作为对象,属性有姓名、年龄等,行为有走、跑等

    车作为对象,属性有轮胎、方向盘等,行为有载人、放音乐等

    具有相同性质的对象,可以抽象称为

封装

封装的意义

  封装是C++面向对象三大特性之一

  封装的意义:

    将属性和行为作为一个整体,表现生活中的事物

    将属性和行为加以权限控制

  封装的意义一

    在设计类的时候,属性和行为写在一起,表现事物

  语法class 类名{访问权限:属性/行为};

示例:设计一个圆类,求圆的周长

const double pi = 3.14;

class Circle{
   
public:
	int m_r;

	double calculate(){
   
		return 2 * pi * m_r;
	}
};

int main(){
   
	Circle c1;
	c1.m_r = 10;

	cout << c1.calculate() << endl;

	system("pause");
	return 0;
}

  封装意义二

    类在设计时,可以把属性和行为放在不同的权限下,加以控制

  访问权限有三种:

    1. public 公共权限

    2. protected 保护权限

    3. private 私有权限

示例:

class Person{
   
public:
	string m_Name;

protected:
	string m_Car;

private:
	int m_Password;

public:
	void func(){
   
		m_Name = "张三";
		m_Car = "汽车";
		m_Password = 123456;
	}
};

int main(){
   
	Person p;

	p.m_Name = "李四";

	//在类外访问不到
	//p.m_Car = "aodi";

	//在类外访问不到
	//p1.m_Password = 123;

	p.func();

	system("pause");
	return 0;
}

struct 和 class 区别

  在 C++ 中 struct 和 class 唯一的区别就在于默认的访问权限不同

  区别:

  • struct 默认权限为公共
  • class 默认权限为私有

示例:

class C1{
   
	int m_A;
};

struct C2{
   
	int m_A;
};

int main(){
   
	C1 c1;
	c1.m_A = 10;

	C2 c2;
	c2.m_A = 10;

	system("pause");
	return 0;
}

成员属性设置为私有

  优点1:将所有成员属性设置为私有,可以自己控制读写权限

  优点2:对于写权限,可以检测数据的有效性

示例:

class Person{
   
public:
	void SetName(string name){
   
		m_Name = name;
	}
	
	string GetName(){
   
		return m_Name;
	}

	int GetAge(){
   
		return m_Age;
	}

	void SetAge(int age){
   
		m_Age = age;
	}

	void SetIdol(string idol){
   
		m_Idol = idol;
	}

private:
	string m_Name;
	int m_Age = 18;
	string m_Idol;
};

int main(){
   
	Person p;

	p.SetName("张三");
	cout << p.GetName
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值