C++养成教程(从入门到精通)

前言

最近因工作需要学习C++,决定开创一个养成系的博客,目标是分享我的C++的学习过程和学习经验。之后会把学习过程中的所有代码写入这篇博客里,由于这一想法是在中途出现,所以更新内容会从中间开始,之后会陆续补充完善全部内容。
(代码学习取自bilibili:黑马程序员匠心之作|C++教程从0到1入门编程,学习编程不再难)

通讯录管理系统

//封装函数显式该界面 如void showMenu()
//在main函数中调用封装好的函数
#include<iostream>
#include<string>
#define MAX 1000 //最大人数
using namespace std;

//联系人结构体
struct Person
{
	string m_Name;//姓名
	int m_Sex;//性别:1男 2女
	int m_Age;//年龄
	string m_Phone;//电话
	string m_Addr;//住址
};


//通讯录结构体
struct Addressbooks
{
	struct Person personArray[MAX];//通讯录中保存的联系人数组

	int m_Size;//通讯录中人员个数
};

//菜单界面
void shouMenu() 
{
	cout << "   *****   *****   *****   *****" << endl;
	cout << "*****   1、添加联系人   *****" << endl;
	cout << "*****   2、显式联系人   *****" << endl;
	cout << "*****   3、删除联系人   *****" << endl;
	cout << "*****   4、查找联系人   *****" << endl;
	cout << "*****   5、修改联系人   *****" << endl;
	cout << "*****   6、清空联系人   *****" << endl;
	cout << "*****   0、退出通讯录   *****" << endl;
}
//1、添加联系人
void addPerson(Addressbooks *abs)
{
	//判断通讯录是否已满,如果满了,将不再添加
	if (abs->m_Size == MAX)
	{
		cout << "通讯录已满,无法添加" << endl;
		return;
	}
	else
	{
		//添加具体联系人

		//姓名
		string name;
		cout << "请输入姓名:" << endl;
		cin >> name;
		abs->personArray[abs->m_Size].m_Name=name;

		//性别
		cout << "请输入性别:" << endl;
		cout << "1--- 男" << endl;
		cout << "2--- 女" << endl;
		int sex = 0;

		while (true) 
		{
			//如果输入的是1 或者 2 可以退出循环,因为输入的是正确值
			//如果输入有误,重新输入
			cin >> sex;
			if (sex == 1 || sex == 2)
			{
				abs->personArray[abs->m_Size].m_Sex = sex;
				break;
			}
			cout << "输入有误,请重新输入" << endl;
		}
		//年龄
		cout << "请输入年龄:" << endl;
		int age = 0;
		while (true)
		{
			if (age > 0 || age < 150)
			{
				cin >> age;
				abs->personArray[abs->m_Size].m_Age = age;
				break;
			}
			cout << "输入有误,请重新输入" << endl;
		}

		//电话
		cout << "请输入联系电话:" << endl;
		string phone;
		cin >> phone;
		abs->personArray[abs->m_Size].m_Phone = phone;

		//住址
		cout << "请输入家庭住址:" << endl;
		string address;
		cin >> address;
		abs->personArray[abs->m_Size].m_Addr = address;

		//更新通讯录人数
		abs->m_Size++;

		cout << "添加成功" << endl;

		system("pause");//请按任意键继续
		system("cls");//清屏操作
	}
}
//2、显式所有联系人
void showPerson(Addressbooks *abs)
{
	//判断通讯录中人数是否为0,如果为0,提示记录为空
	//如果不为0,显式记录的联系人信息
	if (abs->m_Size == 0)
	{
		cout << "当前记录为空" << endl;
	}
	else
	{
		for (int i = 0; i < abs->m_Size; i++)
		{
			cout << "姓名:" << abs->personArray[i].m_Name << "\t";
			cout << "性别:" << (abs->personArray[i].m_Sex ==1 ?"男":"女" )<< "\t";
			cout << "年龄:" << abs->personArray[i].m_Age << "\t";
			cout << "电话:" << abs->personArray[i].m_Phone << "\t";
			cout << "住址:" << abs->personArray[i].m_Addr << endl;
		}
	}

	system("pause");//按任意键继续
	system("cls");//清屏
}

//3、检测联系人是否存在,如果存在,返回联系人所在数组中的具体位置,不存在返回-1
//参数1  通讯录  参数2 对比姓名
int isExist(Addressbooks* abs, string name)
{
	for (int i = 0; i < abs->m_Size; i++)
	{
		//找到用户输入的姓名了
		if (abs->personArray[i].m_Name == name)
		{
			return i;
		}
	}
	return -1;//如果遍历结束都没有找到,返回-1
}
//3、删除指定联系人
void deletePerson(Addressbooks *abs)
{
	cout << "请输入您要删除的联系人" << endl;

	string name;
	cin >> name;

	//ret==-1 未查到
	//ret !=-1 查到了
	int ret=isExist(abs, name);

	if (ret != -1)
	{
		//查找到人,要进行删除操作
		for (int i = ret; i < abs->m_Size; i++)
		{
			//数据前移
			abs->personArray[i] = abs->personArray[i + 1];
		}
		abs->m_Size--;//更新通讯录中的人员数
		cout << "删除成功" << endl;
	}
	else
	{
		cout << "查无此人" << endl;
	}

	system("pause");
	system("cls");
}

