C++核心编程
本阶段主要针对C++面向对象编程技术做详细讲解,探讨C++中的核心和精髓

1.内存分区模型
C++在程序执行时,将内存大方向划分为4个区域
1.代码区:存放函数的二进制代码,由操作系统进行管理
2.全局区:存放变量和静态变量以及常量
3.栈区:由编译器自动分配释放,存放函数的参数值,局部变量等
4.堆区:由程序员分配和释放,若程序员不释放,程序结束时由操作系统回收
内存四区的意义:
不同区域存放的数据,赋予不同的生命周期,给我们更大的灵活编程

1.1 程序运行前
在程序编译后,生成了exe可执行程序,未执行该程序前分为两个区域
代码区:
存放CPU执行的机器指令
代码区是共享的,共享的目的是对于频繁被执行的程序,只需要在内存中有一份代码即可
代码区是只读的,使其只读的原因是防止程序意外的修改了它的指令
全局区:
全局变量和静态变量存放在此
全局区还包含了常量区,字符串常量和其他常量也存放在此
该区域的数据在程序结束后由操作系统释放

#include
using namespace std;

//全局变量
int g_a = 10;
int g_b = 10;

//const修饰的全局变量
// c - const g - global l - local
const int c_g_a = 10;
const int c_g_b = 10;

int main() {
//创建普通局部变量
int a = 10;
int b = 10;

//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;
//cout << "全局变量g_b的地址为:" << &g_b << endl;
cout << "全局变量g_b的地址为:" << (int)&g_b << endl;//静态变量  在普通变量前面加static,属于静态变量
static int s_a = 10;
static int s_b = 10;cout << "静态变量s_a的地址为:" << (int)&s_a << endl;
cout << "静态变量s_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_b的地址为:" << (int)&c_g_b << endl;const int c_l_a = 10;
const int c_l_b = 10;cout << "局部常量 c_l_a的地址为:" << (int)&c_l_a << endl;
cout << "局部常量 c_l_b的地址为:" << (int)&c_l_b << endl;system("pause");return 0;

}
1.2 程序运行后

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

#include
using namespace std;

//栈区数据注意事项
//栈区的数据由编译器管理开辟和释放

int* func(int b) {
b = 100;//形参数据也会放在栈区
int a = 10;//局部变量 存放在栈区,栈区的数据在函数执行完后自动释放
return &a;//返回局部变量的地址
}

int main() {

//接受func函数的返回值
int* p = func(1);cout << *p << endl;   //第一次可以打印正确的数字是因为编译器做了保留
cout << *p << endl; //第二次数据不再保留system("pause");return 0;

}

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

#include
using namespace std;

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

int main() {

//在堆区开辟数据
int* p = func();cout << *p << endl;
cout << *p << endl;
cout << *p << endl;system("pause");return 0;

}

1.3 new运算符
c++中利用new操作符在堆区开辟数据
堆区开辟的数据, 由程序员手动开辟, 手动释放, 释放利用操作符delete
语法 : new数据类型
利用new创建的数据,会返回数据对应的类型的指针

#include
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;
//堆区的数据由程序员管理开辟释放
//如果想释放堆区的数据利用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;//给10个数赋值
}for (int i = 0;i < 10;i++) {cout << arr[i] << endl;
}
//释放堆区数组
//释放数组时要加一个[]
delete[]arr;

}

int main() {

test01();
test02();system("pause");return 0;

}

2.引用
2.1 引用的基本使用
作用:给变量起别名
语法:数据类型 &别名 = 原名

#include
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 引用注意事项
1.引用必须初始化
2.引用在初始化之后,不可以改变

#include
using namespace std;
int main() {

int a = 10;//1.引用必须初始化
//int& b;//错误必须初始化
int& b = a;
//2.引用在初始化之后,不可以改变
int c = 20;b = c;//赋值操作,不是更改应用cout << "a = " << a << endl;
cout << "b = " << b << endl;
cout << "c = " << c << endl;system("pause");return 0;

}

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

#include
using namespace std;

//交换函数
//1.值传递
void mySwap01(int a, int b) {
int temp = a;
a = b;
b = temp;
/cout << "swap01 a = " << a << endl;
cout << "swap01 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;system("pause");return 0;

}
总结:通过引用参数产生的效果同按地址传递是一样的.引用的语法更清楚简单

2.4 引用做函数的返回值
作用:引用是可以作为函数的返回值存在的
注意:不要返回局部变量引用
用法:函数调用作为左值

#include
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;//内存已经释放int& ref2 = test02();cout << "ref2 = " << ref2 << endl;test02() = 1000;//如果函数返回值是引用,这个函数调用可以作为左值cout << "ref2 = " << ref2 << endl;
cout << "ref2 = " << ref2 << endl;system("pause");return 0;

}

2.5 引用的本质
本质:引用的本质在C++内部实现是一个指针常量
#include
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);system("pause");return 0;

}
结论:C++推荐使用引用技术,因为语法很方便,引用本质是指针常量,但是所有的指针操作编译器都帮我们做了

2.6 常量引用
作用:常量引用主要用来修饰形参,防止误操作
在函数形参列表中,可以加const修饰形参,防止形参改变实参

