一、内存的分区模型
1.程序运行前
1.1全局区:
#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() {
//全局区 全局变量 静态变量 常量(字符串常量)
cout << "全局变量g_a的地址为" << (int)&g_a << 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;
//常量 字符串常量 const常量
cout << "字符串常量 的地址为:" << (int)& "hello world" << endl;
//常量 const修饰的全局常量和局部常量
//const全部变量
cout << "const全局常量c_g_a的地址为" << (int)&c_g_a << endl;
cout << "const全局常量c_g_b的地址为" << (int)&c_g_b << endl;
//const修饰的局部常量
const int c_l_a = 10;
const int c_l_b = 10;
cout << "const局部常量c_l_a的地址为" << (int)&c_l_a << endl;
cout << "const局部常量c_l_b的地址为" << (int)&c_l_b << endl;
//创建普通的局部变量
int a = 10;
int b = 10;
cout << "局部变量a的地址为" << (int)&a << endl;
cout << "局部变量b的地址为" << (int)&b << endl;
system("pause");
return 0;
}
2.程序运行后
栈区:
#include<iostream>
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;
3.new操作符
#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;
//cout << *p << endl;//内存已经被释放,再次访问就是非法操作,会报错
}
void test02() {
//创建10整型数据的数组,在堆区
int* arr = new int[10];//10代表数组中有10个元素
for (int i = 0; i < 10; i++) {
arr[i] = i + 100;//给10个元素赋值 100~109
}
for (int i = 0; i < 10; i++) {
cout << arr[i] << endl;
}
//释放堆区数组
//释放数组的时候,要加[]才可以
delete[] arr;
}
//2.在堆区利用new开辟数组
int main() {
test01();
test02();
system("pause");
return 0;
}
二、引用
1.引用的基本使用
2.引用的注意事项
(1)引用必须初始化
(2)引用在初始化后不可以修改
#include<iostream>
using namespace std;
int main() {
//1.引用 必须初始化
//int& b = a;//这是错误代码,必须要初始化
int a = 10;
int& b = a;
//引用初始化后不可更改
int c = 20;
//int& b = c; 已经是a的别名就不能再是b的别名了。
b = c;//这是赋值操作,不是引用
system("pause");
return 0;
}
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;
cout << *a << endl;
cout <<* b << endl;
}
//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);
//cout << a << endl;
//cout << b << endl;
//地址传递会改变实参
mySwap02(&a, &b);
//引用传递形参也会改变实参
mySwap03(a, b);
system("pause");
return 0;
}
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 << endl;//第一次结果正确,是因为编译器做了保留
cout << ref << endl;//第二次结果错误,是因为a的内存已经释放。
int& ref2 = test02();
cout << ref2 << endl;
test02() = 1000;//返回值是a a=1000;ref2就是a的别名
cout << ref2 << endl;
cout << ref2 << endl;
cout << ref2 << endl;
system("pause");
return 0;
}
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;
system("pause");
return 0;
}
6.常量引用
作用:常量引用主要用来修饰形参,防止误操作
在函数 形参列表中,可以加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 << endl;
system("pause");
return 0;
}
三、函数提高
1.函数的默认参数
#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,int c=10,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) << endl;;
system("pause");
return 0;
}
2.函数的占位参数
描述:C++中函数的列表里可以有占位参数,用来做占位,调用函数时必须填补该位置
语法: 返回值类型 函数名(数据类型){
}
#include<iostream>
using namespace std;
//占位参数
//返回值类型 函数名(数据类型) { }
//占位参数 ,还可以有默认参数
void func(int a,int) {
cout << "tiis is a func" << endl;
}
int main() {
func(10, 10);
system("pause");
return 0;
}
3.函数重载
作用:函数名可以相同,提高复用性
3.1函数重载的概述
满足条件:
1.同一作用域下
2.函数名相同
3.函数参数类型不同或者个数不同或者顺序不同
注意:函数的返回值不可以作为函数重载的条件
#include<iostream>
using namespace std;
//函数重载
//作用:函数名可以相同,提高复用性
//满足条件:
//1.同一作用域下
//2.函数名相同
//3.函数参数类型不同或者个数不同或者顺序不同
void func() {
cout << "tiis is a func" << endl;
}
void func(int a) {
cout << "tiis is a func!" << endl;
}
int main() {
system("pause");
return 0;
}
3.2函数重载的注意事项
引用作为重载的条件
函数重载碰到函数默认参数
#include<iostream>
using namespace std;
//1.引用作为重载的条件
void func(int &a) {//int &a=10;这是不合法的
cout << "1" << endl;
}
void func(const int& a) {//const int &a=10; 这是合法的
cout << "2" << endl;
}
//函数重载碰到函数默认参数
void func2(int a,int b=10) {
cout << "1" << endl;
}
void func2(int a ) {
cout << "1" << endl;
}
int main() {
int a = 10;
func(a);//调用的是第一个函数
func(10);//调用的是第二个函数
func2(10);//当函数重载碰到默认参数,出现二义性,报错,尽量避免这种情况
func2(10, 20);//这是正常调用
system("pause");
return 0;
}
四、类和对象
1.封装
1.1封装的意义
#include<iostream>
using namespace std;
//圆周率 常量
const double PI = 3.14;
//设计一个圆类,求圆的周长
//圆求周长的公式2Πr
//class代表设计一个类,类后面紧跟着的就是类的名称
class Circle {
//访问权限
public:
//属性通常是变量
//半径
int m_r;
//行为 通常为函数
//获取圆的周长
double calculateZC() {
//返回圆的周长 2PIr
return 2 * PI * m_r;
}
};
int main() {
//通过圆类创造一个具体的圆(对象 )
//对象的创建 类名 对象名
Circle c1;
//给圆对象的属性赋值
c1.m_r = 10;
cout << "圆的周长为" << c1.calculateZC() << endl;
system("pause");
return 0;
}
案例:设计学生类,
#include<iostream>
using namespace std;
#include<string>
//设计一个学生类,属性有姓名和学号
//可以给姓名和学号赋值,可以显示学生的姓名和学号
//类中的属性和行为统称为成员
//属性 也叫成员属性 也叫成员变量
//行为 也叫成员函数 成员方法
class Student {
public:
//属性
string m_name;//姓名
int m_id;//学号
//行为
//显示姓名和学号
void showStudent() {
cout << "姓名:" << m_name << "学号:" << m_id << endl;
}
//给姓名赋值
void setName(string name) {
m_name = name;
}
//给id赋值
void setId(int id) {
m_id = id;
}
};
int main() {
//创建一个具体学生 实例化对象
Student s1;
s1.m_name = "张三";
s1.m_id = 1;
s1.showStudent();
//通过行为给属性赋值
s1.setName("张三");
s1.setId(1);
system("pause");
return 0;
}
#include<iostream>
using namespace std;
#include<string>
//成员属性 和成员函数都有权限。
//访问权限 三种
//公共权限 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 s1;
s1.m_Name = "李四";
s1.m_Car = "奔驰";//保护权限属性在类外不可以访问
s1.m_Password = 1234;//私有权限在类外也不可以访问
system("pause");
return 0;
}
1.2struct和class的区别
#include<iostream>
using namespace std;
#include<string>
//struct 和class区别
//struct 默认权限是公共 public
//class 默认权限是私有 private
struct c1 {
int m_c1;
};
class c2 {
int m_c2;
};
int main() {
//struct 和class区别
//struct 默认权限是公共 public
//class 默认权限是私有 private
c1 C1;
C1.m_c1 = 10;//struct 默认权限是公共 public 类外可以访问
c2 C2;
C2.m_c2 = 10;class 默认权限是私有 private 类外不可以访问
system("pause");
return 0;
}
1.3成员属性设置为私有
#include<iostream>
using namespace std;
#include<string>
//成员属性设置为私有
//1.可以自己控制读写的权限
//2.对于写可以检测数据额有效性
class Person {
public:
//写年龄 设置姓名
void setName(string name) {
m_Name = name;
};
//读姓名 获取姓名
string getName() {
return m_Name;
};
//获取年龄
int getAge() {
//m_Age = 0;//初始化为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) {
string m_Lover = lover;
};
private://属性私有化,类外不可访问
//姓名 可读可写
string m_Name;
//年龄 只读
int m_Age;
//情人 只写
string m_Lover;
};
int main() {
Person p;
p.getName();
p.setName("李四");
cout << "姓名为" << p.getName() << endl;
//访问年龄 只读在外界不能赋值
p.setAge(10);
cout << "年龄为" << p.getAge() << endl;
//给情人设置为仓井
p.setLover("仓井");
//cout << "情人为" << p.setLover( << endl;只写权限 外界只能赋值不能访问
system("pause");
return 0;
}
练习案例1:
#include<iostream>
using namespace std;
#include<string>
//立方体设计案例
//1.创建立方体类
//2.设计属性
//3,设计行为 获取立方体面积和体积
//4,分别用全局函数和成员函数 判断两个立方体是否相等
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_L * m_H + 2 * m_W * m_H;
}
//获取立方体的体积
int calculateV() {
return m_H * m_L * m_W;
}
//利用成员函数判断两个立方体是否相等
bool isSameByclass(Cube& c) {
if (m_W == c.getW() && m_L == c.getL() && 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.getW() == c2.getW() && c1.getL() == c2.getL() && 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;
cout << "c1的体积为" << c1.calculateV() << endl;
//创建第二个立方体
Cube c2;
c2.setL(10);
c2.setW(10);
c2.setH(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;
}
案例2:点和圆的关系
#include<iostream>
using namespace std;
#include<string>
//设计一个点类
class Point {
public:
//设置x坐标
void setX(int x) {
m_X = x;
}
//获取x坐标
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) {
//1.计算两点之间距离的平方 两横坐标减竖坐标的平方和
//圆心的X坐标-点的X坐标
int distance =
(c.getCenter().getX() - p.getX()) * (c.getCenter().getX() - p.getX()) +
(c.getCenter().getY() - p.getY()) * (c.getCenter().getY() - p.getY());
//2.计算半径的平方
int rDistance = c.getR() * c.getR();
//判断
if (distance == rDistance) {
cout << "点在圆上" << endl;
}
else if (distance > rDistance) {
cout << "点在圆外" << endl;
}
else {
cout << "点在圆内" << endl;
}
}
int main() {
//创建一个圆
Circle c;
//半径为10
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;
}
案例的第二核心点,整理代码
将代码分装
2.对象的初始化和清理
2.1构造函数和析构函数
2.2构造函数的分类以及调用
#include<iostream>
using namespace std;
//1.析构函数的分类及调用
//分类
//1.按照参数分类 无参构造(默认构造) 和有参构造
//2.按照类型分类 普通构造 拷贝构造
class Person {
public:
//普通构造函数
//构造函数 无参函数调用
Person() {
cout << "Person的无参构造函数调用" << endl;
}
//构造函数 有参函数调用
Person(int a) {
age = a;
cout << "Person的有参构造函数调用" << endl;
}
//拷贝构造函数 const保证数据不被修改
Person(const Person &p) {
//将传入的人身上所有的属性,拷贝到我身上
age = p.age;
cout << "Person的拷贝构造函数调用" << endl;
}
//析构函数
~Person() {
cout << "Person的析构函数调用" << endl;
}
//属性
int age;
};
//调用
void test01() {
//1.括号法
Person p1;//默认构造函数调用
Person p2(10);//有参构造函数调用
Person p3(10);//拷贝构造函数调用
//注意事项
// 1.调用默认构造函数的时候,不要加() 加括号后编译器会认为是函数的声明 Person p1();
cout << "p2的年龄为" << p2.age << endl;
cout << "p3的年龄为" << p3.age << endl;
//2.显示法
Person s1;//默认构造函数调用
Person s2 = Person(10);//有参构造函数调用
Person s3 = Person(s2);//拷贝构造函数调用
//注意事项
Person(10);//1.匿名对象 特点:当前执行结束后,系统会立即回收掉匿名的对象
//2.不要利用拷贝构造函数 初始化匿名对象 Person(s3);编译器会认为Person(s3)==Person(s3)编译器会认为是对象的声明
//3.隐式转换法
Person s4 = 10;//有参构造相当于写了 Person s4=Person(10);
Person s5 = s4;//拷贝构造
}
int main() {
test01();
system("pause");
}
2.3拷贝构造函数的调用时机
#include<iostream>
using namespace std;
//拷贝构造函数调用时机
//1.使用一个已经创建完毕的对象来初始化一个新的对象
//2.值传递的方式给函数参数传值
//3.值返回局部对象
class Person {
public:
Person() {
cout << "Person的默认构造函数调用" << endl;
}
Person(int age) {
m_Age = age;
cout << "Person的有参构造函数调用" << endl;
}
Person(const Person& p) {
m_Age = p.m_Age;
cout << "Person的拷贝构造函数调用" << endl;
}
~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;
}
2.4构造函数的调用规则
2.5深拷贝与浅拷贝
#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(10,170);
cout << "p1的 年龄为" << p1.m_Age <<"p1的身高为" << *p1.m_Height<< endl;
Person p2(p1);
cout << "p2的 年龄为" << p2.m_Age << "p2的身高为" << *p2.m_Height << endl;
}
int main() {
test01();
system("pause");
return 0;
}
2.6初始化列表
#include<iostream>
using namespace std;
//初始化列表
class Person {
public:
//传统初始化操作构造函数初始化
/*Person(int a, int b, int c) {
m_A = a;
m_B = b;
m_C = c;
}*/
//初始化 列表进行初始化属性
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(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;
}
2.7类对象作为类成员
#include<iostream>
using namespace std;
#include <string>
//类对象作为类成员
// 手机类
class Phone {
public:
Phone(string pName)
{
cout << "phone的构造函数调用" << endl;
m_PName = pName;
}
~Phone() {
cout << "P析构" << endl;
}
//手机品牌的名称
string m_PName;
};
//人类
class Person {
public:
//Phone m_Phone=pName;隐式转换法
Person(string name,string pName):m_Name(name),m_Phone(pName) {
cout << "Person的构造函数调用" << endl;
}
~Person() {
cout << "人析构" << 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;
}
2.8静态成员
#include<iostream>
using namespace std;
#include <string>
//静态成员函数
//所有对象共享同一个函数
//静态成员函数只能访问静态成员变量
class Person {
public:
//静态成员函数
static void func() {
m_A = 100;//静态成员函数 可以访问静态成员变量
m_B = 200;//静态成员函数 不可以访问静态成员变量,无法区分到底是哪个对象的属性
cout << "static void func调用" << endl;
}
static int m_A;//静态成员变量
int m_B;//非静态成员变量
private:
static void fun2(){
cout << "static void func2调用" << endl;
}
};
int Person::m_A = 0;//给静态成变量初始化
void test01() {
//通过对象访问
Person p;
p.func();
//通过类名访问
Person::func();
//Person::func2(); 类外访问不到这个私有的静态成员函数
}
int Person::m_A = 0;//给静态成变量初始化
int main() {
system("pause");
return 0;
}
3.C++对象模型和this指针
3.1成员变量和成员函数分开存储
空对象占用的内存空间为1
只有非静态成员变量属于类的对象上
#include<iostream>
using namespace std;
#include <string>
//成员变量和成员函数分开存储的
class Person {
int m_A;//非静态的成员变量 属于类的对象上的
static int m_B;//静态成员变量 不属于类的对象上
void func() {}//非静态成员函数 不属于类的对象上
static void func2() {} //静态成员函数 不属于类的对象上
};
int Person::m_B = 0;//给静态成变量初始化
void test01() {
Person p;
//空对象占用内存空间为:0 4 1
//c++编译器会给每个空对象也分配一个字节的空间,是为了区分空对象占内存的位置
//每个空对象也应该有一个独一无二的内存地址
cout << "size of p" << sizeof(p) << endl;
}
void test02() {
Person p;
cout << "size of p=" << sizeof(p) << endl;
}
int main() {
//test01();
test02();
system("pause");
return 0;
}
3.2 this指针概念
#include<iostream>
using namespace std;
#include <string>
//this的用途
//1、解决名称冲突
//2.返回对象本身用 *this
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;
}
3.3 空指针访问成员函数
#include<iostream>
using namespace std;
#include <string>
//空指针调用成员函数
class Person {
public:
void showClassName() {
cout << "this is Person class" << endl;
}
void showPersonAge() {
//报错原因是因为传入的指针是为空
if (this == NULL) {
return;
}
cout << "age=" <<this-> m_Age << endl;
}
int m_Age;
};
void test01() {
Person* p = NULL;
p->showClassName();
p->showPersonAge();
}
int main() {
test01();
system("pause");
return 0;
}
3.4 const修饰成员函数
#include<iostream>
using namespace std;
#include <string>
//const修饰成员函数
//常函数
class Person {
public:
//this指针的本质 是指针常量 指针的指向是不可以修改的
//const Person * const this
//在成员函数后面加const ,修饰的是this指针,让指针的值也不可以修改
void showPerson() const
{
//this->m_A = 100;
//this = NULL;//this指针不可以修改指针的指向的
this->m_B = 100;
}
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() {
system("pause");
return 0;
}
4.友元
4.1全局函数做友元
#include<iostream>
using namespace std;
#include <string>
//全局函数做友元
//建筑物类
class Buiding {
//goodGay全局函数是Building好朋友,可以访问Buiding的私有属性
friend void goodGay(Buiding* buiding);
public:
//给属性付初值
Buiding() {
m_SittingRoom = "客厅";
m_BedRoom = "卧室";
}
public:
string m_SittingRoom;//客厅
private:
string m_BedRoom;//卧室
};
//全局函数
void goodGay(Buiding* buiding) {
cout << "好基友的全局函数正在访问:" << buiding->m_SittingRoom << endl;
cout << "好基友的全局函数正在访问:" << buiding->m_BedRoom << endl;
}
void test01() {
Buiding buiding;
goodGay(&buiding);
}
int main() {
test01();
system("pause");
return 0;
}
//goodGay全局函数是Building好朋友,可以访问Buiding的私有属性
friend void goodGay(Buiding* buiding);
4.2类做友元
#include<iostream>
using namespace std;
#include <string>
//类做有元
class Buiding;
class GoodGay {
public:
GoodGay();
void visit();//参观函数 访问Building中的属性
Buiding * building;
};
class Building {
//goodGay是本类的好朋友可以访问本类中的私有成员
friend class GoodGay;
public:
Building();
string m_Sittingroom;//客厅
private:
string m_Bedroom;//卧室
};
//类外写成员函数
Building::Building() {
m_Sittingroom = "客厅";
m_Bedroom = "卧室";
}
GoodGay::GoodGay() {
//创建一个建筑物的对象
building = new Buiding;
}
void GoodGay::visit() {
cout << "好基友类正在访问:" << building->m_SittingRoom << endl;
cout << "好基友类正在访问:" << building->m_BedRoom << endl;
}
void test01() {
GoodGay gg;
gg.visit();
}
int main() {
system("pause");
return 0;
}
//goodGay是本类的好朋友可以访问本类中的私有成员
friend class GoodGay;
4.3成员函数做友元
#include<iostream>
using namespace std;
#include <string>
//类做有元
class Building;
class GoodGay {
public:
GoodGay();
void visit();//让visit函数 可以访问Builfng中的私有成员
void visit2();//让visit2函数 不可以访问Builfng中的私有成员
Building * building;
};
class Building {
//告诉编译器 GoodGay类下的visit成员函数作为本类的好朋友,可以访问私有成员
friend void GoodGay::visit();
public:
Building();
public:
string m_Sittingroom;//客厅
private:
string m_Bedroom;//卧室
};
//类外实现成员函数
Building::Building() {
m_Bedroom = "卧室";
m_Sittingroom = "客厅";
}
GoodGay::GoodGay(){
building = new Building;
}
void GoodGay::visit(){
cout << "visit函数正在访问:"<<building->m_Sittingroom << endl;
cout << "visit函数正在访问:" << building->m_Bedroom << endl;
}
void GoodGay::visit2(){
cout << "visit2函数正在访问:" << building->m_Sittingroom << endl;
//cout << "visit2函数正在访问:" << building->m_Bedroom << endl;
}
void test01() {
GoodGay gg;
gg.visit();
gg.visit2();
}
int main() {
test01();
system("pause");
return 0;
}
//告诉编译器 GoodGay类下的visit成员函数作为本类的好朋友,可以访问私有成员
friend void GoodGay::visit();
5.运算符重载
5.1加号运算符重载
#include<iostream>
using namespace std;
#include <string>
//加号运算符重载
//1.成员函数重载+号
//2.全局函数重载+号
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;
}
//3.函数重载的版本
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;
//1.成员函数重载本质的调用
//Person p3=p1.operator+(p2);
//2.全局函数重载本质调用
//Person p3 = operator+(p1, p2);
Person p3 = p1 + p2;
//3.运算符重载 也可以发生函数重载本质调用
//Person p4 = operator+(p4, 100);
Person p4 = p1 + 100;//Person + int
cout << "p3.m_A=" << p3.m_A << endl;
cout << "p3.m_B=" << p3.m_B << endl;
cout << "p4.m_A=" << p4.m_A << endl;
cout << "p4.m_B=" << p4.m_B << endl;
}
int main() {
test01();
system("pause");
return 0;
}
5.2左移运算符重载
#include<iostream>
using namespace std;
#include <string>
//左移运算符重载
class Person {
//利用友元函数访问私有属性
friend ostream& operator <<(ostream& cout, Person& p);
public:
//给属性符初始值。
Person(int a, int b) {
m_A = a;
m_B = b;
}
private:
//利用成员函数重载 左移运算符 p.operator<<(cout) 简化版本 p<<cout
//不会利用成员函数重载,因为无法实现 cout在左侧
/*void operator << (cout) {
}*/
int m_A;
int m_B;
};
//只能利用全局函数重载左移运算付
//本质 operator<<(cout,p) 简化 cout << p
ostream& operator <<(ostream& cout, Person& p) {
cout << "m_A" << p.m_A << " m_B" << p.m_B;
return cout;
}
void test01() {
Person p(10,10);
cout << p<< endl;
};
int main() {
test01();
system("pause");
return 0;
}
5.3递增运算符重载
#include<iostream>
using namespace std;
#include <string>
//递增运算符重载
//自定义整型
class MyInteger {
friend ostream& operator<<(ostream& cout, MyInteger& myint);
public:
MyInteger() {
m_Num = 0;
}
//重载前置++运算符 返回引用为了一直对一个 数据进行递增操作
MyInteger& operator++() {
//先进行++运算
m_Num++;
//再将自身做返回
return *this;//返回 自身
}
//重载后置++运算符
//void operator++(int) int代表占位参数可以区分前置和后置递增函数
MyInteger operator++(int) {
//先记录当时的结果(返回结果)
MyInteger temp = *this;
//后进行递增
m_Num++;
//最后将记录的结果做返回
return temp;
}
private:
int m_Num;
};
//重载左移运算符
ostream &operator<<(ostream & cout, MyInteger& myint){
cout << myint.m_Num ;
return cout;
}
void test01() {
MyInteger myint;
cout << ++myint<<endl;
}
void test02() {
MyInteger myint;
}
int main() {
test01();
system("pause");
return 0;
}
5.4赋值运算符重载
#include<iostream>
using namespace std;
#include <string>
//赋值运算符重载
class Person {
public:
Person(int age) {
m_Age = new int(age);
}
~Person() {
if (m_Age != NULL) {
delete m_Age;
m_Age = NULL;
}
}
//重载赋值运算符
Person& operator=(Person & p) {
//编译器是提供浅拷贝
//m_Age=p.m_Age;
//应该先判断是否有属性在堆区,如果有先释放干净,在进行深拷贝
if (m_Age != NULL) {
delete m_Age;
m_Age = NULL;
}
//深拷贝
m_Age = new int(*p.m_Age);
//返回对象本身
return *this;
}
int* m_Age;
};
void test01() {
Person p1(18);
Person p2(20);
Person p3(30);
p3 = p2 = p1;//赋值运算
cout << "p1的年领为:" << *p1.m_Age << endl;
cout << "p1的年领为:" << *p2.m_Age << endl;
}
int main() {
test01();
int a = 10;
int b = 20;
int c = 30;
c = b = a;
cout << "a=" << a << endl;
cout << "b=" << b << endl;
cout << "c=" << c << endl;
system("pause");
return 0;
}
5.5关系运算符重载
#include<iostream>
using namespace std;
#include <string>
//关系运算符重载
class Person {
public:
Person(string name, int age) {
m_Name = name;
m_Age = age;
}
//重载关系==运算符
bool operator==(Person& p) {
if (this->m_Name == p.m_Name && this->m_Age == p.m_Age) {
return true;
}
return false;
}
//重载关系!=运算符
bool operator!=(Person& p) {
if (this->m_Name == p.m_Name && this->m_Age == p.m_Age) {
return false;
}
return true;
}
string m_Name;
int m_Age;
};
void test01() {
Person p1("Tom", 18);
Person p2("Tom", 18);
if (p1 == p2) {
cout << "p1和p2是相等的" << endl;
}
else {
cout << "p1和p2是不相等的" << endl;
}
if (p1 != p2) {
cout << "p1和p2是不相等的" << endl;
}
else {
cout << "p1和p2是相等的" << endl;
}
}
int main() {
test01();
system("pause");
return 0;
}
5.6函数调用运算符重载
#include<iostream>
using namespace std;
#include <string>
//函数调用运算符重载
//打印输出类
class MyPrint {
public:
//重载函数调用运算符
void operator()(string test) {
cout << test << endl;
}
};
void MyPrint02(string test) {
cout << test << endl;
}
void test01() {
MyPrint myPrint;
myPrint("hello wprld");//由于使用起来非常类似于函数调用,因此称为仿函数
MyPrint02("hello wprld");
}
//仿函数非常灵活,没有固定额写法
class MyAdd {
public:
int operator()(int num1, int num2) {
return num1 + num2;
}
};
void test02() {
MyAdd myadd;
int ret = myadd(100, 100);
cout << "ret=" << ret << endl;
//匿名函数对象
cout << MyAdd()(100, 100) << endl;
}
int main() {
test01();
test02();
system("pause");
return 0;
}
6.继承
6.1继承的基本语法
#include<iostream>
using namespace std;
#include <string>
//继承
// 继承的好处
// 1.减少重复的代码
// 语法:class 子类:继承方式 父类
// 子类也称为派生类 父类也称为基类
//普通实现页面
//JAVA页面
/*class Java {
public:
void header() {
cout << "首页、公开课、登录、注册...(公共的头部)" << endl;
}
void footer() {
cout << "帮助中心、交流合作、站内地图....(公共的底部)" << endl;
}
void left() {
cout << "Java Python C++...(公共分类列表)" << endl;
}
void content() {
cout << "Java学科视频" << endl;
}
};
//Python页面
class Python {
public:
void header() {
cout << "首页、公开课、登录、注册...(公共的头部)" << endl;
}
void footer() {
cout << "帮助中心、交流合作、站内地图....(公共的底部)" << endl;
}
void left() {
cout << "Java Python C++...(公共分类列表)" << endl;
}
void content() {
cout << "Python学科视频" << endl;
}
};
//C++页面
class CPP {
public:
void header() {
cout << "首页、公开课、登录、注册...(公共的头部)" << endl;
}
void footer() {
cout << "帮助中心、交流合作、站内地图....(公共的底部)" << endl;
}
void left() {
cout << "Java Python C++...(公共分类列表)" << endl;
}
void content() {
cout << "C++学科视频" << endl;
}
};*/
//继承实现页面
//公共页面类
class BasePage {
public:
void header() {
cout << "首页、公开课、登录、注册...(公共的头部)" << endl;
}
void footer() {
cout << "帮助中心、交流合作、站内地图....(公共的底部)" << endl;
}
void left() {
cout << "Java Python C++...(公共分类列表)" << endl;
}
};
//Java页面
class Java:public BasePage {
public:
void content() {
cout << "Java学科视频" << endl;
}
};
//Python页面
class Python :public BasePage {
public:
void content() {
cout << "python学科视频" << endl;
}
};
//C++页面
class CPP :public BasePage {
public:
void content() {
cout << "C++学科视频" << endl;
}
};
void test01() {
cout << "Java下载视频页面如下" << endl;
Java ja;
ja.header();
ja.footer();
ja.left();
ja.content();
cout << "---------------------" << endl;
}
void test02() {
cout << "Python下载视频页面如下" << endl;
Python py;
py.header();
py.footer();
py.left();
py.content();
cout << "---------------------" << endl;
}
void test03() {
cout << "C++下载视频页面如下" << endl;
CPP cp;
cp.header();
cp.footer();
cp.left();
cp.content();
cout << "---------------------" << endl;
}
int main() {
test01();
test02();
test03();
system("pause");
return 0;
}
6.2继承方式
#include<iostream>
using namespace std;
#include <string>
//继承方式
//公共继承
class Base1 {
public:
int m_A;
protected:
int m_B;
private:
int m_C;
};
class Son1 :public Base1 {
public:
void func() {
m_A = 10;//父类中的公共权限成员 到子类中依然是公共权限
m_B = 10;//父类中的保护权限成员 到子类中依然是保护权限
//m_C = 10;//父类中的私有权限成员 到子类中访问不到
}
};
void test01() {
Son1 s1;
s1.m_A = 100;
//s1.m_B = 100;//到Son1中 m_B是保护权限 类外访问不到
}
//保护继承
class Base2 {
public:
int m_A;
protected:
int m_B;
private:
int m_C;
};
class Son2 :protected Base2 {
public:
void func() {
m_A = 100;//父类中的公共权限成员 到子类中是保护权限
m_B = 100;//父类中的保护权限成员 到子类中依然是保护权限
//m_C = 10;//父类中的私有权限成员 到子类中访问不到
}
};
void test02() {
Son2 s2;
//s2.m_A = 100;//到Son2中 m_A是保护权限 类外访问不到
//s2.m_B = 100;//到Son1中 m_B是保护权限 类外访问不到
}
//私有继承
class Base3 {
public:
int m_A;
protected:
int m_B;
private:
int m_C;
};
class Son3 :private Base3{
public:
void func() {
m_A = 100;//父类中的公共权限成员 到子类中是私有权限
m_B = 100;//父类中的保护权限成员 到子类中是私有权限
//m_C = 10;//父类中的私有权限成员 到子类中访问不到
}
};
void test03() {
Son3 s3;
//s2.m_A = 100;//到Son3中 m_A是保护权限 类外访问不到
//s2.m_B = 100;//到Son3中 m_B是保护权限 类外访问不到
}
class GrandSon3 :public Son3 {
public:
void func() {
//m_A = 1000;到Son3中变成了私有权限 儿子也访问不到了
}
};
int main() {
system("pause");
return 0;
}
6.3继承中的对象模型
#include<iostream>
using namespace std;
#include <string>
//继承中的对象模型
class Base {
public:
int m_A;
protected:
int m_B;
private:
int m_C;
};
class Son :public Base {
public:
int m_D;
};
void test01() {
//16 父类中所有非静态成员属性都会被子类继承下去
//父类中私有成员属性 是被编译器给隐藏了,因此是访问不到,但是确实是继承下去了。
cout << "size of Son="<<sizeof(Son) << endl;
}
int main() {
test01();
system("pause");
return 0;
}
6.4继承中的构造和析构顺序
6.5继承同名成员处理方法
#include<iostream>
using namespace std;
#include <string>
//继承中同名成员处理方法
class Base {
public:
Base() {
m_A = 100;
}
void func() {
cout << "Base 函数调用 " << endl;
}
void func(int a) {
cout << "Base int a 函数调用 " << endl;
}
int m_A;
};
class Son :public Base {
public:
Son() {
int m_A = 200;
}
void func() {
cout << "Son 函数调用 " << endl;
}
int m_A;
};
//同名属性访问方式
void test01() {
Son s;
cout << "m_A" << s.m_A << endl;
// 如果通过子类对象访问父类中同名成员,需要加作用域
cout << "m_A" << s.Base::m_A << endl;
}
//同名函数访问方式
void test02() {
Son s;
s.func();//直接调用调用的是子类中的地同名成员
//调用父类函数
s.Base::func();
//如果子类中出现和父类同名的成员函数,子类的同名函数会隐藏掉父类中所有同名的成员函数
s.Base::func(100);
}
int main() {
test01();
test02();
system("pause");
return 0;
}
6.6继承同名静态成员处理方法
#include<iostream>
using namespace std;
#include <string>
//继承中同名静态成员处理方法
class Base {
public:
Base() {
m_A = 100;
}
static void func() {
cout << "Base static void func的调用" << endl;
}
static int m_A;
};
//给静态属性初始化
int Base::m_A = 100;
class Son :public Base {
public:
Son() {
int m_A = 200;
}
static void func() {
cout << "Son static void func的调用" << endl;
}
static int m_A;
};
//给静态属性初始化
int Son::m_A = 200;
//同名静态属性访问方式
void test01() {
//1.通过对象访问
cout << "通过对象访问:" << endl;
Son s;
cout << "Son 下 m_A" << s.m_A << endl;
cout << "Base下 m_A" << s.Base::m_A << endl;
//通过类名访问
cout << "通过类名访问:" << endl;
cout << "Son 下 m_A=" << Son::m_A << endl;
//第一个吗 ::代表通过类名 第二个::代表访问父类作用域下的
cout << "Son 下 m_A=" << Son::Base::m_A << endl;
}
//同名静态函数访问方式
void test02() {
//通过对象访问
cout << "通过对象访问:" << endl;
Son s;
s.func();//直接调用调用的是子类中的地同名成员
//调用父类函数
s.Base::func();
//如果子类中出现和父类同名的成员函数,子类的同名函数会隐藏掉父类中所有同名的成员函数
cout << "通过类名访问:" << endl;
//通过类名访问
Son::func();
Son::Base::func();
}
int main() {
//test01();
test02();
system("pause");
return 0;
}
6.7多继承语法
#include<iostream>
using namespace std;
#include <string>
//多继承语法
class Base1 {
public:
Base1() {
m_A = 100;
}
int m_A;
};
class Base2 {
public:
Base2() {
m_A = 900;
m_B = 200;
}
int m_A;
int m_B;
};
//子类需要继承Base1和Base2
//语法:class 子类:继承方式 父类1,继承方式 父类2。。。
class Son:public Base1,public Base2 {
public:
Son(){
m_C = 300;
m_D = 400;
}
int m_C;
int m_D;
};
void test01() {
Son s;
cout << "sizeof Son=" << sizeof(s) << endl;
//当父类中出现同名成员,需要加作用域
cout << "m_A" << s.Base1::m_A << endl;
cout << "m_A" << s.Base2::m_A << endl;
}
int main() {
test01();
system("pause");
return 0;
}
6.8菱形继承
#include<iostream>
using namespace std;
#include <string>
//菱形继承 钻石继承
//动物类
class Animal {
public:
int m_Age;
};
//利用虚继承,解决菱形继承的问题
//继承之前 加上关键字virtual 变为虚继承
//Animal称为虚基类
//羊类
class Sheep:virtual public Animal {};
//驼类
class Tuo:virtual public Animal{};
//羊驼类
class Sheeptuo :public Sheep, public Tuo {};
void test01() {
Sheeptuo st;
st.Sheep::m_Age = 18;
st.Tuo::m_Age = 10;
//当菱形继承,两个父类拥有同样相同数据,需要加以作用域区分。
cout << "Sheep::m_Age =" << st.Sheep::m_Age << endl;
cout << "Tuo::m_Age =" << st.Tuo::m_Age << endl;
cout << st.m_Age << endl;
//这份数据我们指导只有一分就可以,菱形继承导致数据有两份,形成了数据浪费。
}
int main() {
test01();
system("pause");
return 0;
}
7.多态
7.1多态的基本概念
#include<iostream>
using namespace std;
//多态
// 动态多态的满足条件
// 1.有继承关系
// 2.子类要重新父类的虚函数
//动类多态的使用
// 父类的指针货者引用,指向子类对象
//动物类
class Animal
{
public:
//虚函数
virtual void sperak()
{
cout << "动物在说话" << endl;
}
};
//猫类
class Cat :public Animal
{
//重写 函数的返回值类型 函数名 参数列表 完全相同
void sperak()
{
cout << "小猫在说话" << endl;
}
};
//狗类
class Dog :public Animal
{
void sperak()
{
cout << "小狗在说话" << endl;
}
};
//执行说话的函数
//地址早绑定 在编译阶段确定函数地址
//如果想执行让猫说话,那么这个函数的地址就不能早绑定,
//就需要在运行阶段绑定,地址晚绑定
void doSpeak(Animal &animal) //Animal &animal =cat;
{
animal.sperak();
}
void test01() {
Cat cat;
doSpeak(cat);
Dog dog;
doSpeak(dog);
}
int main(){
test01();
system("pause");
return 0;
}
7.2多态的案例-计算机类
#include<iostream>
using namespace std;
#include<string>
//分别利用普通写法和多态技术实现计算器
//普通写法
class Calculator {
public:
int getResult(string oper) {
if (oper == " +")
{
return m_Num1 + m_Num2;
}
else if (oper == "-") {
return m_Num1 - m_Num2;
}
else if (oper == "*") {
return m_Num1 * m_Num2;
}
//如果想扩展新的操作需要修改原码
//在真的开发中 提倡 开闭原则
//开闭原则:对扩展 进行开放,对修改进行关闭
}
int m_Num1;//操作数1
int m_Num2;//操作数2
};
void test01() {
//创建计算器对象
Calculator c;
c.m_Num1 = 10;
c.m_Num2 = 10;
cout << c.m_Num1 << "+" << c.m_Num2 << "=" << c.getResult("+") << endl;
cout << c.m_Num1 << "-" << c.m_Num2 << "=" << c.getResult("-") << endl;
cout << c.m_Num1 << "*" << c.m_Num2 << "=" << c.getResult("*") << endl;
}
//利用多台态实现计算器
// 多态的好处:
// 1.组织结构清晰
// 2.可读性强
// 3.对前期后期的维护新高
//实现计算器的基类(抽象类)
class AbstractCalculator {
public:
virtual int getResult() {
return 0;
}
int m_Num1;
int m_Num2;
};
//加法计算器类
class AddCalculator :public AbstractCalculator {
public:
int getResult() {
return m_Num1 + m_Num2;
}
};
//减法计算器类
class SubCalculator :public AbstractCalculator {
public:
int getResult() {
return m_Num1 - m_Num2;
}
};
//乘法计算器类
class MulCalculator :public AbstractCalculator {
public:
int getResult() {
return m_Num1 * m_Num2;
}
};
void test02() {
//多态使用条件
//父类的指针或者引用指向子类对象
//加法运算
AbstractCalculator* abc = new AddCalculator;
abc->m_Num1 = 10;
abc->m_Num2 = 10;
cout << abc->m_Num1 << "+" << abc->m_Num2 << "=" << abc->getResult() << endl;
//用完直接销毁
delete abc;
//减法运算
abc = new SubCalculator;
abc->m_Num1 = 10;
abc->m_Num2 = 5;
cout << abc->m_Num1 << "-" << abc->m_Num2 << "=" << abc->getResult() << endl;
//用完直接销毁
delete abc;
//乘法运算
abc = new MulCalculator;
abc->m_Num1 = 10;
abc->m_Num2 = 5;
cout << abc->m_Num1 << "*" << abc->m_Num2 << "=" << abc->getResult() << endl;
//用完直接销毁
delete abc;
}
int main(){
test01();
test02();
system("pause");
return 0;
}
7.3纯虚函数和抽象类
#include<iostream>
using namespace std;
#include <string>
//纯虚函数和抽象类
class Base {
public:
//纯虚函数
//只要有一个纯虚函数,这个类称为抽象类
//抽象类特点:
//1.无法实例化对象
//2.抽象类的子类 必须要重写父类中的纯虚函数,否则也属于抽象类
virtual void func() = 0;
};
class Son :public Base {
public:
virtual void func()
{
cout << "func 的函数调用" << endl;
}
};
void test01() {
//Base b; 抽象类无法实例化对象
//new Base;抽象类无法实例化对象
Son s;
Base* base = new Son;
base->func();
}
int main() {
test01();
system("pause");
return 0;
}
7.4多态案例2-制作饮品
#include<iostream>
using namespace std;
//饮品制作案例
class AbstractDringking {
public:
//煮水
virtual void Boil() = 0;
//冲泡
virtual void Brew() = 0;
//倒入杯中
virtual void PourInCup () = 0;
//加入其他辅料
virtual void PutSomething() = 0;
//制作饮品
void makeDrink() {
Boil();
Brew();
PourInCup();
PutSomething();
}
};
//制作咖啡
class Coffee:public AbstractDringking
{
public:
//煮水
virtual void Boil()
{
cout << "煮农夫山泉" << endl;
}
//冲泡
virtual void Brew()
{
cout << "冲泡咖啡" << endl;
}
//倒入杯中
virtual void PourInCup()
{
cout << "倒入杯中" << endl;
}
//加入其他辅料
virtual void PutSomething(){
cout << "加入糖和牛奶" << endl;
}
};
//制作茶叶
class Tea :public AbstractDringking
{
public:
//煮水
virtual void Boil()
{
cout << "煮开水" << endl;
}
//冲泡
virtual void Brew()
{
cout << "冲泡茶叶" << endl;
}
//倒入杯中
virtual void PourInCup()
{
cout << "倒入杯中" << endl;
}
//加入其他辅料
virtual void PutSomething() {
cout << "加入柠檬" << endl;
}
};
//制作函数
void doWork(AbstractDringking * abs) {
abs->makeDrink();
delete abs;//释放
cout << "-------------------------" << endl;
}
void test01() {
//制作咖啡
doWork(new Coffee);
doWork(new Tea);
}
int main() {
test01();
system("pause");
return 0;
}
7.5虚析构和纯虚析构
#include<iostream>
using namespace std;
#include <string>
//虚析构和纯虚析构
class Animal {
public:
//纯虚函数
Animal(){
cout << "Animal的构造函数调用" << endl;
}
//利用虚析构可以解决 父类指针释放子类对象释放不干净的问题
/*virtual ~Animal() {
cout << "Animal的虚析构函数调用" << endl;
}
*/
//纯虚析构声明 需要声明也需要偶实现
//有了纯虚析构之后,这个类也属于抽象类,无法实例化对象
virtual ~Animal() = 0;
virtual void speak() = 0;
};
//Animal纯虚析构实现
Animal::~Animal() {
cout << "Animal的纯虚析构函数调用" << endl;
}
class Cat :public Animal {
public:
//纯虚函数
Cat(string name) {
cout << "Cat的构造函数调用" << endl;
m_Name=new string(name);
}
virtual void speak() {
cout << *m_Name<<"小猫在说话" << endl;
}
~Cat() {
if (m_Name != NULL) {
cout << "Cat的析构函数调用" << endl;
delete m_Name;
m_Name = NULL;
}
}
string * m_Name;
};
void test01() {
Animal* animal = new Cat("Tom");
animal->speak();
//父类指针在析构时候 不会调用子类中析构函数 导致子类如果有堆区属性,出现内存泄漏
delete animal;
}
int main() {
test01();
system("pause");
return 0;
}
7.6多态案例3-电脑操作
#include<iostream>
using namespace std;
//案例3 电脑组装
//抽象不同的零件类
//抽象CPU的类
class CPU
{
public:
//抽象的计算的函数
virtual void calculate() = 0;
};
//抽象显卡的类
class VideoCard
{
public:
//抽象的显示的函数
virtual void display() = 0;
};
//抽象内存条的类
class Memory
{
public:
//抽象存储的函数
virtual void storage() = 0;
};
//电脑类
class Computer {
public:
Computer(CPU* cpu, VideoCard* vc, Memory* mem) {
m_cpu = cpu;
m_vc = vc;
m_mem = mem;
}
//提供工作的函数
void work() {
//让零件工作起来调用接口
m_cpu->calculate();
m_vc->display();
m_mem->storage();
}
~Computer() {
//释放CPU零件
if (m_cpu != NULL) {
delete m_cpu;
m_cpu = NULL;
}
//释放显卡零件
if (m_vc != NULL) {
delete m_vc;
m_vc = NULL;
}
//释放内存条零件
if (m_mem != NULL) {
delete m_mem;
m_mem = NULL;
}
}
private:
CPU* m_cpu;//cpu零件的指针
VideoCard* m_vc;//显卡的指针
Memory* m_mem;//内存条的指针
};
//具体的厂商
//Intel厂商
class IntelCPU :public CPU {
public:
virtual void calculate() {
cout << "Inter的CPU开始计算了" << endl;
}
};
class IntelVideoCard :public VideoCard {
public:
virtual void display() {
cout << "Inter的显卡开始显示了" << endl;
}
};
class IntelMemory :public Memory {
public:
virtual void storage() {
cout << "Inter的内存条开始存储了" << endl;
}
};
//Lenovo厂商
class LenovoCPU :public CPU {
public:
virtual void calculate() {
cout << "Lenovo的内存条开始存储了" << endl;
}
};
class LenovoMemory :public Memory {
public:
virtual void storage() {
cout << "Lenovo的内存条开始存储了" << endl;
}
};
class LenovoVideoCard :public VideoCard {
public:
virtual void display() {
cout << "Lenovo的显卡开始显示了" << endl;
}
};
void test01() {
//第一台电脑的零件
CPU* intelCpu = new IntelCPU;
VideoCard* intelCard = new IntelVideoCard;
Memory* intelMemory = new IntelMemory;
cout << "第一电台脑开始工作了:" << endl;
//创建第一台电脑
Computer* computer1 = new Computer(intelCpu,intelCard,intelMemory);
computer1->work();
delete computer1;
cout << "---------------------------" << endl;
cout << "第二台电脑开始工作了:" << endl;
//第二台电脑组装
Computer* computer2 =new Computer(new LenovoCPU,new LenovoVideoCard,new LenovoMemory);
computer2->work();
delete computer2;
cout << "---------------------------" << endl;
cout << "第三台电脑开始工作了:" << endl;
//第三台电脑组装
Computer* computer3 = new Computer(new LenovoCPU, new IntelVideoCard, new LenovoMemory);
computer3->work();
delete computer3;
}
int main() {
test01();
system( "pause" );
return 0;
}
五、文件操作
5.1文本文件
5.1.1写文件
#include<iostream>
using namespace std;
#include <string>
#include<fstream>//头文件的包含
//文本文件 写文件
void test01() {
//1、包含头文件 fstream
//2,创建流对象
ofstream ofs;
//3.指定打开方式 文件路径 打开方式
ofs.open("test.txt", ios::out);
//4.写内容
ofs << "姓名:张三" << endl;
ofs << "年龄:18" << endl;
ofs << "性别:男" << endl;
//5.关闭文件
ofs.close();
}
int main() {
test01();
system("pause");
return 0;
}
5.1.2读文件
#include<iostream>
using namespace std;
#include <string>
#include<fstream>//头文件的包含
//文本文件 读文件
void test01() {
//1、包含头文件 fstream
//2,创建流对象
ifstream ifs;
//3.打开文件 并且判断文件是否打开成功
ifs.open("test.txt", ios::in);
if (!ifs.is_open()) {
cout << "文件打开失败" << endl;
return;
}
//4.读数据 四种方法
//第一种
/*char buf[1024] = {0};
while (ifs >> buf) {
cout << buf << endl;
}
*/
//第二种
/*/char buf[1024] = {0};
while (ifs.getline(buf,sizeof(buf)))
{
cout << buf << endl;
}
*/
//第三种
/*string buf;
while (getline(ifs, buf)) {
cout << buf << endl;
}
*/
//第四种
char c;
while ((c = ifs.get()) != EOF) { // end of line
cout << c << endl;
}
//5.关闭文件
ifs.close();
}
int main() {
test01();
system("pause");
return 0;
}
5.2 二进制文件
5.2.1二进制写文件
#include<iostream>
using namespace std;
#include <string>
#include<fstream>//头文件的包含
//二进制 写文件
class Person {
public:
char m_Name[64];//姓名
int m_Age;//年龄
};
void test01() {
//1.包含头文件
//2.创建流对象
ofstream ofs("person.txt", ios::out | ios::binary);
//3.打开文件
//ofs.open("person.txt", ios::out | ios::binary);
//4.写文件
Person p = { "张三",18};
ofs.write((const char*)&p, sizeof(Person));
//5.关闭文件
ofs.close();
}
int main() {
test01();
system("pause");
return 0;
}
5.2.2二进制读文件
#include<iostream>
using namespace std;
#include <string>
#include<fstream>//头文件的包含
//二进制 写文件
class Person {
public:
char m_Name[64];//姓名
int m_Age;//年龄
};
void test01() {
//1.包含头文件
//2.创建流对象
ifstream ifs;
//3.打开文件 判断文件是否打开成功
ifs.open("person.txt", ios::in | ios::binary);
if (!ifs.is_open()) {
cout << "打开文件失败" << endl;
return;
}
//4.读文件
Person p = { "张三",18};
ifs.read((char*)&p, sizeof(Person));
cout << "姓名: " << p.m_Name << " 年龄: " << p.m_Age << endl;
//5.关闭文件
ifs.close();
}
int main() {
test01();
system("pause");
return 0;
}