//4、查找指定联系人信息
void findPerson(Addressbooks *abs)
{
	cout << "请输入您要查找的联系人" << endl;
	string name;
	cin >> name;

	//判断指定的联系人是否存在通讯录中
	int ret = isExist(abs, name);

	if (ret != -1)//找到联系人
	{
		cout << "姓名:" << abs->personArray[ret].m_Name << "\t";
		cout << "性别:" << abs->personArray[ret].m_Sex << "\t";
		cout << "年龄:" << abs->personArray[ret].m_Age << "\t";
		cout << "电话:" << abs->personArray[ret].m_Phone << "\t";
		cout << "住址:" << abs->personArray[ret].m_Addr << endl;
	}
	else //未找到联系人
	{
		cout << "查无此人" << endl;
	}


	//任意键按下后 清屏
	system("pause");
	system("cls");
}

//5、修改指定联系人信息
void modifyPerson(Addressbooks* abs)
{
	cout << "请输入您要修改的联系人" << endl;
	string name;
	cin >> name;

	int ret = isExist(abs, name);

	if (ret != -1)//找到指定联系人
	{
		//姓名
		string name;
		cout << "请输入姓名:" << endl;
		cin >> name;
		abs->personArray[ret].m_Name = name;
		//性别
		cout << "请输入性别:" << endl;
		cout << "1 --- 男" << endl;
		cout << "2 --- 女" << endl;
		int sex = 0;

		while(true)
		{
			cin >> sex;
			if (sex == 1 || sex == 2)
			{
				//输入正确 退出循环输入
				abs->personArray[ret].m_Sex = sex;
				break;
			}
			cout << "输入有误,请重新输入" << endl;
		}
		//年龄
		cout << "请输入年龄:" << endl;
		int age = 0;
		cin >> age;
		abs->personArray[ret].m_Age = age;

		//电话
		cout << "请输入联系电话:" << endl;
		string phone;
		cin >> phone;
		abs->personArray[ret].m_Phone = phone;

		//电话
		cout << "请输入家庭住址:" << endl;
		string address;
		cin >> address;
		abs->personArray[ret].m_Addr = address;

		cout << "修改成功" << endl;
		
	}
	else //未找到联系人
	{
		cout << "查无此人" << endl;
	}
	system("pause");
	system("cls");
}

//6、清空所有联系人
void cleanPerson(Addressbooks* abs)
{
	abs->m_Size = 0;//将当期记录联系人数量置为0,做逻辑清空操作
	cout << "通讯录已清空" << endl;
	//按任意键后清屏
	system("pause");
	system("cls");

}

int main()
{
	//创建通讯录结构体变量
	Addressbooks abs;
	//初始化通讯录中当前人员个数
	abs.m_Size = 0;

	int select = 0;//创建用户选择输入的变量

	while(true)
	{
		//菜单调用
		shouMenu();

		cin >> select;

		switch (select)
		{
		case 1://1、添加联系人
			addPerson(&abs);
			break;
		case 2://2、显式联系人
			showPerson(&abs);
			break;
		case 3://3、删除联系人
		/*{
			cout << "请输入删除联系人姓名:" << endl;
			string name;
			cin >> name;

			if (isExist(&abs, name) == -1)
			{
				cout << "查无此人" << endl;
			}
			else
			{
				cout << "找到此人" << endl;
			}
		}*/
			deletePerson(&abs);
			break;
		case 4://4、查找联系人
			findPerson(&abs);
			break;
		case 5://5、修改联系人
			modifyPerson(&abs);
			break;
		case 6://6、清空联系人
			cleanPerson(&abs);
			break;
		case 0://0、退出通讯录
			cout << "欢迎下次使用" << endl;
			system("pause");
			return 0;
			break;
		default:
			break;
		}
	}
	


	system("pause");
	return 0;
}

栈区

面向对象

#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() {

	//全局区

	//全局变量、静态变量、常量

	//创建普通局部变量
	int a = 10;
	int b = 10;

	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,属于静态变量
	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;// c const g_global 1-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;

	system("pause");

	return 0;
}

栈区

#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;
}

堆区

堆区

#include<iostream>
using namespace std;

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

int main1() {

	//在堆区开辟数据
	int* p = func1();

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

	system("pause");
	return 0;
}

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;//内存已经被释放,再次访问就是非法操作,会报错
}


//2、在堆区利用new开辟数组
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;
}

int main() {

	//test01();
	test02();

	system("pause");
	return 0;
}