#include
using namespace std;

//打印数据
void showValue(const int &val) {
//在函数体内无法再修改val(防止误操作)
//val=1000;//错误!
cout << "val = " << val << endl;
}

int main() {

//常量引用
//使用场景:用来修饰形参防止误操作
int a = 10;
//int& ref = 10;//错误,引用必须引用一块合法的内存空间
//加上const之后编译器将代码修改为int temp = 10;const int& ref=temp;
const int& ref = 10;
//ref = 20;//加入const变为只读,不可修改showValue(a);cout << "a = " << a << endl;system("pause");return 0;

}

三 函数提高
3.1 函数默认参数
在C++中,函数的形参列表中的形参是可以有默认值的
语法 : 返回值类型 函数名(参数 = 默认值) {}

#include
using namespace std;

//函数默认参数
//如果自己传入数据,就用自己的数据,如果没有,就用默认值
//语法:返回值类型 函数名 (形参=默认值){}

//注意事项:
//1.如果某个位置已经有了默认参数,那么这个位置往后从左到右必须有默认值
//int func(int a, int b = 20, int c ) {
// return a + b + c;
//}

//2.如果函数声明有默认函数,函数实现就不能有默认函数
//声明和实现只能有一个有默认参数
//int func2(int a, int b = 10, int c = 10);//错误

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

int main() {

cout << func2(10, 20, 30) << endl;system("pause");return 0;

}

3.2 函数占位参数
C++中的函数的形参列表里面可以有占位函数,用来做占位,调用函数时必须填补该位置
语法:返回值类型 函数名 (数据类型){}
在现阶段函数的占位存在意义不大,但是后面的课程中会用到该技术

#include
using namespace std;

//占位函数
//声明函数的返回值类型 函数名 (数据类型) {}
//目前阶段的占位函数,还用不到,后面会用到
//占位参数还可以有默认参数
void func(int a, int b = 10) {
cout << "this is func " << endl;
}

int main() {

func(10);system("pause");return 0;

}
3.3 函数重载
3.3.1 函数重载概述
作用:函数名可以相同,提高复用性
函数重载满足条件:
同一作用域下
函数名相同
函数参数类型不同 或者 个数不同 或者 顺序不同
注意:函数的返回值不可以作为函数重载的条件

#include
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(int a, double b) {
cout << "func (int a,double b)的调用 " << endl;
}

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

//注意事项
//函数的返回值不可以作为函数重载的条件
//int func(double a, int b) {
// cout << "func (double a,int b)的调用 " << endl;
//}

int main() {

func();
func(10);
func(3.14);
func(10, 3.14);
func(3.14, 10);system("pause");return 0;

}

3.2.2 函数重载注意事项
引用作为重载条件
函数重载碰到函数默认参数
#include
using namespace std;

//函数重载的注意事项
//1.引用作为重载的条件
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;
}

//2.函数重载遇到默认参数
void func2(int a,int b = 10) {
cout << “func(int a)的调用” << endl;
}

void func2(int a) {
cout << “func(int a)的调用” << endl;
}

int main() {

int a = 10;
func(a);func(10);//func2(10);//函数重载碰到默认参数,出现二义性,报错,尽量避免这种情况system("pause");return 0;

}

4 类和对象
C++面向对象的三大特征:封装,继承,多态
C++认为万事万物皆为对象,对象上有其属性和行为

例如:1.人可以作为对象,属性有姓名,年龄,身高,体重…行为有走,跑,跳,吃饭,唱歌…
2.车也可以作为对象,属性有轮胎,方向盘,车灯…行为有载人,放音乐,放空调
3.具有相同性质的对象,我们可以抽象地称为类,人属于人类,车属于车类

4.1 封装
4.1.1 封装的意义
封装是C++面向对象三大特征之一
封装的意义:
1.将属性和行为作为一个整体,表现生活中的事物
2.将属性和行为加以权限控制
封装的意义一:
在设计类的时候,属性和行为写在一起,表现事物
语法:class 类名 {访问权限 : 属性 / 行为 };
#include
using namespace std;

//圆周率
const double PI = 3.14;

//设计一个圆类求圆的周长
//圆求周长的公式: 2 * PI * 半径

//class代表设计一个类,类后面紧跟着的就是类名称
class Circle
{
//访问权限
//公共权限
public:
//属性
//半径
int m_r;
//行为
//获取圆的周长
double calculateZC() {
return 2 * PI * m_r;
}
};

int main() {

//通过圆类创建具体的圆(对象)
//实例化 (通过一个类创建一个对象的过程)
Circle c1;
//给圆对象的属性进行赋值
c1.m_r = 10;
// 2 * PI * 10 = 62.8;
cout << "圆的周长为:" << c1.calculateZC() << endl;system("pause");return 0;

}

设计一个学生类,属性有姓名和学号
可以给姓名和学号赋值,可以显示学生的姓名和学号

#include
#include
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;
}
void setId(int id) {m_Id = id;
}

};

int main() {

//创建一个具体学生 实例化对象
Student s1;
Student s2;
//给s1对象进行属性赋值操作
s1.setName("张三");//s1.m_Name = "张三";
s1.setId(1);//s1.m_Id = 1;s2.m_Name = "李四";
s2.m_Id = 2;//显示学生信息s1.showStudent();
s2.showStudent();system("pause");return 0;

}

