【C++】【第一篇】【黑马 p84 - p105 】【引用】【重载】【类和对象-struct、class】
0 前言
在学习slam的过程中,越来越觉得C++的不可或缺,但是碍于自己的C++能力实在不堪入目,故近期打算系统跟着教程走一篇C++,如果之后有时间,打算把C++ primer plus这本书好好看一遍,之前看的云里雾绕,在黑马程序员教程的基础下,希望再学习该书能够顺利一些。
本次学习记录全程跟着黑马程序员匠心之作|C++教程从0到1入门编程,学习编程不再难。如有问题,请联系我!
基础部分我已经看完,为了之后的复习方便,故打算使用csdn进行记录,从p84视频开始~
第三阶段 C++核心编程
本阶段主要针对C++面向对象的编程技术
1 内存分区模型
1.1 程序运行前
1.2 程序运行后
1.3 new操作符(P88)
演示代码如下:
#include <iostream>
using namespace std;
//1、new的基本语法
int* func()
{
// 在堆区创建整型数据
// new返回的是 该数据类型的指针
int * p = new int(10);
double* P_d = new double(1.23);
return p;
}
void test01()
{
int* p = func();
cout << *p << endl;
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个元素,它会返回new的首地址,所以用地址接受
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 引用(p89)
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 引用注意事项(p90)
- 引用必须初始化
- 引用在初始化之后,不可以改变
实例如下:
#include <iostream>
using namespace std;
int main()
{
int a = 10;
int b = 20;
// 1、引用必须初始化
//int &c; //错误,引用必须初始化
int& c = a;// 一旦初始化后,就不可以更改,也就是不可以再是别的别名
// 2、引用在初始化后,不可以改变
c = b; // 这是赋值操作,不是更改引用
// 引用的本质就是 指针常量
cout << "a = " << a << endl;
cout << "b = " << b << endl;
cout << "c = " << c << endl;
system("pause");
return 0;
}
2.3 引用做函数参数(p91)
- 作用:函数传参时,可以利用引用的技术让形参修饰实参
- 优点:可以简化指针修改实参
实例如下:
#include <iostream>
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)//这里的a,b是实参a,b的别名,这里只是刚好名字起的一样
{
int temp = a;
a = b;
b = temp;
}
int main()
{
int a = 10;
int b = 20;
mySwap01(a, b);// 值传递,形参不会修饰实参
cout << "a = " << a << endl;
cout << "b = " << b << endl;
mySwap02(&a, &b);//地址传递,形参会修饰实参
cout << "a = " << a << endl;
cout << "b = " << b << endl;
mySwap03(a, b);//引用传递,形参也会修饰实参的
cout << "a = " << a << endl;
cout << "b = " << b << endl;
system("pause");
return 0;
}
- 总结:通过引用参数产生的效果同地址传递是一样的,引用的语法更清楚简单
2.4 引用做函数返回值(p92)
- 作用:引用是可以作为函数的返回值存在的
- 注意:不要返回局部变量引用
- 用法:函数调用作为左值
实例如下:
#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 << "ref = " << ref2 << endl;
cout << "ref = " << ref2 << endl;//均为10,正确
test02() = 1000;// 如果函数的返回值是引用,那么函数调用可以作为左值
cout << "ref = " << ref2 << endl;
cout << "ref = " << ref2 << endl;
system("pause");
return 0;
}
2.5 引用的本质(p93)
- 本质:引用的本质在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);
system("pause");
return 0;
}
- 结论:C++推荐用引用技术,因为语法方便,引用本质是指针常量,但是所有的指针操作编译器都帮我们做了
2.6 常量引用(p94)
- 作用:常量引用主要用来修饰形参,防止误操作
- 在函数形参列表中,可以加const修饰形参,防止形参改变实参
实例如下:
#include <iostream>
using namespace std;
//打印数据函数
void showValue(const int &val)
{
//val = 1000;
cout << 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之后变为只读,不可以修改
int a = 100;
showValue(a);
cout << "a = " << a << endl;
system("pause");
return 0;
}
3 函数提高(p95)
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)
//{
// return a + b + c;
//}
//2、如果函数声明有默认参数,函数实现就不能有默认参数
//声明和实现只能有一个有默认参数
//int func3(int a = 10, int b = 10);
//int func3(int a = 20, int b = 20)
//{
// return a + b;
//}
int main()
{
cout << func(10) << endl;;// 60
cout << func(10, 30) << endl;//70
//func3(10, 10); //会报错,
system("pause");
return 0;
}
3.2 函数占位参数(p96)
- C++中函数的形参列表里可以有占位参数,用来做占位,调用函数时必须填补该位置
- 语法:
返回值类型 函数名 (数据类型) {}
- 现阶段函数的占位参数存在意义不大,但是后面的课程中会用到该技术。
#include <iostream>
using namespace std;
// 占位参数
// 返回值类型 函数名(数据类型) {}
//目前阶段的占位参数还用不到,后面课程中会用到
//占位参数可以有默认参数
void func(int a, int = 10)
{
cout << "this is func" << a << endl;
}
int main()
{
func(10,10);
func(10);// 如果有默认参数,那么占位参数可以省略,否则不可以省略
system("pause");
return 0;
}
3.3 函数重载(p97)
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(int a, double b)
{
cout << "func(int a, int b)" << endl;
}
void func(double b, int a)
{
cout << "func(double b, int a)" << endl;
}
//注意事项
//函数的返回值不可以作为函数重载的条件
//int func(double b, int a)
//{
// cout << "func(double b, int a)" << endl;
//}
int main()
{
func();
func(10);
func(3.14);
func(3.14, 10);
system("pause");
return 0;
}
3.3.2 函数重载注意事项(p98)
- 引用作为重载条件
- 函数重载碰到函数默认参数
#include <iostream>
using namespace std;
//函数重载的注意事项
//1、引用作为重载的条件
void func(int &a) //int &a = 10;不合法
{
cout << "fun(int &a)" << endl;
}
void func(const int& a) //const int &a = 10;合法
{
cout << "fun(const int &a)" << endl;
}
//2、函数重载碰到默认参数
void func2(int a,int b=10)
{
cout << "func2(int a,int b)的调用" << endl;
}
void func2(int a)
{
cout << "func2(int a)的调用" << endl;
}
int main()
{
int a = 10;
func(a);//fun(int &a) a可读可写,调用的也是
func(10);//func(const int& a) 常量只读
//func2(10);//当函数重载碰到默认参数,出现二义性,报错,尽量避免这种情况
func2(10,20);
//函数重载尽量避免默认值
system("pause");//
return 0;
}
4 类和对象(p99)
- C++面向对象的三大特征为:封装、继承、多态
- C++认为万事万物都皆为对象,对象上有其属性和行为
例如:
- 人可以作为 对象,属性有姓名、年龄、体重…行为有走、跑、跳、吃饭、唱歌
- 车也可以作为对象
- 具有相同性质的对象,我们可以抽象称为类,人属于人类,车属于车类
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 calculateZC()
{
return 2 * PI * m_r;
}
};
int main()
{
// 通过圆类 创建具体的圆(对象)
// 实例化 (通过一个类创建一个对象的过程)
Circle cl;
//给圆对象 的属性进行赋值
cl.m_r = 10;
// 2*PI*10 = 62.8
cout << "圆的周长为: " << cl.calculateZC() << endl;
system("pause");//
return 0;
}
(P100)
实例二:设计一个学生类,属性有姓名和学号,可以给姓名和学号赋值,可以显示学生的姓名和学号
#include <iostream>
using namespace std;
#include <cstring>
class Student
{
public:
string name;
string ID;
void show_name()
{
cout << "该学生的姓名为:" << name << endl;
}
void show_ID()
{
cout << "该学生的学号为: " << ID << endl;
}
};
int main()
{
Student stu1;
stu1.name = "张三";
stu1.ID = "20200322";
stu1.show_name();
stu1.show_ID();
system("pause");//
return 0;
}
-
封装的意义二 (P101):
类在设计时,可以把属性和行为放在不同的权限下,加以控制 -
访问权限有三种:
- public 公共权限
- protected 保护权限
- private 私有权限
实例如下:
#include <iostream>
using namespace std;
#include <cstring>
//访问权限
//三种
// 公共权限 public 成员 类内可以访问 类外可以访问
// 保护权限 protected 成员 类内可以访问 类外不可以访问 子类可以访问父类的保护内容
// 私有权限 private 成员 类内可以访问 类外不可以访问 子类不可以访问父类的私有内容
class Preson
{
public:
// 公共权限
string m_Name; // 姓名
protected:
//保护权限
string m_Car; //汽车
private:
//私有权限
int m_Password; //银行卡密码
public:
void func()//虽然函数内有保护权限和私有权限,但是函数本身属于公共权限
{
m_Name = "张三";
m_Car = "拖拉机";
m_Password = 123456;
}
};
int main()
{
// 实例化具体对象
Preson p1;
p1.m_Name = "李四";
//p1.m_Car = "奔驰"; //保护权限的内容,在类外访问不到
//p1.m_Password = 123; // 私有权限内容,类外访问不到
p1.func();
system("pause");
return 0;
}
4.1.2 struct和class的区别
- 在C++中 struct和class唯一的区别就在于默认的访问权限不同
- 区别:
- struct:默认权限为公共
- class:默认权限为私有
#include <iostream>
using namespace std;
#include <cstring>
class C1
{
int m_A; // 默认权限 是私有
};
struct C2
{
int m_A; // 默认权限 是公有
};
int main()
{
//struct 和 class 的区别
// struct 默认权限是 公共 public
// class 默认权限是 私有 private
C1 c1;
//c1.m_A = 100; //不可以访问,默认私有,不可以访问
C2 c2;
c2.m_A = 100; //可以访问 ,在struct默认的权限是公共,因此可以访问
system("pause");
return 0;
}
4.1.3 成员属性设置为私有(p103)
- 优点1:将所有成员属性设置为私有,可以自己控制读写权限
- 优点2:对于写权限,我们可以检测数据的有效性
#include <iostream>
using namespace std;
#include <cstring>
// 成员属性设置为私有
// 1、可以自由控制读写权限
// 2、对于写可以检测数据的有效性
// 设计人类
class Preson
{
private:
//姓名 可读可写
string m_Name;
//年龄 只读
int m_Age;
//情人 只写
string m_Lover;
public:
//设置姓名
void setName(string name)
{
m_Name = name;
}
//获取姓名
string getName()
{
return m_Name;
}
//获取年龄 可读可写 如果想修改(年龄的范围必须是0~150之间)
int getAge()
{
m_Age = 0;
return m_Age;
}
//设置年龄
void setAge(int age)
{
if (age < 0 || age > 150)
{
m_Age = 0;
cout << "你这个老妖精!" << endl;
return;
}
m_Age = age;
}
// 设置情人 只写
void setLover(string lover)
{
m_Lover = lover;
}
};
int main()
{
Preson p;
p.setName("张三");
cout << "姓名为: " << p.getName() << endl;
cout << "年龄为: " << p.getAge() << endl;
p.setLover("苍井");
//cout << "情人为: " << p.m_Lover << endl; //不可以访问
system("pause");
return 0;
}
4.1.4 练习案例1:设计立方体类(p104)
- 设计立方体类(cube)
- 求出立方体的面积和体积
- 分别用全局函数和成员函数判断两个立方体是否相等
#include <iostream>
using namespace std;
#include <cstring>
//设计立方体类
//1、创建立方体类
class Cube
{
public:
//2、设计属性
//获取长
int getL() { return m_L; }
//设置长
void setL(int L) { m_L = L; }
//获取宽
int getW() { return m_W; }
//设置宽
void setW(int W) { m_W = W; }
//获取高
int getH() { return m_H; }
//设置高
void setH(int H) { m_H = H; }
//3、设计行为,获取立方体面积和体积
//获取表面积
double caculateS() { return 2 * m_L * m_W + 2 * m_L * m_H + 2 * m_W * m_H; }
//获取体积
double caculateV() { return m_L * m_W * m_H; }
//4、分别利用全局函数和成员函数 判断两个立方体是否相等
//利用成员函数判断两个立方体是否相等
bool isSameByClass(Cube & c)
{
if (getL() == c.getL() && getW() == c.getW() && getH() == 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.setH(10);
c1.setL(10);
c1.setW(10);
cout << "c1的面积为:" << c1.caculateS() << endl;
cout << "c1的体积为:" << c1.caculateV() << endl;
//创建第二个立方体
Cube c2;
c2.setH(10);
c2.setL(10);
c2.setW(10);
bool ret = isSame(c1, c2);
if (ret)
{
cout << "c1和c2是相等的!" << endl;
}
else
{
cout << "c1和c2是不等的!" << endl;
}
bool ret2 = c1.isSameByClass(c2);
if (ret2)
{
cout << "成员函数判断结果:c1和c2是相等的!" << endl;
}
else
{
cout << "成员函数判断结果:c1和c2是不等的!" << endl;
}
system("pause");
return 0;
}
4.1.5 练习案例2:点和圆的关系
- 设计一个圆形类(Circle),和一个点类(Point),计算点和圆的关系
#include <iostream>
using namespace std;
#include <cstring>
//点和圆的关系案例
//点类
class Point
{
public:
//设置X
void setX(int x) { m_X = x; }
//获取Y
int getX() { return m_X; }
//设置Y
void setY(int Y) { m_Y = 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 circle;
circle.setR(10);
Point center;
center.setX(10);
center.setY(0);
circle.setCenter(center);
Point p;
p.setX(10);
p.setY(10);
isInCircle(circle, p);
system("pause");
return 0;
}
- 显然,这样一个文件里面的代码量很大,所以可以把每个类拆分到各个头文件中,如下:
- 然后在头文件文件夹中创建一个头文件
point.h
,内容如下
#pragma once //防止头文件重复包含
#include <iostream>
using namespace std;
//点类
//一般在一个类设计中,头文件只需要一个声明就可以了,留下变量和函数的声明就可以了
class Point
{
public:
//设置X
void setX(int x);
//获取Y
int getX();
//设置Y
void setY(int Y);
//获取Y
int getY();
private:
int m_X;
int m_Y;
};
- 在源文件文件夹中创建一个文件
point.cpp
,内容如下
#include "point.h"
// setX(int x) { m_X = x; } //会报错,会提示不认识m_X,
//所以要加上Point::setX,告诉他这个是Point作用域下的变量
//设置X
void Point::setX(int x) { m_X = x; }
//获取Y
int Point::getX() { return m_X; }
//设置Y
void Point::setY(int Y) { m_Y = Y; }
//获取Y
int Point::getY() { return m_Y; }
- 然后在头文件文件夹中创建一个头文件
circle.h
,内容如下
#include "circle.h"
//设置半径
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;
}
- 在源文件文件夹中创建一个文件
circle.cpp
,内容如下
#pragma once
#include <iostream>
using namespace std;
#include "point.h"
class Circle
{
public:
//设置半径
void setR(int r);
//获取半径
int getR();
//设置圆心
void setCenter(Point& center);
//获取圆心
Point getCenter();
private:
int m_R;//半径
//在类中,可以让另一个作为本类中的成员
Point m_Center;//圆心
};
- 然后就可以把主文件里面的代码修改为:
#include <iostream>
using namespace std;
#include <cstring>
#include "point.h"
#include "circle.h"
//点和圆的关系案例
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 circle;
circle.setR(10);
Point center;
center.setX(10);
center.setY(0);
circle.setCenter(center);
Point p;
p.setX(10);
p.setY(10);
isInCircle(circle, p);
system("pause");
return 0;
}
- shift+tab 键可以 代码片缩进