引用

引用

#include<iostream>
using namespace std;



int main() {


	//1、应用必须初始化
	int a = 10;
	//创建引用
	int& b = a;

	//2、应用在初始化后,不可以改变
	int c = 20;

	b = c;//赋值操作,而不是更改引用
	//引用基本语法
	//数据类型 &别名=原名
	


	cout <<"a="<< a << endl;//??? 也是20
	cout << "b=" << b << endl;
	cout << "c=" << c << endl;


	system("pause");
	return 0;

}

引用的本质

#include<iostream>
using namespace std;

//引用的本质在c++内部实现是一个指针常量
//发现是引用,转换为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;
	cout << &ref << endl;
	cout << ref << endl;
	ref = 20;//内部发现ref是引用,自动帮我们转换为:*ref=20;

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

	system("pause");
	return 0;
}

引用做函数参数

#include<iostream>
using namespace std;

//交换函数

	//1、值传递
void mySwap01(int a, int b)
{
	int temp = a;
	a = b;
	b = temp;
	/*cout << "swap01a=" << a << endl;
	cout << "swap01b=" << 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;
}

引用做函数返回值

#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 << "ref2=" << ref2 << endl;
	cout << "ref2=" << ref2 << endl;
	cout << "ref2=" << ref2 << endl;

	test02() = 1000;//如果函数的返回值是引用,这个函数调用可以作为左值

	cout << "ref2=" << ref2 << endl;
	cout << "ref2=" << ref2 << endl;


	system("pause");
	return 0;
}

常量引用

#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
	//int& ref = 10;//错误,引用必须引一块合法的内存空间
	//const int& ref = 10;
	//ref = 10;//错误 加入const之后变为只读,不可修改

	int a = 100;
	showValue(a);
	cout << "a=" << a << endl;

	system("pause");
	return 0;
}

函数高级

函数的默认参数

#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 func2(int a, int b = 10, int c=20)正确
int func2(int a, int b, int c)
{
	return a + b + c;
}

//2、如果函数声明有默认参数,函数实现不能有默认参数
int func3(int a=10, int b=10);

//int func3(int a=10, int b=10) 错误
//声明和实现只能有一个默认参数
int func3(int a , int b)
{
	return a + b;
}
int main() {

	//
	cout << func(10,30) << endl;

	cout << func3(10, 10) << endl;

	system("pause");
	return 0;
}

函数的占位参数

#include<iostream>
using namespace std;

//占位参数
//返回值类型 函数名(数据类型){}

//目前阶段的占位参数 我们还用不到,后面课程中会用到
//占位参数 还可以有默认参数
void func(int a,int=10)
{
	cout << "this if func" << endl;
}
int main() {

	func(10);

	system("pause");
	return 0;
}

函数重载

#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,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;
}

函数重载注意事项

#include<iostream>
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)
{
	cout << "func2(int a)的调用" << endl;
}
void func2(int a,int b=10)
{
	cout << "func2(int a,int b)的调用" << endl;
}

int main() {

	/*int a = 10;
	func(a);*/

	//func(10);

	func2(10,20);//当函数重载碰到默认参数,出现二义性,报错,避免这种情况

	system("pause");
	return 0;
}

类和对象-封装

属性和行为作为主体

#include<iostream>
using namespace std;

//圆周率
const double PI = 3.14;

//设计一个圆类,求圆的周长
//圆求周长的公式:2*pi*半径
//封装、继承、多态,万事万物都称为对象

//代表设计一个类,类后面紧跟着的就是类名称
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<iostream>
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;
	//给s1对象 进行属性赋值操作
	s1.setName("张三");
	s1.setId(1);
	//显式学生信息

	s1.showStudent();

	Student s2;
	s2.m_Name = "李四";
	s2.m_Id = 2;
	s2.showStudent();

	system("pause");
	return 0;
}


访问权限

#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;//私有权限内容,类外访问不到

	p1.func();

	system("pause");
	return 0;
}


成员属性私有化

#include<iostream>
using namespace std;

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

//设计人类
class Person
{
public:

	//设置姓名
	void setName(string name)
	{
		m_Name = name;
	}
	//获取姓名
	string getName()
	{
		return m_Name;
	}
	//获取年龄 可读可写 如果想修改(年龄的范围必须0^150之间)
	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)
	{

	}

private:
	//姓名  可读可写
	string m_Name;
	//年龄  只读
	int m_Age;
	//情人  只读
	string m_Lower;
};
int main() {

	Person p;
	p.setName("张三");

	cout << "姓名为:" << p.getName() << endl;

	//p.setAge(18);//错误 只读状态
	p.setAge(8);
	cout << "年龄为:" <<p.getAge() << endl;


	//设置情人为苍井女士
	p.setLover("苍井");
	//cout << "情人为:" << p.getLover() << endl;//错误,外界访问不到

	system("pause");
	return 0;
}