封装意义二:
类在设计时,可以把属性和行为放在不同的权限下,加以控制
访问权限有三种:
1.public 公共权限
2.protected 保护权限
3.private 私有权限

#include
#include
using namespace std;

//访问权限

//公共权限 public 成员类内可以访问,类外可以访问
//保护权限 protected成员类内可以访问类外不可以访问 儿子可以访问父亲中的保护内容
//私有权限 private 成员类内可以访问类外不可以访问 儿子不可以访问父亲的私有内容

class Person {
public:
//公共权限
string m_Name;
protected:
//保护权限
string m_Car;
private:
//私有权限
int m_Password;
private:
void func() {
m_Name = “张三”;
m_Car = “拖拉机”;
m_Password = 123456;
}
};

int main() {

//实例化具体对象
Person p1;p1.m_Name = "李四";
//p1.m_Car = "奔驰";//保护权限内容在类外访问不到
//p1.m_Password = 123;//私有权限内容类外访问不到system("pause");return 0;

}

4.1.2 struct和class区别
在C++struct和class唯一的区别在于默认的访问权限不同
区别:
1.struct默认权限为公共
2.class 默认权限为私有

#include
using namespace std;

class C1 {
int m_A;//私有权限
};

struct C2 {
int m_A;//公共权限
};

int main() {

//struct 和 class 区别
C1 c1;
//c1.m_A = 100;//私有成员不可访问C2 c2;
c2.m_A = 100;//公共成员可以访问system("pause");return 0;

}

4.1.3 成员属性设置为私有
优点1:将所有成员属性设置为私有,可以自己控制读写权限
优点2:对于写权限,我们可以检测数据的有效性

#include
#include
using namespace std;

//成员属性设置为私有
//1.可以自己控制读写权限
//2.对于写可以检测数据的有效性

//设计人类
class Person {
public:
//设置姓名
void setName(string name) {
m_Name = name;
}
//获取姓名
string getName() {
return m_Name;
}
//设置年龄
void setAge(int age) {
if (age < 0 || age>150) {
m_Age = 0;
cout << “你这个老妖精!” << endl;
return;
}
m_Age = age;
}
//获取年龄
int getAge() {
return m_Age;
}
//设置情人
void setLover(string lover) {
m_Lover = lover;
}
private:
//姓名 可读可写
string m_Name;
//年龄 只读
int m_Age;
//情人 只写
string m_Lover;
};

int main() {

Person p;p.setName("张三");p.setAge(18);p.setLover("永女士");cout << "姓名为:" << p.getName() << endl;cout << "年龄为:" << p.getAge() << endl;//cout << "情人为:" << p.getLoevr() << endl;//不可以访问system("pause");return 0;

}

//立方体类设计
//1.创建立方体类
//2.设计属性
//3.设计行为获取立方体面积和体积
//4.判断两个立方体是否相等

#include
using namespace std;

