内存分区模型
1.1程序运行前
在程序编译后,生成了exe可执行文件,未执行该程序前分了两个区域
代码区 :存放函数体的二进制代码,由操作系统进行管理。
全局区:该区域的数据在程序结束后由操作系统释放。
#define _CRT_SECURE_NO_WARNINGS 1
#include<iostream>
using namespace std;
int g_a = 0;
int g_b = 0;
const int c_g_a = 0;
const int c_g_b = 0;
int main()
{
//局部变量
int a = 0;
int b = 0;
cout << "局部变量a的地址为:" << (int) & a << endl;
cout << "局部变量b的地址为:" <<(int) & b << endl;
//全局变量
cout << "全局变量g_a的地址为:" << (int) & g_a << endl;
cout << "全局变量g_b的地址为:" << (int) & g_b << endl;
//静态变量
static int s_a = 0;
static int s_b = 0;
cout << "静态变量s_a的地址为:" << (int) & s_a << endl;
cout << "静态变量s_b的地址为:" << (int) & s_b << endl;
//全局常量
cout << "全局常量c_g_a的地址为:" << (int) & c_g_a << endl;
cout << "全局常量c_g_b的地址为:" << (int)& c_g_b << endl;
//字符串常量
cout << "字符串常量的地址为:" << (int)&"hello world" << endl;
//局部常量
const int c_l_a = 10;//c- const g-global l - local
const int c_l_b = 10;
cout << "局部常量 c_l_a的地址为:" << (int)&c_l_a << endl;
cout << "局部常量 c_l_b的地址为:" << (int)&c_l_b << endl;
}
总结:
c++中在程序运行前分为全局区和代码区
代码区的特点是共享和只读
全局区中存放全局变量,静态变量,常量
常量区存放const修饰的全局常量和字符串常量
1.2程序运行后
栈区:由编译器自动分配释放,存放函数的参数值,局部变量等。
注意事项:不要返回局部变量的地址,栈区开辟的数据由编译器自动释放
#include<iostream>
using namespace std;
//栈区数据注意事项 --- 不要返回局部变量的地址
//栈区的数据由编译器管理开辟和释放
int* func(int b)//形参数据也放在栈区
{
int b = 100;
int a = 10;//局部变量,存放在栈区,栈区的数据在函数执行完后自动释放
return &a;//返回局部变量的地址
}
int main()
{
int* p = func(1);
cout << *p << endl;//VS2022一直保存 其他可能只保留一次 甚至不保留
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.3new操作符
释放操作符delete
#include<iostream>
using namespace std;
//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;
}
//2.在堆区利用new开辟数组
void test02()
{
//创建10个整型数据的数组在堆区
int*arr=new int[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();
test02();
//2.
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;
return 0;
}
2.2引用的注意事项
引用必须初始化
引用在初始化后,不可以改变
#include<iostream>
using namespace std;
int main()
{
int a = 10;
//1.引用必须初始化
int& b=a;//错误,必须初始化
//2.引用一旦初始化后就不可以再改变
int c = 20;
b = c;//赋值操作 不是更改引用
cout << "a=" << a << endl;//20
cout << "b=" << b << endl;//20
cout << "c=" << c << endl;//20
return 0;
}
2.3引用做函数参数
作用:函数传参时,可以利用引用的技术让形参修饰实参
优点:可以简化指针修改实参
#include<iostream>
using namespace std;
//交换函数
//1.值传递
void mySwap01(int a, int b)
{
int temp = a;
a = b;
b = temp;
//cout << "a=" << a << endl;
//cout << "b=" << b << endl;
}
//2.地址传递
void mySwap02(int*a,int *b)//
{
int temp = *a;
*a = *b;
*b = temp;
}
//3.引用传递
void mySwap03(int& a, int& b)
{
int temp = a;
a = b;
b = temp;
}
int main()
{
int a = 10;
int b = 20;
//mySwap01(a,b);//值传递 形参不会修饰实参
//mySwap02(&a, &b);//地址传递,形参会修饰实参
mySwap03(a, b);//引用传递,形参会修饰实参的
cout << " a=" << a << endl;
cout << " b=" << b << endl;
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 &ret = test01();
//cout << "ret=" << ret << endl;//x86 和 x64结果不同
//VS2022默认使用x64系统运行 会保留多次 改成x86的话系统只保留一次
int& ref2 = test02();
cout << "ref2=" << ref2 << endl;
cout << "ref2=" << ref2 << endl;
test02() = 1000;//如果函数的返回值是引用,这个函数调用可以作为左值
cout << "ref2=" << ref2 << endl;
cout << "ref2=" << ref2 << endl;//全都是10 但是加了1000那一条后 以下的就变为了1000
system("pause");
return 0;
}
2.5引用的本质
本质:引用的本质再c++内部实现是一个指针常量
#include<iostream>
using namespace std;
//发现是引用 转换为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);
return 0;
}
结论:c++推荐引用计数,因为语法方便,引用本质是指针常量,但是所有的指针操作编译器帮我们做了
2.6常量引用
作用:常量引用主要用来修饰形参,防止误操作
再函数形参列表中,可以加const修饰形参,防止形参改变实参
#include<iostream>
using namespace std;
//打印数据函数
void showValue(const int& val)
{
//val = 1000;不可以修改
cout << "val=" << val << endl;
}
int main()
{
//常量引用
//使用场景,用来修饰形参。防止误操作
//int a = 10;
加上const之后,编译器将代码修改,int temp =10;const int &ref=temp;
//const int& ref = 10;//引用必须引一块合法的内存空间。但是加了const就不会有警报了
//ref = 20;//加入了const 之后变为只读 不可以修改
int a = 100;
showValue(a);
cout << "a=" << a << endl;
system("pause");
return 0;
}
3.函数提高
3.1函数默认参数
c++中,函数的形参列表中的形参是可以有默认值的。
语法:返回值类型 函数名 (参数=默认值){}
#include<iostream>
using namespace std;
//函数默认参数
//如果我们自己传入了数据 就用我们自己的数据,如果没有,就用默认值
int func(int a, int b=20, int c=30)
{
return a + b + c;
}
//注意事项
//1.如果某个位置已经有了默认参数,那么从这个位置往后,从左到右都必须有默认值
//int func2(int a, int b = 10, int c, int d)
//{
// return a + b + c + d;
//}
//2.如果函数声明有默认参数,函数实现就不能有默认参数
声明和实现只能有一个有默认参数
int func2(int a=10, int b=10);
int func2(int a, int b)
{
return a + b;
}
int main()
{
//cout<<func(10, 20, 30)<<endl;
//cout << func(10) << endl;
//cout << func(10,30) << endl;//70
cout << func2(69,20) << endl;
system("pause");
return 0;
}
3.2函数占位参数
c++中函数的形参列表里可以有占位参数,用来做占位,调用函数时必须填补该位置
语法:返回值类型 函数名 (数据类型){}
#include<iostream>
using namespace std;
//占位参数
//返回值类型 函数名(数据类型){}
//目前阶段的占位参数,我们还用不到 但是后面会用到 目前只是铺垫
void func(int a=10,int = 10)
{
cout << "this is func" << endl;
}
int main()
{
func(10,10);
system("pause");
return 0;
}
3.3函数重载
3.3.1函数重载概述
作用:函数名可以使用,提高复用性
函数重载满足条件:同一个作用域下
函数名称相同
函数参数类型不同,或者个数不同,或者顺序不同
注意:函数的返回值不可以作为函数重载的条件
#include<iostream>
using namespace std;
//函数重载
// “多音字”
//可以让函数名相同 提高复用性
void func()
{
cout << "func的调用" << endl;
}
void func(int a)
{
cout << "func(int a)调用!" << endl;
}
void func(double a)
{
cout << "func(double)的调用" << endl;
}
int main()
{
//func();
//func(10);
//func(3.14);
//设置两个参数 但是参数顺序不同依然可以
system("pause");
return 0;
}
//注意事项:函数的返回值不可以作为函数重载的条件
//为什么?因为编译器不知道调用那个
函数重载的注意事项:
引用作为重载的条件
函数重载碰到函数默认参数
#include<iostream>
using namespace std;
//函数重载的注意事项
//1.引用作为重载的条件
void fun(int& a)//int &a=10;不合法
{
cout << "fun(int &a)调用" << endl;
}
void fun(const int& a)//const int&a=10; 引用的本质
{
cout << "fun(const int &a)调用" << endl;
}
//2.函数重载碰到默认参数
void func2(int a)
{
cout << "func2(int a)的调用" << endl;
}
void func2(int a,int b=10)
{
cout << "func2(int a,int b)的调用" << endl;
}
int main()
{
//int a = 10;
//fun(a);//调用第一个
//fun(10);//调用第二个
//func2(10); //可以调用两个函数 编译器会出现二一性
system("pause");
return 0;
}
4.类和对象
c++面向对象的三大特性为:封装,继承,多态
c++认为万事万物皆为对象,对象上有其属性和行为
例如:
人可以作为对象,他or她有姓名 身高
车,
具有相同性质的对象,我们可以抽象为类,人属于人类,车属于车类
4.1封装
4.1.1封装的意义
封装是c++面向对象三大特性之一
封装的意义:
将属性和行为作为一个整体,表现生活中的事物
将属性和行为加以权限控制
封装的意义一:
在设计类的时候,属性和行为写在一起,表现事物
语法:class类名{访问权限:属性/行为};
示例:设计一个圆类,求圆的周长
#include<iostream>
using namespace std;
const double PI = 3.14;
//设计一个圆类,求圆的周长
//圆求周长公式=2*PI*半径
//class 代表一个类,类后面紧跟着的就类名称
class Circle
{
//访问权限
//公共权限
public:
//属性
//半径
int m_r;
//行为
//获取圆的周长
double caculateZC()
{
return 2 * PI * m_r;
}
};
int main()
{
//通过圆类 创建具体的圆(对象)
//实例化(通过一个类 创建一个对象的过程)
Circle cl;
//给圆对象 的属性进行赋值
cl.m_r = 10;
cout << "圆的周长" << cl.caculateZC() << endl;
system("pause");
return 0;
}
设计一个学生类,属性有姓名和学号,可以给学号和姓名赋值,可以显示学生的姓名和学号
#include<iostream>
#include<string>
using namespace std;
//设计学生类
class student
{
public://公共权限
//属性
//类中的属性和行为 我们统一称为成员
//属性 成员属性 成员变量
//行为 成员函数 成员方法
string m_name;
int m_id;
//行为
//显示姓名和学号
void showstudent()
{
cout << "姓名:" << m_name << " 学号: " << m_id << endl;
}
//给姓名赋值
void setname(string name)
{
m_name = name;
}
};
int main()
{
//创建一个具体的学生 实例化学生
student s1;
//给学生s1进行赋值操作
s1.m_name = "张三";
s1.m_id = 1;
s1.setname("张三");
//显示学生信息
s1.showstudent();
student s2;
s2.m_name = "李四";
s2.m_id = 2;
s2.showstudent();
system("pause");
return 0;
}
封装意义二:
类在设计时,可以把属性和行为放在不同的权限下,加以控制
访问权限有三种:
1.public 公共权限
2.protected 保护权限
3.private 私有权限
#include<iostream>
using namespace std;
//访问权限
//三种
//公共权限 public 成员类内可以访问 类外可以访问
//保护权限 protected 成员类内可以访问 类外不可以访问 儿子可以访问父亲中的保护内容
//私有权限 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 p1;
p1.m_Name = "李四";
//p1.m_Car = "奔驰";//保护权限内容,在类外访问不到
//p1.m_Password = 123;//私有权限的内容,类外访问不到
return 0;
}
struct和class的区别
在c++中struct和class唯一的区别就在于默认的访问权限不同
区别:
struct 默认权限为公共
class默认权限为私有
#include<iostream>
using namespace std;
class C1
{
int m_A;//默认 是私有
};
struct C2
{
int m_A;//默认 是公共
};
int main()
{
//struct 和 class的区别
//struct默认权限是 公共public
C2 c2;
c2.m_A = 100;
//class默认权限为 私有private
C1 c1;
//c1.m_A = 100;
system("pause");
return 0;
}
成员属性设置为私有
优点一:将所有成员属性设置为私有,可以自己控制读写权限
优点二:对于写权限,我们可以检测数据的有效性
#include<iostream>
using namespace std;
#include<string>
class person
{
public:
//设置姓名
void setname(string name)
{
m_name = name;
}
//获取姓名 只读
string getname()
{
return m_name;
}
//获取年龄
int getAge()
{
return m_age;
}
//只写
void setlover(string lover)
{
m_lover = lover;
}
void setAge(int age)
{
if (age < 0||age>150)
{
cout << "年龄有误" << endl;
return;
}
m_age = age;
}
private:
//可读可写
string m_name;
//只读
int m_age;
//只写
string m_lover;
};
int main()
{
person p;
p.setname("张三");
p.setAge(20);
cout << "姓名为: " << p.getname() << endl;
cout << "年龄为: " << p.getAge() << endl;
p.setlover("y");
//cout << "情人为: " << p. << endl;
}
案例一:设计立方体类
Cube 求出立方体的面积和体积
分别用全局函数和成员函数判断两个立方体是否相等
#include<iostream>
using namespace std;
//立方体类的案例
//创建立方体的类
//属性 行为
//分别利用全局函数和成员函数判断两个立方体是否相等
class Cube
{
public:
//设置长
void setL(int l)
{
m_L = l;
}
//获取长
int getL()
{
return m_L;
}
//设置宽
void setW(int w)
{
m_W = w;
}
//获取宽
int getW()
{
return m_W;
}
//设置高
void setH(int h)
{
m_H = h;
}
//获取高
int getH()
{
return m_H;
}
//获取立方体面积
int calculateS()
{
return 2 * m_L * m_W + 2 * m_W * m_H + 2 * m_L * m_H;
}
//获取立方体体积
int calculateV()
{
return m_L * m_H * m_W;
}
//成员函数
bool issamebyclass(Cube&c)
{
if (m_L== c.getL() && m_H == c.getH() && m_W == c.getW())
{
return true;
}
return false;
}
private:
int m_L;//长
int m_W;//宽
int m_H;//高
};
//利用全局函数判断,两个立方体是否相等
bool isSame(Cube c1,Cube c2)
{
if (c1.getL() == c2.getL() && c1.getH() == c2.getH() && c1.getW() == c2.getW())
{
return true;
}
return false;
}
int main()
{
Cube c1;
c1.setL(10);
c1.setH(10);
c1.setW(10);
cout << "c1的体积为: " << c1.calculateV() << endl;
cout << "c1的面积为: " << c1.calculateS() << endl;
//创建第二个立方图
Cube c2;
c2.setL(10);
c2.setH(10);
c2.setW(10);
bool ret = isSame(c1, c2);
if (ret)
{
cout << "c1等于c2" << endl;
}
else
{
cout << "c1和c2不相等" << endl;
}
ret = c1.issamebyclass(c2);
if (ret)
{
cout << "c1等于c2" << endl;
}
else
{
cout << "c1和c2不相等" << endl;
}
system("pause");
return 0;
}
点和圆的关系
设计一个圆形类(Circle ),和一个点类(Point),计算点和圆的关系
#include<iostream>
using namespace std;
//点和圆关系案例
// 点类
class Point
{
public:
void setX(int x)
{
m_X = x;
}
int getX()
{
return m_X;
}
void setY(int y)
{
m_Y = y;
}
int getY()
{
return m_Y;
}
private:
int m_X;
int m_Y;
};
//圆类
class Circle
{
public:
void setR(int r)
{
m_R = r;
}
int getR()
{
return m_R;
}
void setCenter(Point center)
{
m_Center = center;
}
Point getCenter()
{
return m_Center;
}
private:
int m_R;//半径
Point m_Center;//圆心
};
void isInCircle(Circle& c, Point& p)
{
//计算两点之间距离的平方
int distance =
(c.getCenter().getX() - p.getX()) * (c.getCenter().getX() - p.getX()) +
(c.getCenter().getY() - p.getY()) * (c.getCenter().getY() - p.getY());
//计算半径
int rDistance = c.getR() * c.getR();
if (distance == rDistance)
{
cout << "点在圆上" << endl;
}
else if (distance > rDistance)
{
cout << "点在圆外" << endl;
}
else
{
cout << "点在圆内" << endl;
}
}
int main()
{
//创建圆
Circle c;
c.setR(10);
Point center;
center.setX(10);
center.setY(0);
c.setCenter(center);
//创建点
Point p;
p.setX(10);
p.setY(10);
isInCircle(c, p);
system("pause");
return 0;
}
对象的初始化和清理
生活中我们买的电子产品都基本会有出厂设置,在某一天我们不要时候也会删除一些自己信息数据保证安全
c++中面对对象来源生活,每个对象也都会有初始化设置以及对象销毁前的清理数据的设置
构造函数和析构函数
对象的初始化和清理也是两个非常重要的安全问题
一个对象或者变量没有初始状态,对其使用后果未知
同样的使用完一个对象或者一个变量,没有及时清理,也会造成一定的安全问题
c++中利用构造函数和析构函数解决上述问题,这两个函数将会被编译器自动调用,完成对象初始化和清理工作。对象的初始化和清理工作是编译器强制我们做的事情,因此如果我们不提供构造和析构,编译器会提供编译器提供的构造函数和析构函数是空实现。
构造函数:主要作用于创建对象时为对象的成员属性赋值,构造函数由编译器自动调用,无须手动调用。
析构函数:主要作用于对象销毁前系统自动调用,执行一些清理工作。
构造函数语法:类名(){}
1.构造函数,没有返回值也不写void
2.函数名称和类名相同
3.构造函数可以有参数,因此可以发生重载
4.程序在调用对象的时候会自动调用构造,无需手动调用,而且只会调用一次
析构函数语法:~类名(){}
1.析构函数,没有返回值也不写void
2.函数名称与类名相同,在名称前加上符号~
3.构造函数不可以有参数,因此不可以发生重载
4.程序在对象销毁前会自动调用析构,无需手动调用,而且只会调用一次
#include<iostream>
using namespace std;
//对象的初始化和清理
//1.构造函数 进行初始化操作
class Person
{
public:
//1.1构造函数
//没有返回值 也没有void
//函数名 与类名相同
//构造函数可以有参数,可以发生重载
//创建对象的时候,构造函数会自动调用,而且只调用一次
Person()
{
cout << "Person构造函数的调用" << endl;
}
//2.析构函数 进行清理的操作
//没有返回值 不写void
//~加类名
//不可以有参数 不可以发生重载
//销毁前 会自动调用析构函数 而且智慧调用一次
~Person()
{
cout << "Person析构函数的调用" << endl;
}
};
//构造和析构都是必须有的实现,如果我们不提供给
//编译器会提供一个空析构和构造
void test01()
{
Person p;//栈区的数据 test01执行完毕后就会释放这个对象
}
int main()
{
test01();
Person p;//没有被释放 整个程序结束之后才会可能调用析构
system("pause");
return 0;
构造函数的分类及调用
两种分类方式:
按参数分为:有参构造和无参构造
按类型分为:普通构造和拷贝构造
三种调用方式:
括号法
显示法
隐式转换法
#include<iostream>
using namespace std;
//1.构造函数的分类和调用
//分类
//按照参数分类 无参构造(默认) 有参构造
//按照类型分类 普通构造 拷贝构造
class Person
{
public:
//构造函数
Person()
{
cout << "Person的无参构造函数调用" << endl;
}
Person(int a)
{
age = a;
cout << "Person的有参构造函数调用" << endl;
}
//拷贝构造函数
Person(const Person &p)
{
//将传入的人身上的所有属性,拷贝到我身上
cout << "Person的拷贝构造函数调用" << endl;
age = p.age;
}
~Person()
{
cout << "Person的析构函数调用" << endl;
}
int age;
};
//调用
void test01()
{
//括号法
//Person p1;//默认构造函数的调用
//Person p2(10);//有参构造函数
//Person p3(p2);//拷贝构造函数的调用
//cout << "p2的年龄为: " << p2.age << endl;
//cout << "p3的年龄为: " << p3.age << endl;
//注意事项1
//调用默认构造函数时候 不要加()
//因为下面这行代码,编译器会认为是一个函数的声明,不会认为是在创建对象
//void func();
/*Person p1();*/
//显示法
//Person p1;
//Person p2 = Person(10);//有参构造
//Person p3 = Person(p2);//拷贝构造
//Person(10);//匿名对象 特点:当前行执行结束后,系统会立即回收掉匿名对象
//cout << "aaaaa" << endl;
//注意事项2:
//不要利用拷贝构造函数,来初始化匿名对象 编译器会认为 Person(p3)==Person p3;认为重定义
//Person(p3);
//隐式转换法
Person p4 = 10;//相当于写了 Person p4=Person(10); 有参构造
Person p5 = p4;//拷贝构造
}
int main()
{
test01();
system("pause");
return 0;
}
拷贝构造函数调用时机
C++中拷贝构造函数调用时机通常有三种情况
使用一个已经创建完毕的对象来初始化一个新对象
值传递的方式给函数擦参数传值
以值方式返回局部对象
#include<iostream>
using namespace std;
//拷贝构造函数的调用时机
class Person
{
public:
Person()
{
cout << "Person默认构造函数调用" << endl;
}
Person(int age)
{
cout << "Person有参构造函数调用" << endl;
m_Age = age;
}
Person(const Person& p)
{
cout << "Person拷贝构造函数调用" << endl;
m_Age = p.m_Age;
}
~Person()
{
cout << "Person析构函数调用" << endl;
}
int m_Age;
};
//1、使用一个已经创建完毕的对象来初始化一个新对象
void test01()
{
Person p1(20);
Person p2(p1);
cout << "P2的年龄" << p2.m_Age << endl;
}
//2、值传递的方式给函数参数传值
void doWork(Person p)
{
}
void test02()
{
Person p;
doWork(p);
}
//3、值方式返回局部对象
Person doWork2()
{
Person p1;
cout << (int*)&p1 << endl;
return p1;
}
void test03()
{
Person p = doWork2();
cout << (int*)&p << endl;
}
int main()
{
//test01();
//test02();
test03();
system("pause");
return 0;
}
构造函数调用规则
默认情况下,c++编译器至少给一个类添加3个函数
1.默认构造函数(无参,函数体为空)
2.默认析构函数(无参,函数体为空)
3.默认拷贝构造函数,对属性进行值拷贝
构造函数规则如下:
如果用户定义有参构造函数,c++不再提供默认无参构造函数,但是会默认提供默认拷贝构造
如果用户定义拷贝构造函数,c++不会再提供其他构造函数
#include<iostream>
using namespace std;
//构造函数的调用规则
//1.创建一个类,c++编译器会给每个类添加至少3个函数
//默认构造(空实现)
//析构函数(空实现)
//拷贝构造(值拷贝)
//如果我们写了有参构造函数,编译器就不再提供默认构造,依然提供拷贝构造
//如果我们写了拷贝构造函数 编译器其他都不提供
class Person
{
public:
/*person()
{
cout << "person默认构造函数调用" << endl;
}*/
/*person(int age)
{
cout << "person有参构造函数调用" << endl;
m_age = age;
}*/
Person(const Person& p)
{
cout << "Person拷贝构造函数调用" << endl;
m_Age = p.m_Age;
}
~Person()
{
cout << "Person析构函数调用" << endl;
}
int m_Age;
};
void test01()
{
Person p;
p.m_Age = 18;
Person p2(p);
cout << "p2的年龄为: " << p2.m_Age << endl;
}
void test02()
{
//Person p(28);
//Person p2(p);
cout << "p2的年龄为: " << p2.m_Age << endl;
}
int main()
{
//test01();
test02();
system("pause");
return 0;
}
深拷贝和浅拷贝
深浅拷贝是面试经典问题,也是常见的一个坑
浅拷贝:简单的赋值拷贝操作
深拷贝:在堆区重新申请空间,进行拷贝操作
#include<iostream>
using namespace std;
//深拷贝和浅拷贝
class person
{
public:
person()
{
cout << "person默认构造函数调用" << endl;
}
person(int age,int height)
{
m_age = age;
m_height=new int(height);
cout << "person有参构造函数调用" << endl;
}
//自己实现拷贝构造函数 解决浅拷贝带来的问题
person(const person& p)
{
cout << "person拷贝构造函数调用" << endl;
m_age = p.m_age;
//m_height = p.m_height; 编译器默认实现就是这行代码
//深拷贝操作
m_height = new int(*p.m_height);
}
~person()
{
//析构代码 将堆区开辟数据做释放操作
if (m_height != null)
{
delete m_height;
m_height = null;
}
cout << "person析构函数调用" << endl;
}
int m_age;
int* m_height;//为什么要用指针 因为要堆区上开辟
};
void test01()
{
person p1(18,160);
cout << "p1的年龄为: " << p1.m_age << "身高: "<<*p1.m_height<<endl;
person p2(p1);//p2先释放
cout << "p1的年龄为: " << p2.m_age << endl;
cout << "p1的年龄为: " << p1.m_age << "身高: " <<* p1.m_height << endl;
}
int main()
{
test01();
system("pause");
return 0;
}