C++中class和struct的区别

#include<iostream>
using namespace std;

//默认的访问权限不同
class C1
{
	int m_A;//默认权限 是 私有
};

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

int main() {

	//struct和class区别
	//struct 默认权限是 公共public
	//class 默认权限是 私有 private
	C1 c1;
	//c1.m_A = 100; //在class里默认权限 私有,因此类外不可以访问

	C2 c2;
	c2.m_A = 100;//在struct默认的权限为公共,因此可以访问

	system("pause");
	return 0;
}


设计案例1-立方体类

#include<iostream>
using namespace std;


//立方体类设计
//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_W * m_H + 2 * m_L * m_H;
	}
	//获取立方体体积
	int calculateV()
	{
		return m_H * m_L * m_W;
	}

	//利用成员函数判断两个立方体是否相等
	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;
	}
}
int main() {

	//创建立方体对象
	Cube c1;
	c1.setL(10);
	c1.setH(10);
	c1.setW(10);

	//600
	cout <<"c1的面积为:"<<c1.calculateS() << endl;
	//1000
	cout << "c1的体积为:"<<c1.calculateV() << 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;
	}

	//利用成员函数判断
	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"point.h"
#include"circle.h"

//点到圆心的距离==半径 点再圆上
//点到圆心的距离>半径  点再圆外
//点到圆心的距离<半径  点再圆内
//点和圆的关系

//点类
//class Point
//{
//public:
//	//设置x
//	void setX(int X)
//	{
//		m_X = X;
//	}
//	//获取x
//	int getX()
//	{
//		return m_X;
//	}
//	void setY(int Y)
//	{
//		m_X = Y;
//	}
//	//获取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.setX(10);

	//判断关系

	isInCircle(c, p);

	system("pause");
	return 0;
}


类和对象-对象特性

构造函数和析构函数

#include<iostream>
using namespace std;

//对象的初始化和清理
//1、构造函数 进行初始化操作
class Person
{
public:
	//构造函数语法:类名(){}
	//1、构造函数,没有返回值也不写void
	//2、函数名称与类名相同
	//3、构造函数可以有参数,因此可以发生重载
	//4、程序在调用对象时候会自动调用构造,无须手动调用,而且只会调用一次
	Person()
	{
		cout << "构造函数的调用" << endl;
	}
	//2、析构函数 进行清理的操作
	//析构函数语法:类名(){}
	//1、析构函数,没有返回值也不写void
	//2、函数名称与类名相同,在名称前加上符号~
	//3、析构函数不可以有参数,因此不可以发生重载
	//4、程序在对象销毁前会自动调用析构,无须手动调用,而且只会调用一次
	~Person()
	{
		cout << "Person的析构函数调用" << endl;
	}
};


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

	system("pause");
	return 0; 
}

函数的分类及调用

#include<iostream>
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、括号法
	//Person p1;//默认构造函数调用
	//Person p2(10);//有参构造函数
	//Person p3(p2);//拷贝构造函数

	//注意事项
	//调用默认构造函数时候,不要加()
	//因为下面这行代码,编译器会认为是一个函数的声明,不会认为在创建对象
	//Person p1();

	//void func();

	//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;
}

拷贝构造函数调用时机

#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;
}

构造函数调用规则

#include<iostream>
using namespace std;

//构造函数的调用规则
//1、创建一个类,C++编译器会给每个类都添加至少3个函数
//1、默认构造函数
//2、默认析构函数
//3、默认拷贝构造函数,对属性进行值拷贝

//2、如果我们写了有参构造函数,编译器就不再提供默认构造,依然提供拷贝构造
class Person
{
public:

	/*Person()
	{
		cout << "Person的默认构造函数调用" << endl;
	}*/

	/*Person(int age)
	{
		cout << "Person的有参构造函数调用" << endl;
		m_Age = age;
	}*/
	Person(const Person& p)
	{
		m_Age = p.m_Age;
		cout << "Person的有拷贝构造函数调用" << endl;
	}
	~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(18);

	Person p2(p);

	cout << "p2的年龄为:" << p2.m_Age << endl;
}
//如果用户定义有参构造函数,c++不再提供默认无参构造,但是会提供默认拷贝构造
//如果用户定义拷贝构造函数,c++不会再提供其他构造函数


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);

	cout << "p2的年龄为:" << p2.m_Age << "身高为:" <<* p2.m_Height << endl;
}

int main()
{

	test01();


	system("pause");
	return 0;
}

初始化列表

#include<iostream>
using namespace std;

//构造函数():属性1(值1),属性2(值2)

//初始化列表
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;
	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;
}

类对象作为类成员

#include<iostream>
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:
	//Phone m_phone=pName 隐式转换法 
	Person(string name, string pName) :m_Name(name),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;
}

静态成员-静态成员函数

#include<iostream>
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 fun2调用" << 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;
}

成员变量和成员函数分开存储