class Cube {
public:
//设置长宽高
void setL(int l) {
m_L = l;
}
void setW(int w) {
m_W = w;
}
void setH(int h) {
m_H = h;
}
//获取长宽高
int getL() {
return m_L;
}int getW() {
return m_W;
}int getH() {
return m_H;
}
//获取立方体面积
int calculateS() {
return 2 * m_L * m_W + 2 * m_L * m_H + 2 * m_W * m_H;
}
//获取立方体体积
int calculateV() {
return m_L * m_W * m_H;
}
//利用成员函数判断两个函数是否相等
bool isSameByClass(Cube &c) {
if (m_L == c.getL() && m_W == c.getW() && m_H == c.getH()) {
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.getW() == c2.getW() && c1.getH() == c2.getH()) {
return true;
}

return false;

}

int main() {

//创建立方体对象
Cube c1;
c1.setL(10);
c1.setW(10);
c1.setH(10);cout << "c1的面积为:" << c1.calculateS() << endl << "c1的体积为:" << c1.calculateV() << endl;//创建第二个立方体
Cube c2;
c2.setL(10);
c2.setW(10);
c2.setH(10);cout << "c2的面积为:" << c2.calculateS() << endl<< "c2的体积为:" << c2.calculateV() << endl;//利用全局函数判断
cout << "利用全局函数判断:" << endl;
bool ret = isSame(c1, c2);
if (ret) {cout << "c1和c2是相等的" << endl;
}
else
{cout << "c1和c2是不相等的" << endl;
}//利用成员函数判断
cout << "利用成员函数判断:" << endl;
ret = c1.isSameByClass(c2);
if (ret) {cout << "c1和c2是相等的" << endl;
}
else
{cout << "c1和c2是不相等的" << endl;
}system("pause");return 0;

}

判断点和圆的位置:

头文件中添加新建项
//如圆类 circle.h

#pragma once//防止头文件重复包含
#include
using namespace std;
#include"point.h"//引用在外的Point函数

//只做函数头的声明

class Circle {
public:
//设置半径
void setR(int r);
//获取半径
int getR();
//设置圆心
void setCenter(Point center);
//获取圆心
Point getCenter() ;
private:
int m_R;//半径
//在类中可以让另一个类作为本类中的成员
Point m_Center;//圆心
};

在源文件中添加新建项
//如circle.cpp
#include<circle.h>

//需要加入作用域: Circle::

//设置半径
void Circle::setR(int r) {
m_R = r;
}
//获取半径
int Circle::getR() {
return m_R;
}
//设置圆心
void Circle::setCenter(Point center) {
m_Center = center;
}
//获取圆心
Point Circle::getCenter() {
return m_Center;
}

全代码:
#include
using namespace std;

//点和圆关系案例
//点类
class Point {
public:
//设置x,y
void setX(int x) {
m_X = x;
}
void setY(int y) {
m_Y = y;
}
//获取x,y
int getX() {
return m_X;
}
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;

}

4.2 对象的初始化和清理
生活中我们买的电子产品都基本会有出厂设置,在某一天我们不用时候也会删除一些自己信息数据以保证安全
C++中的面向对象来源于生活,每个对象也都会有初始设置以及对象销毁前的清理数据的设置

4.2.1 构造函数和析构函数
对象的初始化和清理也是两个非常重要的安全问题
一个对象或者变量没有初始状态,对其使用后果是未知
同样的使用完一个对象或变量,没有及时清理,也会造成一定的安全问题
C++利用了构造函数和析构函数解决上述问题,这两个函数将会被编译器自动调用,完成对象的初始化和清理工作
对象的初始化和清理工作是编译器强制要我们做的事情,因此我们不提供构造和析构,编译器会提供
编译器提供的构造函数和析构函数是空实现
构造函数:主要作用在于创建对象时为对象的成员属性赋值,构造函数由编译器自动调用,无需手动调用
析构函数:主要作用在于对象销毁前系统自动调用,执行一些清理工作

#include
using namespace std;

//对象的初始化和清理
//1.构造函数 进行初始化
class Person {
//1.构造函数
//没有返回值 不用写void
//函数名 与类名相同
//构造函数可以有参数,可以发生重载
//创建对象的时候,构造函数会自动调用,而且只调用一次
public:
Person() {
cout << "Person 构造函数的调用 " << endl;
}

//2.析构函数 进行清理
//没有返回值 不写void
//函数名和类名相同 在名称前加~
//析构函数不可以有参数,不可以发生重载
//对象在销毁前 会自动调用析构函数,而且只会调用一次
~Person() {cout << "Person 析构函数的调用 " << endl;
}

};

//构造和析构都是必须有的实现,如果我们自己不提供,编译器会提供一个空实现的构造和析构
void test01(){
Person p;//在栈上的数据,test01执行完毕后释放这个对象
}

int main() {

//test01();
Person p;system("pause");return 0;

}

4.2.2 构造函数的分类及调用
两种分类方式
按参数分为:有参构造和无参构造
按类型分为:普通构造和拷贝构造
三种调用方法:
括号法
显示法
隐式转换法

#include
using namespace std;

//构造函数的分类及调用
//分类
// 按照参数分类: 无参构造(默认构造)和有参构造
// 按照类型分类: 普通构造和拷贝构造
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() {
1.括号法
//cout << “括号法:” << endl;
//Person p1;//默认构造函数调用
//Person p2(10);//有参构造函数
//Person p3(p2);//拷贝构造函数

注意事项
调用默认构造函数的时候不要加()
Person p1();//错误,编译器认为是函数声明而不是创建对象//cout << "p2的年龄为:" << p2.age << endl;
//cout << "p3的年龄为:" << p3.age << endl;//2.显示法
Person p1;
Person p2 = Person(10);//有参构造
Person p3 = Person(p2);//拷贝构造Person(10);//匿名对象
//特点:当前行执行结束后,系统立即回收匿名对象
cout << "aaaa" << endl;//注意事项2
//不要利用拷贝构造函数初始化匿名对象 编译器会认为Person (p3) == Person p3;
//Person(p3);//错误//3.隐式转换法
Person p4 = 10;//相当于Person p4 = Person(10); 有参构造
Person p5 = p4;//拷贝构造

}

int main() {

test01();system("pause");return 0;

}

4.2.3 拷贝构造函数调用函数
C++中拷贝构造函数调用时机通常有三种情况
使用一个已经创建完毕的对象来初始化一个新对象
值传递的方式给函数参数传值
以值方式返回局部对象

#include
using namespace std;

class Person {
public:
Person() {
cout << “Person默认构造函数调用” << endl;
}
Person(int age) {
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§;
}
//3.值方式返回局部对象
Person doWork2() {
Person p1;
cout << (int*)&p1 << endl;
return p1;
}
void test03() {
Person p = doWork2();
cout << (int*)&p << endl;
}

int main() {

cout << "test01:" << endl;
test01();cout << "test02:" << endl;
test02();cout << "test03:" << endl;
test03();system("pause");return 0;

}

4.2.4 构造函数调用规则
默认情况下,C++编译器至少给一个类添加三个函数
1.默认构造函数(无参,函数体为空)
2.默认析构函数(无参,函数为空)
3.默认拷贝函数,对属性进行值拷贝

构造函数调用规则如下:
如果用户定义有参构造函数,C++不在提供默认无参构造,但会提供默认拷贝构造
如果用户定义拷贝函数构造函数,C++不会再提供其他构造函数

#include
using namespace std;

//构造函数的调用规则
//1.创建一个类,C++编译器会给每个类都添加至少3个函数
//默认构造 (空实现)
//析构函数 (空实现)
//拷贝构造 (值拷贝)

//2.如果我们写了有参构造函数,编译器就不再提供默认构造,依然提供拷贝构造
//如果我们写了拷贝构造函数,编译器就不再提供其他普通构造函数了
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§;
//
// cout << “p2的年龄为:” << p2.m_Age << endl;
//}
void test02() {
Person p(18);

Person p2(p);cout << "p2的年龄为:" << p2.m_Age << endl;

}

int main() {

//test01();test02();system("pause");return 0;

}

4.2.5 深拷贝与浅拷贝
深浅拷贝是面试经典问题,也是常见的坑

浅拷贝:简单的赋值拷贝操作
深拷贝:在堆区重新申请空间,进行拷贝操作

#include
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);
cout << "p2的年龄为:" << p2.m_Age << " 身高为:" << *p2.m_Height << endl;

}

int main() {

test01();system("pause");return 0;

}
总结:如果属性有在堆区开辟的,一定要自己提供拷贝构造函数,防止浅拷贝带来的问题

4.2.6 初始化列表
作用:C++提供了初始化列表语法,用来初始化属性
语法 : 构造函数() : 属性1(值1), 属性2(值2)…{}

#include
using namespace std;

//初始化列表
class Person
{
public:

//Person(int a, int b, int c) {
//  m_A = a;
//  m_B = b;
//  m_C = c;
//}传统初始化操作//int m_A;
//int m_B;
//int m_C;//初始化列表初始化属性
/*Person() :m_A(10), m_B(20), m_C(30) {}*/
Person(int a, int b, int c) :m_A(a), m_B(b), m_C(c) {}int m_A;
int m_B;
int m_C;

};

void test01() {
//Person p(10, 20, 30);
//Person p;
Person p(30, 20, 10);
cout << "m_A = " << p.m_A << endl;
cout << "m_B = " << p.m_B << endl;
cout << "m_C = " << p.m_C << endl;
}

int main(){

test01();system("pause");return 0;

}

4.2.7 类对象作为类成员

C++类中的成员是另一个类的对象,我们称该成员为成员对象

B类中有对象A作为成员,A为对象成员
那么当创建B对象时,A与B的构造和析构的顺序

#include
using namespace std;

//类对象作为类成员

//手机
class Phone {
public:
Phone(string pName) {
cout << “Phone的构造函数调用” << endl;
m_PName = pName;
}
~Phone(){
cout << “Phone的析构函数调用” << endl;
}
string m_PName;
};

//人类
class Person
{
public:
Person(string name,string pName):m_Name(name),m_Phone(pName){
//Phone m_Phone= pName; 隐式转换法
cout << “Person的构造函数调用” << endl;
}
~Person() {
cout << “Person的析构函数调用” << endl;
}

string m_Name;
Phone m_Phone;

};

//当其他类对象作为本类成员,构造时候先构造类对象,再构造自身
//析构的顺序与构造相反

void test01() {
Person p(“张三”, “苹果MAX”);

cout << p.m_Name << " 拿着: " << p.m_Phone.m_PName << endl;

}

int main() {

test01();system("pause");return 0;

}

4.2.8 静态成员
静态成员就是成员变量和成员函数前加上关键词static,称为静态成员
静态成员分为:
静态成员变量:
所有对象共享同一份数据
在编译阶段分配内存
类内声明,类外初始化
静态成员函数:
所有对象共享一个函数
静态成员函数只能访问静态成员变量

#include
using namespace std;

//静态成员函数
//所有对象共享同一个函数
//静态成员函数只能访问静态成员变量
class Person {
public:
//静态成员函数
static void func() {
m_A = 100;//静态成员函数可以访问 静态成员变量
//m_B = 200;//静态成员函数 不可以访问 非静态成员变量 无法区分是哪个对象的m_B属性
cout << “static void func调用” << endl;
}

static int m_A;//静态成员变量
int m_B;//非静态成员

private:
static void func2() {
cout << “static void func2调用” << endl;
}
};

int Person::m_A = 0;

//两种访问方式
void test01() {
//1.通过对象访问
Person p;
p.func();

//2通过类名访问
Person::func();
//Person::func2();类外访问不到私有静态成员函数

}

int main() {

test01();system("pause");return 0;

}

4.3 C++对象模型和this指针

4.3.1 成员变量和成员函数分开存储
在C++中,类内的成员变量和成员函数分开存储
只有非静态成员变量才属于类的对象上

#include
using namespace std;

// 成员变量 和 成员函数 是分开存储的

class Person {
int m_A;//非静态成员变量 属于类的对象上的

static int m_B;//静态成员变量 不属于类的对象上void func() {}//非静态成员函数

};

int Person::m_B = 0;

void test01() {
Person p;
//空对象占用内存空间为: 0 4 1
//C++编译器会给每个空对象分配一个字节空间为了区分空对象占内存的位置
//每个空对象也应该有一个独一无二的内存地址
cout << "size of p = " << sizeof§ << endl;
}