#include<iostream>
using namespace std;

//成员变量和成员函数是分开存储的
class Person
{
	int m_A;//非静态成员变量  属于类的对象上

	static int m_B;//静态成员变量  不属于类对象上

	void func(){}//非静态成员函数 不属于类对象上

	static void func2(){}//静态成员函数 不属于类的对象上
};
int Person::m_B = 0;

void test01()
{
	Person p;
	//空对象占用内存空间为: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;
}

this指针的用途

#include<iostream>
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;
}
//this指针是隐含每一个非静态成员函数内的一种指针
//this指针不需要定义,直接使用即可

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

int main()
{
	//test01();

	test02();

	system("pause");
	return 0;
}

空指针访问成员函数

#include<iostream>
using namespace std;

//空指针调用成员函数
class Person
{
public:
	void showClassName()
	{
		//报错原因是因为传入的指针是为NULL
		if (this == NULL)
		{
			return;
		}
		cout << "this is Person class" << endl;
	}

	void showPersonAge()
	{
		cout << "age=" << this->m_Age << endl;
	}

	int m_Age;
};

void test01()
{
	Person* p = NULL;

	p->showClassName();

	p->showPersonAge();
}

int main()
{


	system("pause");
	return 0;
}

const修饰成员函数

#include<iostream>
using namespace std;
//const修饰成员函数
//常函数:
//成员函数后加const后我们称为这个函数为常函数
//常函数内不可以修改成员属性
//成员属性声明时加关键字mutable后,在常函数中依然可以修改
class Person
{
public:

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

类和对象-友元

友元类

#include<iostream>
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 << "好基友类正在访问:" << building->m_SittingRoom << endl;
	cout << "好基友类正在访问:" << building->m_BedRoom << endl;
}

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

int main()
{
	test01();

	system("pause");

	return 0;
}

全局函数做友元

#include<iostream>
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 << "好基友全局函数 正在访问:" << building->m_SittingRoom << endl;

	cout << "好基友全局函数 正在访问:" << building->m_BedRoom << endl;
}

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

int main()
{

	test01();

	system("pause");
	return 0;
}

成员函数做友元

#include<iostream>
using namespace std;

//成员函数做友元
class Building;
class GoodGay
{
public:

	GoodGay();

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

	Building* building;
};

class Building
{
	//告诉编译器 GoodGay类下的visit成员函数作为本类的好朋友,可以访问私有成员
	friend void GoodGay::visit();
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 << "visit2 函数正在访问:" << 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;
}

加号运算符重载

#include<iostream>
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:
	//利用成员函数重载 左移运算符  p.operator<<(cout) 简化版本p<<cout
	//不会利用成员函数重载<<运算符,因为无法实现 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;
	return cout;
}

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


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

	cout << p << "hello world"<<endl;

}


int main()
{


	test01();

	system("pause");
	return 0;
}

左移运算符重载

#include<iostream>
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:
	//利用成员函数重载 左移运算符  p.operator<<(cout) 简化版本p<<cout
	//不会利用成员函数重载<<运算符,因为无法实现 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;
	return cout;
}

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


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

	cout << p << "hello world"<<endl;

}


int main()
{


	test01();

	system("pause");
	return 0;
}

递增运算符重载

#include<iostream>
using namespace std;

//通过重载递增运算符,实现自己的整型数据
//递增运算符重载++

//重载递增运算符

//自定义整型
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;
	cout << myint << endl;
}
void test02()
{
	MyInteger myint;

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


int main()
{


	test02();


	system("pause");
	return 0;
}

赋值运算符重载

#include<iostream>
using namespace std;

//赋值运算符重载
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 << "p2的年龄为:" << *p2.m_Age << endl;

	cout << "p3的年龄为:" << *p3.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;
}

关系运算符重载

#include<iostream>
using namespace std;

//重载关系运算符

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("Jerry", 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;
}

函数调用运算符重载

#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 world");//由于使用起来非常类似于函数调用,因此称为仿函数

	MyPrint02("hello world");
}

//仿函数非常灵活,没有固定的写法
//加法类

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;
}

类和对象-继承

基本语法

#include<iostream>
using namespace std;

//普通实现页面

//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;
//	}
//};
//
//void test01()
//{
//	cout << "Java下载视频页面如下:" << endl;
//	Java ja;
//	ja.header();
//	ja.footer();
//	ja.left();
//	ja.content();
//
//	cout << "------------" << endl;
//	cout << "Python下载视频页面如下:" << endl;
//	Python py;
//	py.header();
//	py.footer();
//	py.left();
//	py.content();
//
//	cout << "--------" << endl;
//	cout << "C++下载视频页面如下:" << endl;
//	CPP c;
//	c.header();
//	c.footer();
//	c.left();
//	c.content();
//}

//继承实现页面

//公共页面类
class BasePage
{
public:
	void header()
	{
		cout << "首页、公开课、登录、注册...(公共头部)" << endl;
	}
	void footer()
	{
		cout << "帮助中心、交流合作、站内地图...(公共底部)" << endl;
	}
	void left()
	{
		cout << "Java、Python、C++、...(公共分类列表)" << endl;
	}
};

//继承的好处:减少重复代码
//语法:class 子类:继承方式 父类
//子类 也称为 派生类
//父类 也称为 基类

//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 << "CPP学科视频" << endl;
	}
};
void test01()
{
	cout << "Java下载视频页面如下:" << endl;
	Java ja;
	ja.header();
	ja.footer();
	ja.left();
	ja.content();

	cout << "------------" << endl;
	cout << "Python下载视频页面如下:" << endl;
	Python py;
	py.header();
	py.footer();
	py.left();
	py.content();

	cout << "--------" << endl;
	cout << "C++下载视频页面如下:" << endl;
	CPP c;
	c.header();
	c.footer();
	c.left();
	c.content();
}

int main()
{
	test01();

	system("pause");
	return 0;
}

继承方式

#include<iostream>
using namespace std;


//继承方式

//公共继承
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 = 100;//父类中私有成员 子类访问不到
	}
};

void test02()
{
	Son2 s1;
	//s1.m_A = 1000;//在Son2中 m_A变为保护权限,因此类外访问不到
	//s1.m_B = 1000;//在Son2中 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 = 100;//父类中私有成员,子类访问不到
	}
};

void test03()
{
	Son3 s1;
	//s1.m_A = 1000;//到Son3中 变为 私有成员 类外访问不到
	//s1.m_B = 1000;//到Son3中 变为 私有成员 类外访问不到
};

class GrandSon3 :public Son3
{
public:
	void func()
	{
		//m_A = 1000;//到了Son3中 m_A变为私有,即使是儿子,也是访问不到
		//m_B = 1000;//到了Son3中 m_B变为私有,即使是儿子,也是访问不到
	}
};

int main()
{


	system("pause");
	return 0;
}

继承中的对象模型

#include<iostream>
using namespace std;

//继承中的对象模型

class Base
{
public:
	int m_A;
protected:
	int m_B;
private:
	int m_C;
};

class Son :public Base
{
public:
	int m_D;
};

//利用开发人员命令提示工具查看对象类型
//跳转盘符  F:
//跳转文件路径 cd 具体路径下
//查看命名
//cl/d1 reportSingleClassLayout类名 文件名

void test01()
{
	//4  12  16
	//父类中所有非静态成员属性都会被子类继承下去
	//父类中私有成员属性 是被编译器给隐藏了,因此是访问不到,但是确实被继承下去了
	cout << "size of Son=" << sizeof(Son) << endl;
}

int main()
{
	test01();


	system("pause");
	return 0;
}

构造和析构顺序

#include<iostream>
using namespace std;

//继承中的构造和析构顺序
class Base
{
public:
	Base()
	{
		cout << "Base的构造函数!" << endl;
	}
	~Base()
	{
		cout << "Base析构函数!" << endl;
	}

};

class Son :public Base
{
public:
	Son()
	{
		cout << "Son的构造函数!" << endl;
	}
	~Son()
	{
		cout << "Son析构函数!" << endl;
	}
};

void test01()
{
	//Base b;
	//继承中的构造和析构顺序如下:
	//先构造父类,再构造子类,析构的顺序与构造的顺序相反
	Son s;
}

int main()
{

	test01();

	system("pause");
	return 0;
}

同名成员处理

#include<iostream>
using namespace std;

//继承中同名成员处理
class Base
{
public:
	Base()
	{
		m_A = 100;
	}
	int m_A;
	void func()
	{
		cout << "Base-func()调用" << endl;
	}
	void func(int a)
	{
		cout << "Base-func(int a)调用" << endl;
	}
};

class Son :public Base
{
public:

	Son()
	{
		m_A = 200;
	}
	int m_A;
	void func()
	{
		cout << "Son-func()调用" << endl;
	}
};
//同名成员属性处理
void test01()
{
	Son s;
	cout << "Son.m_A=" << s.m_A << endl;
	//如果通过子类对象 访问到父类中同名成员,需要加作用域
	cout << "Son.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;
}

同名静态成员处理

#include<iostream>
using namespace std;

//继承中的同名静态成员处理方式

class Base
{
public:

	static int m_A;

	static void func()
	{
		cout << "Base-static void func()" << endl;
	}
	static void func(int a)
	{
		cout << "Base-static void func(int a)" << endl;
	}
};
int Base::m_A = 100;

class Son:public Base
{

public:
	static int m_A;