void test02() {
Person p;
cout << "size of p = " << sizeof§ << endl;
}

int main() {

//test01();
test02();system("pause");return 0;

}

4.3.2 this指针概念
通过4.3.1知道C++中成员变量和成员函数是分开储存的
每一个非静态成员函数只会诞生一份函数实例,也就是说多个同类型的对象会共用一块代码
那么问题是:这一块代码是如何区分哪个对象调用自己的?

C++通过提供特殊的指针 this 解决上述问题,this 指针指向被调用的成员函数所属的对象

this 指针是隐含每一个非静态成员函数内的一种指针
this 指针不需要定义,直接使用即可

this 指针的用途:
当形参和成员变量同名时,可用this指针来区分
在类的非静态成员函数中返回对象本身,可使用return* this

#include
using namespace std;

class Person {
public:
Person(int age) {
//this 指针指向被调用的成员函数所属的对象
this->age = age;
}

Person& PersonAddAge(Person& p) {this->age += p.age;//this指向p2的指针,而*this指向的就是p2这个对象的本体return *this;
}int age;

};

//1.解决名称冲突
void test01() {
Person p1(18);
cout << “p1的年龄为:” << p1.age << endl;
}
//2.返回对象本身用 * this
void test02() {
Person p1(10);

Person p2(10);//链式编程思想
p2.PersonAddAge(p1).PersonAddAge(p1).PersonAddAge(p1);cout << "p2的年龄为:" << p2.age << endl;

}

int main() {

test01();test02();system("pause");return 0;

}

4.3.3 空指针访问成员函数

C++中空指针也是可以调用成员函数的,但是也要注意有没有用到this指针

如果用到this指针,需要加以判断保证代码的健壮性

#include
using namespace std;

//空指针调用成员函数
class Person {
public:
void showClassName() {
cout << “this is Person class” << endl;
}
void showPersonAge(int age) {
/if (this == NULL) {
return;
}
/ //防止程序崩溃

 //报错原因是因为传入的指针是空指针cout << "age = " << this->m_Age << endl;
}
int m_Age;

};

void test01() {
Person* p = NULL;

p->showClassName();//p->showPersonAge(18);

}

int main() {

test01();system("pause");return 0;

}

4.3.4 const修饰成员函数

常函数:
成员函数后加const后我们称这个函数为常函数
常函数内不可以修改成员属性
成员属性声明时加关键字mutable后,在常函数中依然可以修改

常对象:声明对象前加const称该对象为常对象
常对象只能调用常函数

#include
using namespace std;

//常函数
class Person {
public:
//this指针的本质是 指针常量 指针的指向是不可以修改的
//const Person * const this;
//在成员函数后面加const,修饰的是this指向,让指针指向的值也不可以修改
void showPerson() const// const -->指针指向值也不可以修改
{
this->m_B;
//this->m_A = 100; 指针指向值不可以修改
//this = NULL;this不可以修改指针指向
}
void func() {

}
int m_A;
mutable int m_B;//特殊变量,即使在常函数中,也可以修改这个值,加关键字mutable

};

void test01() {
Person p;
p.showPerson();
}

//常对象

void test02() {
const Person p;//在对象前加const,变为常对象
//p.m_A = 100; 不可修改
p.m_B = 100;//特殊变量m_B在常对象下也可以修改

//常对象只能调用常函数
p.showPerson();
//p.func(); 常对象不可以调用普通成员函数,因为普通成员函数可以修改属性

}

int main() {

test01();test02();system("pause");return 0;

}

4.4 友元

生活中你的家有客厅(public),有你的卧室(private)
客厅所有来的客人都可以进去,但是你的卧室是私有,也就是说只有你能进去,但是,可以允许好友进去

在程序里,有些私有属性也想让类外特殊的一些函数或者类进行访问,就需要用到友元的技术

友元的目的就是让一个函数或者类访问另一个类中的私有成员

友元的关键字是friend

友元的三种实现
全局函数做友元
类做友元
成员函数做友元

4.4.1 全局函数做友元
#include
#include
using namespace std;

class Building {
//goodGay全局函数是 Building的好朋友,可以访问Building中私有成员
friend void goodGay(Building* building);
public:
Building() {
m_SittingRoom = “客厅”;
m_BedRoom = “卧室”;
}
public:
string m_SittingRoom;
private:
string m_BedRoom;
};

//全局函数
void goodGay(Building* building) {
cout << "goodGay全局函数 正在访问: " << building->m_SittingRoom << endl;

cout << "goodGay全局函数 正在访问: " << building->m_BedRoom << endl;

}

void test01() {
Building building;
goodGay(&building);
}

int main() {

test01();system("pause");return 0;

}

#include
#include
using namespace std;

//类做友元
class Building;
class GoodGay {
public:

GoodGay();void visit();//参观函数 访问Building中的属性Building* building;

};
class Building {
//GoodGay类是本类的好朋友,可以访问本类中私有成员
friend class GoodGay;

public:
Building();
public:
string m_SittingRoom;
private:
string m_BedRoom;
};

//类外写成员函数
Building::Building() {
m_SittingRoom = “客厅”;
m_BedRoom = “卧室”;
}

GoodGay::GoodGay() {
//创建一个建筑物对象
building = new Building;
}

void GoodGay::visit() {
cout << “GoodGay正在访问:” << building->m_SittingRoom << endl;
cout << “GoodGay正在访问:” << building->m_BedRoom << endl;

}

void test01() {
GoodGay gg;
gg.visit();
}

int main() {

test01();system("pause");return 0;

}
#include
using namespace std;

class Building;
class GoodGay {
public:
GoodGay();

void visit();//让visit函数可以访问Building中私有成员
void visit2();//让visit函数不可以访问Building中私有成员Building* building;

};

class Building {
//告诉编译器 GoodGay类下的visit成员函数作为本类的好朋友,可以访问私有成员

friend void GoodGay::visit();
friend void GoodGay::visit2();

public:
Building();
public:
string m_SittingRoom;

private:
string m_BedRoom;
};

//类外实现成员函数
Building::Building() {
m_SittingRoom = “客厅”;
m_BedRoom = “卧室”;
}

GoodGay::GoodGay() {
building = new Building;
}

void GoodGay::visit() {
cout << “visit函数正在访问:” << building->m_SittingRoom << endl;
cout << “visit函数正在访问:” << building->m_BedRoom << endl;
}

void GoodGay::visit2() {
cout << “visit函数正在访问:” << building->m_SittingRoom << endl;
cout << “visit函数正在访问:” << building->m_BedRoom << endl;
}

void test01() {
GoodGay gg;
gg.visit();
gg.visit2();
}

int main() {

test01();system("pause");return 0;

}

4.5 运算符重载

运算符重载概念:对已有的运算符重新定义,赋予其另一种功能,以适应不同的数据类型

4.5.1 加号运算符

作用:实现两个自定义数据类型相加的运算

#include
using namespace std;

//加号运算符重载
class Person {
public:

//1.成员函数重载 加号
/*Person operator+(Person& p) {Person temp;temp.m_A = this->m_A + p.m_A;temp.m_B = this->m_B + p.m_B;return temp;
}*/int m_A;
int m_B;

};

//2.全局函数重载 加号
Person operator+(Person& p1, Person& p2) {
Person temp;
temp.m_A = p1.m_A + p2.m_A;
temp.m_B = p1.m_B + p2.m_B;
return temp;
}

//函数重载的版本
Person operator+(Person& p1, int num) {
Person temp;
temp.m_A = p1.m_A + num;
temp.m_B = p1.m_B + num;
return temp;
}

void test01() {
Person p1;
p1.m_A = 10;
p1.m_B = 10;

Person p2;
p2.m_A = 10;
p2.m_B = 10;//成员函数重载本质调用
//Person p3 = p1.operator+(p2);//全局函数重载本质调用
Person p3 = operator+(p1, p2);//Person p3 = p1 + p2;//运算符重载,也可以发生函数重载
Person p4 = p1 + 10;//person+int cout << "p4.m_A = " << p3.m_A << endl;
cout << "p4.m_B = " << p3.m_B << endl;

}

int main() {

test01();system("pause");return 0;

}
总结1:对于内置的数据类型的表达式的运算符是不可能更改的
总结2:不要滥用运算符重载

4.5.2 左移运算符重载

#include
using namespace std;

//左移运算符重载
class Person {

friend ostream& operator<<(ostream& cout, Person& p);

public:
Person(int a, int b) {
m_A = a;
m_B = b;
}
private:
//利用成员函数重载左移运算符
//不会利用成员函数重载<<运算符,因为无法实现 cout 在左侧
/void operator<<(cout) { }/
int m_A;
int m_B;
};

//只能利用全局函数重载左移运算符
ostream& operator<<(ostream& cout,Person& p) {
//本质 operator<<(cout , p) 简化 cout << p
//换行操作
//cout << "m_A = " << p.m_A << " m_B = " << p.m_B << endl;
cout << "m_A = " << p.m_A << " m_B = " << p.m_B;
return cout;
}

void test01() {
Person p(10,10);

//p.m_A = 10;
//p.m_B = 10;cout << p << endl;

}

int main() {

test01();system("pause");return 0;

}

4.5.3 递增运算符重载

作用:通过重载递增运算符,实现自己的整型数据

#include
using namespace std;

//递增运算符重载
//自定义整型
class MyIntger {
friend ostream& operator<<(ostream& cout, MyIntger myint);
public:
MyIntger() {
m_Num = 0;
}

//重载前置++运算符 返回引用为了一个一直对一个数据进行递增操作
MyIntger& operator++() {//先进行++运算m_Num++;//再将自身做返回return *this;
}
//重载后置++运算符
//int 代表占位参数,可以用于区分前置和后置递增
MyIntger operator++(int) {//先 记录当时结果MyIntger temp = *this;//后 递增m_Num++;//最后将记录结果做返回return temp;
}

private:
int m_Num;
};

//重载左移运算符
ostream& operator<<(ostream& cout, MyIntger myint) {
cout << myint.m_Num;
return cout;
}

void test01() {
MyIntger myint;

//返回引用两者相同,均为2   返回值则myint为1
cout << ++(++myint) << endl;
cout << myint << endl;

}