	static void func()
	{
		cout << "Son-static void func()" << endl;
	}
	static void func(int a)
	{
		cout << "Son-static void func(int a)" << endl;
	}
};
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;

	//2、通过类名访问
	cout << "通过类名访问:" << endl;
	cout << "Son下 m_A=" << Son::m_A << endl;
	//第一个::代表通过类名方式访问  第二个::代表访问父类作用域下
	cout << "Base下 m_A=" << Son::Base::m_A << endl;
}
//同名静态成员函数
void test02()
{
	//1、通过对象访问
	cout << "通过对象访问" << endl;
	Son s;
	s.func();
	s.Base::func();

	//2、通过类名访问
	cout << "通过类名访问" << endl;
	Son::func();
	Son::Base::func();

	//子类出现和父类同名静态成员函数,也会隐藏父类中所有同名成员函数
	//如果想访问父类中被隐藏同名成员,需要加作用域
	Son::Base::func(100);
}

int main()
{
	//test01();

	test02();


	system("pause");
	return 0;
}

继承语法

#include<iostream>
using namespace std;

//多继承语法

class Base1
{
public:

	Base1()
	{
		m_A = 100;
	}

	int m_A;
};
class Base2
{
public:

	Base2()
	{
		m_A = 200;
	}

	int m_A;
};

//子类 需要继承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 << "Base1::m_A=" << s.Base1::m_A << endl;
	cout << "Base2::m_A=" << s.Base2::m_A << endl;
}

int main()
{
	test01();


	system("pause");
	return 0;
}

菱形继承问题

#include<iostream>
using namespace std;

//动物类

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 = 28;
	//当菱形继承,两个父类拥有相同数据,需要加以作用域区分
	cout << "st.Sheep::m_Age=" << st.Sheep::m_Age << endl;
	cout << "st.Tuo::m_Age=" << st.Tuo::m_Age << endl;
	cout << "st.m_Age="<<st.m_Age << endl;

	//这份数据我们知道 只要有一份就可以 菱形继承导致数据有两份 资源浪费
}

int main()
{
	test01();

	system("pause");
	return 0;
}

类和对象-多态

多态的基本语法

#include<iostream>]
using namespace std;
//静态多态 函数重载和运算符重载
//动态多态 派生类和虚函数实现运行时多态
//静态多态的函数地址早绑定-编译阶段确定函数地址
//动态多态的函数地址晚绑定


//动物类
class Animal
{
public:
	//虚函数
	virtual void speak()
	{
		cout << "动物在说话" << endl;
	}
};

//猫类
class Cat :public Animal
{
public:
	void speak()
	{
		cout << "小猫在说话" << endl;
	}
};

//狗类
class Dog :public Animal
{
public:
	//重写 函数返回值类型  函数名  参数列表 完全相同
	void speak()
	{
		cout << "小狗在说话" << endl;
	}
};

//执行说话的函数
//地址早绑定 在编译阶段确定函数地址
//如果想执行让猫说话,那么这个函数的地址就不能提前绑定,需要在运行阶段进行绑定,地址晚绑定

//动态多态满足条件
//1、有继承关系
//2、子类重写父类的虚函数

//动态多态使用
//父类的指针或者引用 执行子类对象

void doSpeak(Animal& animal)//Animal & animal=cat;
{
	animal.speak();
}

void test01()
{
	Cat cat;
	doSpeak(cat);

	Dog dog;
	doSpeak(dog);
}
int main()
{

	test01();

	system("pause");
	return 0;
}

多态的原理剖析

#include<iostream>
using namespace std;

//动物类
class Animal
{
public:
	//虚函数
	//vfptr-虚函数(表)指针 v-virtual f-function ptr-pointer
	//vftable-虚函数表  v-virtual f-function table-table
	//当子类重写父类的虚函数,子类中的虚函数表 内部 会替换称子类的虚函数地址 
	virtual void speak()
	{
		cout << "动物在说话" << endl;
	}
};

//猫类
class Cat :public Animal
{
public:
	/*void speak()
	{
		cout << "小猫在说话" << endl;
	}*/
};

//狗类
class Dog :public Animal
{
public:
	//重写 函数返回值类型  函数名  参数列表 完全相同
	void speak()
	{
		cout << "小狗在说话" << endl;
	}
};
void doSpeak(Animal& animal)//Animal & animal=cat;
{
	animal.speak();
}

void test01()
{
	Cat cat;
	doSpeak(cat);

	Dog dog;
	doSpeak(dog);
}

void test02()
{
	cout << "sizeof Animal=" << sizeof(Animal) << endl;
}
int main()
{

	test02();

	system("pause");
	return 0;
}

案例1-计算器类

#include<iostream>
using namespace std;

//分别利用普通写法和多态技术实现计算器

//普通写法
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 = 10;

	cout << abc->m_Num1 << "-" << abc->m_Num2 << "=" << abc->getResult() << endl;
	delete abc;

	//减法运算
	abc = new MulCalculator;
	abc->m_Num1 = 10;
	abc->m_Num2 = 10;

	cout << abc->m_Num1 << "*" << abc->m_Num2 << "=" << abc->getResult() << endl;
	delete abc;
}



int main()
{

	//test01();

	test02();

	system("pause");
	return 0;
}

纯虚函数和抽象类

#include<iostream>
using namespace std;

//纯虚函数和抽象类
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;
}

案例2-制作饮品

#include<iostream>
using namespace std;

//多态案例2 制作饮品
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
{
	//煮水
	virtual void Boil() 
	{
		cout << "煮农夫山泉" << endl;
	}

	//冲泡
	virtual void Brew() 
	{
		cout << "冲泡咖啡" << endl;
	}

	//倒入杯中
	virtual void PourInCup()
	{
		cout << "倒入杯中" << endl;
	}

	//加入辅料
	virtual void PutSomething()
	{
		cout << "加入糖和牛奶" << endl;
	}
};

//制作茶叶
class Tea :public AbstractDringking
{
	//煮水
	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;//释放
}

void test01()
{
	//制作咖啡
	doWork(new Coffee);


	cout << "------------" << endl;
	//制作茶叶
	doWork(new Tea);
}

int main()
{

	test01();

	system("pause");
	return 0;
}

虚析构和纯虚析构

#include<iostream>
using namespace std;

//虚析构和纯虚析构

class Animal
{
public:

	Animal()
	{
		cout << "Animal构造函数调用" << endl;
	}

	//利用虚析构可以解决 父类指针释放子类对象时不干净的问题
	//virtual ~Animal()
	//{
	//	cout << "Animal虚析构函数调用" << endl;
	//}
	纯虚析构 需要声明也需要实现
	//有了纯虚析构,这个类也属于抽象类,无法实例化对象
	virtual ~Animal() = 0;

	//纯虚函数
	virtual void speak() = 0;
};

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;
}

案例3-电脑组装需求分析

#include<iostream>
using namespace std;


//抽象不同零件类
//抽象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();
	}

	//提供析构函数 释放3个电脑零件
	~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 << "Intel的CPU开始计算了!" << endl;
	}
}; 

class IntelVideoCard :public VideoCard
{
public:
	virtual void display()
	{
		cout << "Intel的显卡开始显示了!" << endl;
	}
};

class IntelMemory :public Memory
{
public:
	virtual void storage()
	{
		cout << "Intel的内存条开始存储了!" << endl;
	}
};

class LenovoCPU :public CPU
{
public:
	virtual void calculate()
	{
		cout << "Lenovo的CPU开始计算了!" << endl;
	}
};

class LenovoVideoCard :public VideoCard
{
public:
	virtual void display()
	{
		cout << "Lenovo的显卡开始显示了!" << endl;
	}
};

class LenovoMemory :public Memory
{
public:
	virtual void storage()
	{
		cout << "Lenovo的内存条开始存储了!" << endl;
	}
};

void test01()
{
	//第一台电脑零件
	CPU* intelCpu = new IntelCPU;
	VideoCard* intelCard = new IntelVideoCard;
	Memory* intelMem = new IntelMemory;

	cout << "第一台电脑开始工作" << endl;
	//创建第一台电脑
	Computer* computer1 = new Computer(intelCpu, intelCard, intelMem);
	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;
}

C++文件操作

文本文件-写文件

#include<iostream>
using namespace std;
#include<fstream>//头文件包含

//文本文件 写文件

void test01()
{
	//1、包含头文件 fstream

	//2、创建流对象

	ofstream ofs;

	//3、指定打开方式
	ofs.open("test.txt", ios::out);

	//4、写内容
	ofs << "姓名:张三" << endl;
	ofs << "性别:男" << endl;
	ofs << "年龄:18" << endl;

	//5、关闭文件
	ofs.close();
}


int main()
{

	test01();

	system("pause");
	return 0;
}

文本文件-读文件

#include<iostream>
using namespace std;
#include<string>
#include<fstream>

//文本文件 读文件
void test01()
{

	//1、包含头文件

	//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)//EOF end of file
	{
		cout << c; 
	}

	//5、关闭文件
	ifs.close();
}

int main()
{
	 
	test01();

	system("pause");
	return 0;
}

二进制文件-写文件

#include<iostream>
using namespace std;
#include<fstream>


//二进制文件 写文件
class Person
{
public:

	char m_Name[64];//姓名
	int m_Age;//年龄
};

void test01()
{
	//1、包含头文件
	//2、创建流对象
	ofstream ofs;
	//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;
}

二进制文件-读文件

#include<iostream>
using namespace std;
#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;

	ifs.read((char*)&p, sizeof(Person));

	cout << "姓名:" << p.m_Name << "年龄:" << p.m_Age << endl;
	//5、关闭文件
	ifs.close();
}


int main()
{
	test01();


	system("pause");
	return 0;
}

(代码学习取自bilibili:黑马程序员匠心之作|C++教程从0到1入门编程,学习编程不再难)

  • 3
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

深渊潜航

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值