void test02() {
MyIntger myint;

cout << myint++ << endl;
cout << myint << endl;

}

int main() {

//test01();test02();//int a = 0;
//cout << ++(++a) << endl;
//cout << a << endl;system("pause");return 0;

}
总结:前置递增返回引用,后置递增返回值

c艹入门->入土 ( 二 )相关推荐

  1. 【SSRS】入门篇(二) -- 建立数据源

    原文:[SSRS]入门篇(二) -- 建立数据源 通过 [SSRS]入门篇(一) -- 创建SSRS项目 这篇,我们建立了一个SSRS项目: 接下来,我们以 AdventureWorks2012 示例 ...

  2. Node.js核心入门(二)

    目录: Node.js核心入门(一) 全局对象 常用工具 事件机制 Node.js核心入门(二) 文件系统访问 HTTP服务器与客户端 文件系统 fs fs 模块是文件操作的封装,它提供了文件的读取. ...

  3. SpringBoot入门(二)——起步依赖

    本文来自网易云社区 在前一篇我们通过简单几步操作就生成了一个可以直接运行的Web程序,这是因为SpringBoot代替我们做了许多工作,概括来讲可以分为起步依赖和自动配置.这一篇先来看看起步依赖. 项 ...

  4. Oracle数据库基础入门《二》Oracle内存结构

    Oracle数据库基础入门<二>Oracle内存结构 Oracle 的内存由系统全局区(System Global Area,简称 SGA)和程序全局区(Program Global Ar ...

  5. SpringMVC入门(二)—— 参数的传递、Controller方法返回值、json数据交互、异常处理、图片上传、拦截器

    SpringMVC入门(二)-- 参数的传递.Controller方法返回值.json数据交互.异常处理.图片上传.拦截器 参考文章: (1)SpringMVC入门(二)-- 参数的传递.Contro ...

  6. 运动控制器编程_快速入门 | 篇二十一:运动控制器ZHMI组态编程简介一

    点击上方"正运动小助手",随时关注新动态! 运动控制器ZHMI组态编程简介一  今天我们来学习一下,运动控制器的ZHMI组态编程简介.本文主要从产品概述.控制器连接触摸屏使用.HM ...

  7. 互联网协议入门(二)【转】

    原文地址:点击前往 上一篇文章分析了互联网的总体构思,从下至上,每一层协议的设计思想. 这是从设计者的角度看问题,今天我想切换到用户的角度,看看用户是如何从上至下,与这些协议互动的. ======== ...

  8. arcgis api for flex 开发入门(二)map 的创建

    arcgis api for flex 开发入门(二)map 的创建 在flex 中创建一个esri 的map ,你只需要使用<esri:Map>标签就可以轻松完成. 在<esri: ...

  9. UWP入门(二) -- 基础笔记

    UWP入门(二) -- 基础笔记 原文:UWP入门(二) -- 基础笔记 不错的UWP入门视频,1092417123,欢迎交流 UWP-04 - What i XMAL? XAML - XML Syn ...

  10. Bootstrap入门(二十一)组件15:警告框

    Bootstrap入门(二十一)组件15:警告框 通过这些简单.灵活的进度条,为当前工作流程或动作提供实时反馈. 进度条组件使用了 CSS3 的 transition 和 animation 属性来完 ...

最新文章

  1. 如何使用Jenkins持续集成C#网站项目
  2. java jdbc 工具_实现JDBC的工具类
  3. python构造一个二叉树_如何用python构造一个n层的完全二叉树
  4. ScriptManager控件声明的各个部分
  5. “面试不败计划”:面试题基础二
  6. 聊聊买卖股票的最佳时机
  7. c#endread怎么打印出来_c# – Socket.EndRead 0字节意味着断开连接?
  8. 菜鸟学习笔记:Java基础篇6(数组、字符串)
  9. “有 些 事 当 了 程 序 员 才 懂”
  10. jaxp与dom4j遍历xml树
  11. url存在宽字节跨站漏洞_【XSS漏洞】XSS漏洞相关总结v1.0
  12. Java:反射和注解从入门到放弃
  13. 2018-01-17
  14. 一种简单的排列组合方法实现(C语言)
  15. 构建LVS+Keepalived高可用群集
  16. dnf修改服务器时间限制,DNF历史性革新,团本刷新时间改为周六,为黑鸦让路
  17. A Belief Propagation Algorithm for Multipath-Based SLAM IEEE TWC2019阅读
  18. UE4 解除帧率限制
  19. 解决Office桌面图标异常
  20. 对excel的导出,使用jxt

热门文章

  1. 怎么将DWG文件有效转换为PDF文件
  2. 操作系统——进程管理的功能
  3. 华为5.0系统如何不用ROOT激活XPOSED框架的步骤
  4. 12.31 icpc 南京站
  5. opencv答题卡识别
  6. echarts 力导向关系图
  7. 路由器的两个端口接在同一个交换机上_2个路由器怎么连接?
  8. Unity Shader 一 激光特效Shader
  9. HTTP Error 500.0 - ANCM In-Process Handler Load Failure
  10. 百度网盘/U盘,上传文件时提示超过4G限制如何解决