C++面向对象+案例(附代码)

C++核心编程

接上一篇 c++基础入门 .

文章目录

1.内存分区模型

c++在执行时将内存划分为四个区:

  • 代码区:存放函数体的二进制代码,有操作系统进行管理的
  • 全局区:存放全局变量、静态变量和常量
  • 栈区:由编译器自动分配和释放,存放函数的参数值,局部变量
  • 堆区:由程序员分配和释放

1.1 程序运行前

代码区:

​ 存放CPU执行的机器指令

​ 代码是共享

​ 代码是只读

全局区:

​ 存放:

  • 全局变量
  • 静态变量:全局和局部
  • 常量:字符串常量、其它常量(由const修饰的全局变量))

​ 该区域的数据在程序结束后由系统释放

1.2程序运行后

栈区

​ 由编译器自动分配和释放,存放函数的参数值和局部变量

​ 注意:不要放回局部变量的地址

堆区

​ 由程序员分配释放,若程序员不释放,在程序运行结束时有系统释放

​ 在c++中主要利用new在堆区开辟内存

1.3new操作符

C++中的new操作符在堆区开辟数据

堆区开辟的数据,由程序员手动开辟,手动释放,释放利用操作符delete

数组释放需要在delete后加[]

2.引用(略)

此部分未做笔记

3.函数提高

此部分和python函数有想通之处,除了重载(懒)

4.类和对象

C++面线对象的三大特性:封装、继承、多态

4.1封装

4.1.1 封装的意义

封装是C++面向对象三大特性之一

封装的意义一:

  • 将属性和行为作为一个整体,表现生活中的事物
  • 加属性和行为加以权限控制
#include <iostream>
#include <string>
using namespace std;

const double PI = 3.14;

// 圆类
class Circle
{
public:  // 公共权限
	// 属性
	int radius;  // 半径

	// 行为
	double calulateZC()
	{
		return 2 * radius * PI;
	}
};

// 学生类
class Student
{
public:
	// 属性
	string sname;
	string sid;
	// 行为:打印学生信息

	// 获取SID
	void setID(string id)
	{
		sid = id;
	}
	// 获取SName
	void setName(string name)
	{
		sname = name;
	}
	//打印学生信息
	void printInfo()
	{
		cout << "学号:" << sid << "\t姓名:" << sname << endl;
	}

};


int main()
{
	// 实例化圆类
	Circle c1;
	// 给实例属性赋值
	c1.radius = 3;
	// 调用实例类中封装的行为--计算圆周长
	cout << "半径为" << c1.radius << "的圆,周长为" << c1.calulateZC() << endl;

	// 实例化学生类
	Student stu1;
	// 学生类属性赋值
	stu1.sid = "10086";
	stu1.sname = "移动";
	// 调用实例用有的方法
	stu1.printInfo();

	Student stu2;
	stu2.setID("110");
	stu2.setName("刑警队");
	stu2.printInfo();
	return 0;
}

封装意义二:

类在设计时,可以把属性和行为放在不同的权限下加以控制

访问权限三种:

  • public:公共权限——类内外均可以访问
  • protected:保护权限——类内可以访问,类外不能访问(子类可以访问父类的成员)
  • privite:私有权限——类内可以访问,类外不能访问(子类不可以访问父类的成员)
4.1.2struct与class区别

区别:

  • struct默认权限为公有
  • class 默认权限为私有
4.1.3 成员属性设置为私有

优点:

  1. 将所有成员属性设置为私有,可以自己控制读写权限
  2. 对于写权限,我们可以检测数据有效性
#include <iostream>
#include <string>
using namespace std;

class Person
{
public:
	
	// 修改私有成员name的接口
	void setName(string name)
	{
		p_name = name;
	}
	// 获取私有成员name的接口
	string getName()
	{
		return p_name;
	}
	// 获取私有成员age的接口
	int getAge()
	{
		return p_age;
	}
	// 设置私有成员money值的接口,只能增加
	void setMoney(int money)
	{
		if (money < 0) 
		{
			cout << "\a不可为负数!" << endl;
		}
		else
		{
			p_money += money;
		}
	}
private:
	string p_name;  // 权限:可读可写
	int p_age = 18;  // 权限:可读  初始化为0
	int p_money = 0;  // 权限:可写
};

int main()
{
	Person p1;
	p1.setName("张三");
	cout << p1.getName() << endl;
	cout << p1.getAge() << endl;
	p1.setMoney(-9);
	return 0;
}
案例:设计立方体类

设计立方体类

求出立方体的面积和体积

分别用全局函数和成员函数判断两个立方体是否相等

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

class Cube
{
private:
	int length = 0;
	int width = 0;
	int height = 0;

public:
	// 设置长宽高
	void set_LWH(int l, int w, int h)
	{
		length = l;
		width = w;
		height = h;
	}
	// 计算面积
	int caculateArea()
	{
		if ((length == 0) || (width == 0) || (height == 0))
		{
			cout << "\aYou must set the length, width and height of cube by function: set_LWH ." << endl;
			return 0;
		}
		else
		{
			return 2*(length * width +  width * height + length * height);
		}
	}
	// 计算体积
	int caculateVolume()
	{
		if ((length == 0) || (width == 0) || (height == 0))
		{
			cout << "\aYou must set the length, width and height of cube by function: set_LWH ." << endl;
			return 0;
		}
		else
		{
			return length * width * height;
		}
	}

	void isSame(Cube &c)
	{
		if (caculateVolume() == c.caculateVolume())
		{
			cout << "两个立方体体积相等!" << endl;

		}
		else
		{
			cout << "两个立方体体积不相等!" << endl;
		}
	}
};

void isEqual(Cube &c1, Cube &c2)
{
	if (c1.caculateVolume() == c2.caculateVolume())
	{
		cout << "两个立方体体积相等!" << endl;
	}
	else
	{
		cout << "两个立方体体积不相等!" << endl;
	}
}

int main()
{
	Cube cube1;
	cube1.set_LWH(2, 3, 4);
	cout << "面积为:" << cube1.caculateArea() << endl;
	cout << "体积为:" << cube1.caculateVolume() << endl;

	Cube cube2;
	cube2.set_LWH(2, 4, 3);
	cout << "面积为:" << cube2.caculateArea() << endl;
	cout << "体积为:" << cube2.caculateVolume() << endl;

	// 判断cube1与cube2体积是否相等
	// 全局方法
	isEqual(cube1, cube2);
	// 成员方法
	cube2.isSame(cube1);
	return 0;
}
案例:点和圆的关系
#include <iostream>
#include <string>
using namespace std;

// 点类
class Dot
{
private:
	int d_x;
	int d_y;
public:
	void set_x(int x)
	{
		d_x = x;
	}
	int get_x()
	{
		return d_x;
	}
	void set_y(int y)
	{
		d_y = y;
	}
	int get_y()
	{
		return d_y;
	}
};

// 圆类
class Circle
{
private:
	int c_r;
	Dot center;
public:
	void set_r(int r)
	{
		c_r = r;
	}
	int get_r()
	{
		return c_r;
	}
	void set_center(Dot d)
	{
		center = d;
	}
	Dot get_center()
	{
		return center;
	}
};


// 判断点与圆的位置关系
int relation(Circle &c, Dot &d)
{
	int value = (c.get_center().get_x() - d.get_x()) * (c.get_center().get_x() - d.get_x()) + 
		(c.get_center().get_y() - d.get_y()) * (c.get_center().get_y() - d.get_y());
	cout << value << "\t" << c.get_r()*c.get_r() << endl;
	if (value == (c.get_r() * c.get_r()))
	{
		return 0;
	}
	else if(value > (c.get_r() * c.get_r()))
	{
		return 1;
	}
	else
	{
		return -1;
	}

}
int main()
{
	Circle c;
	c.set_r(4);
	Dot center;
	center.set_x(0);
	center.set_y(0);
	c.set_center(center);

	Dot d;
	d.set_x(2);
	d.set_y(2);

	int res = relation(c, d);

	if (res == 0)
	{
		cout << "点在圆弧上。" << endl;
	}
	else if (res < 0)
	{
		cout << "点在圆内。" << endl;
	}
	else
	{
		cout << "点在圆外。" << endl;
	}
	return 0;
}

4.2对象的初始化与清理

4.2.1构造函数与析构函数

构造函数

  • 作用:类的初始化
  • 语法:类名 (){语句}
    • 没有返回值不写void
    • 函数名与类名相同
    • 构造函数可以有参数,因此可以发生重载
    • 程序在调用该对象时会自动调用构造函数,无需手动调用,而且只会调用一次

析构函数

  • 作用:类的清理
  • 语法:~类名 (){}
  • 特点:
    1. 没有返回值不写void
    2. 函数名与类名相同,在名称前加上~
    3. 析构函数不能有参数,因此不可以发生重载
    4. 程序在销毁该对象时会自动调用构造函数,无需手动调用,而且只会调用一次
#include <iostream>
#include <string>
using namespace std;

class Person
{
private:
	string p_name = "init";
	int p_age = 0;

public:
	Person() { cout << "这是person类初始化!" << endl; }
	~Person() { cout << "这是person类清理!" << endl; }
	void get()
	{
		cout << "name: " << p_name << "\tage: " << p_age << endl;
	}
};

int main()
{
	Person p1;
	p1.get();
	return 0;
}

4.2.2构造函数的分类及调用

两种分类方式:

  • 按参数分:有参构造和无参构造
  • 按类型分:普通构造和拷贝构造

三种调用方法:

  • 括号法 (调用默认构造函数时不能加括号
  • 显示法
  • 隐式转换法
#include <iostream>
#include <string>
using namespace std;

class Person
{
public:
	int age = 0;
	// 无参构造
	Person() { cout << "无参构造函数!" << endl; }
		
	// 有参构造
	Person(int a) { age = a; cout << "有参构造函数!" << endl; }

	// 拷贝构造 必须加const
	Person(const Person& p) 
	{ 
		age = p.age;
		cout << "这是拷贝构造函数!\t" << age << endl; 
	}
	~Person() { cout << "这是析构函数!" << endl; }
};


// 2.调用
// 2.1 括号法
void test01()
{
	Person p1;  // 无参
	Person p2(10);  // 有参
	Person p3(p2);  // 拷贝

}

// 2.2显示法
void test02()
{
	Person p1;  // 无参
	Person p2 = Person(10); // 有参
	Person p3 = Person(p2);  // 拷贝
}

// 2.3隐式转换法
void test03()
{
	Person p1; // 无参
	Person p2 = 10;  // 有参
	Person p3 = p2;  // 拷贝
}
int main()
{
	// test01();
	//test02();
	test03();
	return 0;
}
4.2.3拷贝函数调用时机
  • 使用一个已经创建完毕的对象来初始化一个新对象
  • 一个对象以值传递的方式给函数参数传值
  • 一个对象以值方式返回局部对象
4.2.4构造函数的调用规则
4.2.5浅拷贝与深拷贝

浅拷贝:

深拷贝:

4.2.6初始化列表
4.2.7类对象作为类成员
4.2.8静态成员

关键字:static

内外初始化 数据类型 类名::静态变量名称 = 值

访问方式:

  • 通过对象访问
  • 通过类名访问

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

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

在C++中,类内的成员变量和成员函数分开存储

只有非静态的成员变量才属于类的对象上

注意:空类占用内存空间为1;

#include <iostream>
using namespace std;

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

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

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

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

void test01()
{
	Person p;
	// 空对象占用内存空间为:1
	cout << sizeof(p) << endl;
}
int main()
{
	test01();
	return 0;
}
4.3.2 this指针

this指针指向被调用的成员函数所属的对象;

this指针是隐含每一个非静态成员函数内的一种指针;

this指针不需要定义,直接使用即可;

this指针用途:

  • 当形参和成员变量同名时,可用this指针来区分;
  • 在类的非静态成员函数中返回对象本身,可使用return *this
#include <iostream>
using namespace std;

class Person
{
public:
	int age;
	Person(int age) 
	{
		this->age = age;
	}
	Person& personAddAge(Person& p) // 以引用方式返回防止新建person类,且值也不会返回期望值
	{
		this->age += p.age;
		return *this;
	}
};

// 1.解决名称冲突
void test01()
{
	Person p(18);
	cout << p.age << endl;
}

// 2.返回对象本身用*this
void test02()
{
	Person p1(10);
	Person p2(12);
	p2.personAddAge(p1).personAddAge(p1);
	cout << p2.age << endl;
}

int main()
{
	test02();
	return 0;
}
4.3.3 空指针调用成员函数

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

	#include <iostream>
using namespace std;

class Person
{
public:
	void showClsName() { cout << "Person class" << endl; }
	void showClsAge() 
	{ 
		if (this == NULL) // 防止程序崩溃
		{
			return;
		}
		cout << "age = " << this->m_age << endl; 
	}
	int m_age;
};

int main()
{
	Person* p = NULL;
	p->showClsName();
	p->showClsAge();
	return 0;
}
4.3.4 const修饰成员函数

常函数

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

常对象

  • 声明对象前加const称该对象为常对象
  • 常对象只能调用函数
#include <iostream>
using namespace std;

class Person
{
public:
	void showCls() const // ==> const Pesron * const this
	{
		// this->m_age = 100;  // 不可修改指针指向的值
		// this = NULL;  // 也不可修改地址指向
		this->m_height = 100.0;  // 可修改
	}

	void showAge() {}

	int m_age;
	mutable float m_height; // 特殊变量,即在常函数中也可修改这个值,加mutable关键字
};

int main()
{
	Person p;
	p.showCls();

	const Person p1;
	// p1.m_age = 20; 不可修改
	p1.m_height = 20.0;  // 可修改,因为类定义里该变量前加了mutable
	
	// 常对象只能调用常函数
	p1.showCls();
	// p1.showAge(); // 常对象不可调用普通成员函数
	return 0;
}

4.4 友元

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

关键字:friend

友元的三种实现:

  • 全局函数作友元
  • 类作友元
  • 成员函数作友元
4.4.1 全局函数作友元

语法:friend 函数声明;

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

// 房屋类
class Building
{
	// goodgay全局函数是Building好朋友,可以访问类中的私有成员 语法:friend + 函数申明;
	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;
}

int main()
{
	Building building;
	goodgay(&building);
	return 0;
}
4.4.2 类作友元

语法:friend class 类名;

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

class Building;
class GoodGay
{
	Building* building;
public:
	GoodGay();
	void visit();  // 参观函数 访问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;
}
int main()
{
	GoodGay gg;
	gg.visit();
	return 0;
}
4.4.3 成员函数作友元
#include <iostream>
#include <string>
using namespace std;

class Building;

// 基友类
class GoodGay
{
public:
	Building* building;
public:
	GoodGay();
	void visit_init();  
	void visit_friend();  // 让该函数作为Building类的友元函数
};

// 建筑类
class Building
{
    // 成员函数作友元
	friend void GoodGay::visit_friend();

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

Building::Building()
{
	this->m_BedRoom = "卧室";
	this->m_SittingRoom = "客厅";
}

// 类外实现成员函数
GoodGay::GoodGay()
{
    // new 一个Building对象初始化building
	this->building = new Building;
}

void GoodGay::visit_init()
{
	cout << "成员函数正在访问:" << building->m_SittingRoom << endl;
	//cout << "成员函数正在访问:" << building->m_BedRoom << endl; // 报错
}

void GoodGay::visit_friend()
{
	cout << "好基友成员函数正在访问:" << building->m_SittingRoom << endl;
	cout << "好基友成员函数正在访问:" << building->m_BedRoom << endl;
}

int main()
{
	GoodGay gg;
	gg.visit_friend();
	gg.visit_init();
	return 0;
}

4.5 运算符重载

4.5.1加号运算符重载

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

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

class Person
{
public:
	int m_A;
	int m_B;
public:
	// Person();
	// Person(int a, int b) :m_A(a), m_B(b) {};

	// 局部重载运算符+
	/*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;
	}*/
};

// 全局重载运算符+
Person operator+ (Person& p1, Person &p2)
{
	Person temp;
	temp.m_A = p1.m_A + p2.m_A;
	temp.m_B = p1.m_B + p2.m_B;
	return temp;
}

// 重载
Person operator+ (Person& p1, int num)
{
	Person temp;
	temp.m_A = p1.m_A + num;
	temp.m_B = p1.m_B + num;
	return temp;
}
int main()
{
	Person p1;
	p1.m_A = 10;
	p1.m_B = 12;

	Person p2;
	p2.m_A = 12;
	p2.m_B = 10;

	Person p3 = p1 + p2;
	cout << p3.m_A << endl;
	cout << p3.m_B << endl;

	// 运算符重载,也可以发生函数重载
	Person p4 = p1 + 10;
	cout << p4.m_A << endl;
	cout << p4.m_B << endl;
	return 0;
}
4.5.2 左移运算符重载

作用:可以输出自定义数据类型

#include <iostream>
using namespace std;

class Person
{
};

// 只能利用全局函数重载左移运算符
ostream& operator<<(ostream &out, Person &p)
{
	out << "class Person";
	return out;
}
/*
int main()
{
	Person p;
	cout << p << endl;
	return 0;
}*/
4.5.3 递增运算符重载
#include <iostream>
using namespace std;

class Myinteger
{
	friend ostream& operator<<(ostream& cout, Myinteger myint);
private:
	int m_Num;
public:
	Myinteger() { m_Num = 0; }

	// 前置递增运算符++重载 返回引用是为了对一个数进行递增
	Myinteger& operator++()
	{
		++m_Num;

		return *this;
	}

	// 后置递增运算符++重载
	Myinteger operator++(int)
	{
		// 记录当前的值
		Myinteger temp = *this;

		// 加操作
		m_Num++;

		// 返回记录值
		return temp;

	}

	// 前置递减运算符--重载
	Myinteger& operator--()
	{
		--m_Num;
		return *this;
	}

	// 后置递减运算符--重载
	Myinteger operator--(int)
	{
		// 先记录原始值
		Myinteger temp = *this;
		
		// 减操作
		m_Num--;

		// 返回记录值
		return temp;
	}

};

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

int main()
{
	Myinteger myint;

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

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

	cout << --myint << endl;
	cout << myint << endl;

	cout << myint-- << endl;
	cout << myint << endl;
}
4.5.4赋值运算符重载

赋值运算符operator=,对属性进行值拷贝(浅拷贝),需要使用深拷贝避开这个坑

#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 (this->m_age != NULL)
		{
			delete m_age;
			m_age = NULL;
		}
		// 深拷贝
		m_age = new int(*p.m_age);
		return *this;
	}
	int *m_age;
};

int main()
{
	Person p1(18);
	Person p2(20);
	Person p3(30);
	p3 = p2 = p1;  // 赋值操作
	cout << *p1.m_age << endl;
	cout << *p2.m_age << endl;
	cout << *p3.m_age << endl;

	return 0;
}
4.5.5关系运算符重载
#include <iostream>
#include <string>
using namespace std;

class Person
{
public:
	string m_name;
	int m_age;
	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;
		}
		else return false;
	}
	bool operator!=(Person& p)
	{
		if (this->m_age != p.m_age || this->m_name != p.m_name)
		{
			return true;
		}
		else return false;
	}
	bool operator>(Person& p)
	{
		if (this->m_age > p.m_age)
		{
			cout << this->m_age << "\t" << p.m_age << endl;
			return true;
		}
		else return false;

	}
	bool operator<(Person& p)
	{
		if (this->m_age < p.m_age)
		{
			return true;
		}
		else return false;
	}
};

int main()
{
	Person p1("张三", 15);
	Person p2("张三", 15);

	if (p1 == p2)
	{
		cout << "p1和p2是相等的" << endl;
	}
	if (p1 != p2)
	{
		cout << "p1 与p2 不相等" << endl;
	}
	if (p1 > p2)
	{
		cout << p1.m_name << "的年龄比" << p2.m_name << "大" << endl;
	}
	if (p1 < p2)
	{
		cout << p1.m_name << "的年龄比" << p2.m_name << "小" << endl;
	}
}
4.5.6 函数调用运算符重载
  • 函数调用运算符()也可以重载
  • 由于重载后使用的方式非常像函数的调用,因此也称为仿函数
  • 仿函数没有固定写法,非常灵活
#include <iostream>
#include <string>
using namespace std;

class MyPrint 
{
public:
	void operator() (string test)
	{
		cout << test << endl;
	}
};

int main()
{
	MyPrint myprint;
	myprint("hello world."); // 由于使用起来很像函数调用,因此也称为仿函数
	return 0;
}

4.6 继承

4.6.1继承的基本语法

语法:class 子类名:继承方式 父类名

#include <iostream>
using namespace std;

class Person
{
public:
	// Person() { cout << "Person类" << endl; }
	void showGrade(){cout << "年级:" << m_grade << "年级" << endl;}
private:
	int m_grade = 3;
};

class Man:public Person
{
public:
	void showSex() { cout << "男生" << endl; }
};

class Woman :public Person
{
public:
	void showSex() { cout << "女生" << endl; }

};

int main()
{
	Man m;
	m.showGrade();
	m.showSex();

	Woman w;
	w.showGrade();
	w.showSex();

	return 0;
}
4.6.2 继承方式
  • 公有继承
  • 保护继承
  • 私有继承
image-20201102190650172
#include <iostream>
using namespace std;

// 父类
class Base
{
public:
	int m_a;
protected:
	int m_b;
private:
	int m_c;
};

// 公共继承
class Son1:public Base
{
	void func()
	{
		m_a = 10;  // 父类中的公共权限成员到子类中依然是公共权限
		m_b = 10;  // 父类中的保护权限成员到子类中依然是保护成员
		// m_c = 10;  // 父类中的保护权限成员  子类访问不到
	}
};

// 保护继承
class Son2 :protected Base
{
	void func()
	{
		m_a = 10;  // 父类中的公共权限成员到子类中是保护权限
		m_b = 10;  // 父类中的保护权限成员到子类中依然是保护权限
		// m_c = 10;  // 父类中的私有成员访问不到
	}
};

// 私有继承
class Son3:private Base
{
	void func()
	{
		m_a = 100;  // 父类中的公共成员到子类中是私有权限
		m_b = 100;  // 父类中的保护权限成员到子类中是私有权限
		// m_c = 100;  // 父类中的私有成员访问不到
	}
};

class grandson3:public Son3
{
	void func()
	{
		// m_a; // m_a父类中已经通过私有继承将父父类中的公共权限变成私有权限,所以子类无法再继承该变量,m_b同理
	}
};
int main()
{
	// 公共继承
	Son1 s1;
	s1.m_a = 100;
	// s1.m_b = 100;  // 父类中的保护权限成员到子类中依然是保护成员, 在类外访问不到

	// 保护继承
	Son2 s2;
	// s2.m_a = 100;  // 保护继承方式下父类的公共权限到子类中就是保护权限,类外访问不到

	// 私有继承
	Son3 s3;
	// s3.m_a;  // 私有继承方式下父类的公共权限到子类中就是私有权限,类外访问不到
	// s3.m_b;  // 私有继承方式下父类的保护权限到子类中就是私有权限,类外访问不到
	return 0;
}
4.6.3 继承中的对象模型

问题:从父类继承过来的成员,哪些属于子类对象中?

父类中非静态成员变量均属于子类的成员,包括父类的私有成员

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

};

int main()
{
	Son s;
	cout << "Son类的大小: " << sizeof(s) << endl;
}

Son类的结构在开发人员工具可以查看

命令:先进入文件名所在文件夹,再cl /d1 reportSingleClassLayout类名 文件名

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-n7xQqkat-1605180981126)(C:\Users\11986\AppData\Roaming\Typora\typora-user-images\image-20201102194621532.png)]

4.6.4继承中的构造函数与析构顺序

子类继承父类后,当创建子类对象,也会调用父类的构造函数

问题:父类和子类的构造函数和析构函数的执行顺序是怎样的?

继承中的构造和析构顺序如下:

  • 先构造父类再构造子类,析构函数相反
#include <iostream>
using namespace std;

// 父类
class Base
{
public:
	int m_a;
protected:
	int m_b;
private:
	int m_c;
public:
	Base() { cout << "Base构造函数" << endl; }
	~Base() { cout << "Base析构函数" << endl; }


};

// 公共继承
class Son :public Base
{
public:
	int s_m_a;
	Son() { cout << "Son构造函数" << endl; }
	~Son() { cout << "Son析构函数" << endl; }


};

int main()
{
	Son s;
}

结果如下:

image-20201102195625275
4.6.5 继承同名成员处理方式

问题:当子类与父类出现同名的成员,如何通过子类对象,访问到子类或父类中同名的数据呢?

  • 访问子类同名成员 直接访问即可
  • 访问父类同名成员 需要加作用域

语法:实例化对象.父类名::变量名

#include <iostream>
using namespace std;

// 父类
class Base
{
public:
	int m_a = 100;
	void show(){ cout << "Base-show func." << endl;}
	void show(int a){ cout << "Base-show func(int a)." << endl; }
	void unique() { cout << "Base unique func." << endl; }
};

// 公共继承
class Son :public Base
{
public:
	int m_a = 200;
	void show()
	{
		cout << "Son-show func." << endl;
	}

};

int main()
{
	Son s;

	// 同名属性处理
	cout << "Son下面的m_a, s.m_a = " << s.m_a << endl;
	cout << "Base下的m_a, s.Base::m_a = " << s.Base::m_a << endl;

	// 同名函数处理
	s.show();
	s.Base::show();
	// 父类中所有show()函数的重载都不能直接访问
	s.Base::show(100);

	// 与子类不同名的函数直接访问
	s.unique();
}

总结:

  1. 当子类与父类拥有同名的成员函数(包括重载),子类会隐藏父类中同名成员函数,加作用域可以访问到父类中同名函数
4.6.6继承同名静态函数成员处理方式

问题:继承中同名的静态成员在子类对象上如何进行访问?

静态成员和非静态成员出现同名,处理方式一致

  • 访问子类同名成员 直接访问即可
  • 访问父类同名成员 需要加作用域
#include <iostream>
#include <iomanip>
using namespace std;

// 父类
class Base
{
public:
	static int m_a;
	static void show(){ cout << "Base-show func." << endl;}
	static void show(int a){ cout << "Base static show func(int a)." << endl; }

};
int Base::m_a = 100;

// 公共继承
class Son :public Base
{
public:
	static int m_a;
	static void show()
	{ cout << "Son static show func." << endl; }

};
int Son::m_a = 200;

int main()
{
	Son s;

	cout << setw(50) << setfill('*') << "1.通过对象访问" << setw(40) << setfill('*') << "" << endl;
	// 1.通过对象访问
	// 静态同名属性处理
	cout << "Son下面的静态成员变量m_a, 通过对象访问, s.m_a = " << s.m_a << endl;
	cout << "Base下的静态成员变量m_a, 通过对象访问,s.Base::m_a = " << s.Base::m_a << endl;

	// 静态同名函数处理
	s.show();
	s.Base::show();
	// 父类中所有静态show()函数的重载都不能直接访问
	s.Base::show(100);

	cout << setw(50) << setfill('*')<< "2.通过类名访问" << setw(40) << setfill('*') << "" << endl;
	// 2.通过类名访问
	cout << "Son下面的静态成员变量m_a,通过类名访问, Son::m_a = " << Son::m_a << endl;
	cout << "Base下面的静态成员变量m_a, 通过类名访问, Son::Base::m_a = " << Son::Base::m_a << endl;
	Son::show();
	Son::Base::show();
	Son::Base::show(100);



}
4.6.7 多继承语法

C++允许一个类继承多个类

语法:class 子类 :继承方式 父类1,继承方式 父类2

注意:多继承可能会引发父类中有同名成员出现,需要加作用域区分。

C++开发中不建议使用多继承

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

class Mom_gene
{
public:
	// 受精卵携带基因类型
	string gene_type;
	Mom_gene() { gene_type = "细胞质基因 & 细胞核基因"; }
};

class Dad_gene
{
public:
	string gene_type;
	Dad_gene() { gene_type = "细胞核基因"; }

};

class Child:public Mom_gene, public Dad_gene
{
public:

	// 染色体数
	int chromosomes_num;

	// 性别
	string m_sex;

	Child() { chromosomes_num = 46; m_sex = "男"; }
};

int main()
{
	Child child;
	// 当父类中出现同名成员需要加作用域区分
	cout << "孩子的基因中Mom携带的细胞基因有:" << child.Mom_gene::gene_type << endl;
	cout << "孩子的基因中Dad携带的细胞基因有:" << child.Dad_gene::gene_type << endl;

	return 0;
}
4.6.8菱形继承(钻石继承)

菱形继承概念:

  • 两个派送类继承同一个基类
  • 又有某个类同时继承于两个派生类

典型的菱形继承案例:

image-20201102212456659

菱形继承问题:

1.羊继承了动物的数据,驼同样继承了动物的数据,当草泥马使用数据时,就会产生二义性

2.草泥马继承自动物的数据继承了两份,但我们只需要一份。

解决方案:利用虚继承,继承之前加上关键字virtual变为虚继承

没有虚继承

// 动物类
class Animal
{
public:
	int m_age;
};

// 羊类
class Sheep :public Animal 
{
	int leg_num;
};

// 驼类
class Tuo :public Animal 
{
public:
	int height;
};

// 羊驼类
class SheepTuo:public Sheep, public Tuo {};

从下图SheepTuo类的结构可以看出在没有基类没有虚继承时,确实有两个m_age变量

image-20201102215405068

虚继承

#include <iostream>
using namespace std;

// 动物类
class Animal
{
public:
	int m_age;
};

// 羊类
class Sheep :virtual public Animal 
{
	int leg_num;
};

// 驼类
class Tuo :virtual public Animal 
{
public:
	int height;
};

// 羊驼类
class SheepTuo:public Sheep, public Tuo {};

int main()
{
	SheepTuo st;
	st.m_age = 20;
	cout << st.m_age << endl;
	return 0;
}

从下图SheepTuo类的结构可以看出在基类虚继承时,只有一个m_age变量

注:vbptr是虚基类指针;vbtable是虚基类表

image-20201102215730170

4.7多态

4.7.1多态的基本概念

多态分为两类:

  • 静态多态:函数重载运算符重载属于静态多态,复用函数名

  • 动态多态:派生类和虚函数实现运行时多态

静态多态与动态多态区别:

  • 静态多态的函数地址早绑定 - 编阶段确定函数地址
  • 动态多态的函数地址晚绑定 - 运行阶段确定函数地址
静态多态-案例
#include <iostream>
using namespace std;

class Animal
{
public:
	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.speak();
}

void test()
{
	Cat cat;
	doSpeak(cat);
}

int main()
{
	test();
	return 0;
}

运行结果:

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-s2oopqem-1605180981129)(C:\Users\11986\AppData\Roaming\Typora\typora-user-images\image-20201103105606181.png)]

动态多态-案例

只需在父类同名成员函数前加virtual

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

// 父类引用的方式指向子类对象,还可以是父类指针
void doSpeak(Animal & animal) // Animal & animal == cat
{
	animal.speak();
}

void test()
{
	Cat cat;
	doSpeak(cat);
}

int main()
{
	test();
	return 0;
}

多态多态的满足条件:

  1. 有继承关系
  2. 子类要重写父类的虚函数

动态多态使用:

  • 父类指针引用指向子类对象

注:函数重写——函数返回值类型 函数名 参数列表 都相同

多态带来的好处:
1. 组织结构清晰
2. 可读性强
3. 对于前期和后期的维护性高

4.7.2 案例-计算器类
#include <iostream>
using namespace std;

// 普通写法:计算器类
class Caculator1
{
private:
	int num1;
	int num2;
	string flag;
private:
	int add() { return num1 + num2; }
	int minus() { return num1 - num2; }
	int times() { return num1 * num2; }
	int divided() { return num1 / num2; }
public:
	Caculator1(int n1, string f, int n2) :num1(n1), flag(f), num2(n2) {}
	void show()
	{
		if (flag == "+")
		{
			cout << num1 << flag << num2 << "=" << add() << endl;
		}
		if (flag == "-")
		{
			cout << num1 << flag << num2 << "=" << minus() << endl;
		}
		if (flag == "*")
		{
			cout << num1 << flag << num2 << "=" << times() << endl;
		}
		if (flag == "/")
		{
			cout << num1 << flag << num2 << "=" << divided() << endl;
		}
	}
};

// 利用多态实现计算器类
/* 多态带来的好处:
	1. 组织结构清晰
	2. 可读性强
	3. 对于前期和后期的维护性高
*/
// 实现计算器抽象类
class AbstractCaculator
{
public:
	virtual int getResult() { return 0; }
	int m_Num1;
	int m_Num2;
};
// 加法计算器类
class AddCaculator :public AbstractCaculator
{
public:
	int getResult() { return m_Num1 + m_Num2; }
};
// 减法计算器类
class MinusCaculator :public AbstractCaculator
{
public:
	int getResult() { return m_Num1 - m_Num2; }
};
// 乘法计算器类
class MultiplyCaculator :public AbstractCaculator
{
public:
	int getResult() { return m_Num1 * m_Num2; }
};

// 普通写法测试
void test01()
{
	int num1, num2;
	string flag;
	cin >> num1 >> flag >> num2;
	Caculator1 c(num1, flag, num2);
	c.show();
}

// 多态写法测试
void test02()
{
	// 多态使用条件
	// 父类引用指向子类对象
	AbstractCaculator* abc = new AddCaculator;
	abc->m_Num1 = 10;
	abc->m_Num2 = 20;
	cout << abc->m_Num1 << " + " << abc->m_Num2 << " = " << abc->getResult() << endl;
	delete abc; // 只是销毁new对象在堆区的数据

	abc = new MinusCaculator;
	abc->m_Num1 = 10;
	abc->m_Num2 = 20;
	cout << abc->m_Num1 << " - " << abc->m_Num2 << " = " << abc->getResult() << endl;
	delete abc;

	abc = new MultiplyCaculator;
	abc->m_Num1 = 10;
	abc->m_Num2 = 20;
	cout << abc->m_Num1 << " * " << abc->m_Num2 << " = " << abc->getResult() << endl;
	delete abc;
}
int main()
{
	// test01();
	test02();
	return 0;
}
4.7.3 纯虚函数和抽象类

纯虚函数语法:virtual 返回值类型 函数名(参数列表)=0

当类中有纯虚函数,这个类也称为抽象类

抽象类特点:

  • 无法实例化对象
  • 抽象类的子类 必须要重写父类中的纯虚函数,否则也属于抽象类
#include <iostream>
using namespace std;

class Base
{
public:
	virtual void func() = 0;
};

class Son :public Base
{
public:
	void func() 
	{
		cout << "这是子类" << endl;
	}
};

int main()
{
	Son s;
	s.func();
	return 0 ;
}
4.7.4 案例-制作饮品

饮品大多制作流程:煮水–>冲泡–>倒入杯中–>加入配料(相当于固定流程或模板)

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-utkZeZvm-1605180981131)(C:\Users\11986\AppData\Roaming\Typora\typora-user-images\image-20201103173145371.png)]

代码实现

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

// 饮品抽象类
class AbstractDrinking
{
public:
	virtual void boiling() = 0;
	virtual void brewing() = 0;
	virtual void pouring() = 0;
	virtual void addsth() = 0;
	void making() 
	{
		boiling();
		brewing();
		pouring();
		addsth();
	}
};

// 咖啡类
class Coffee:public AbstractDrinking
{
	void boiling() { cout << "煮农夫山泉中......" << endl; }
	void brewing() { cout << "正在冲泡咖啡......" << endl; }
	void pouring() { cout << "倒入杯中......" << endl; }
	void addsth() { cout << "加入奶和白糖......" << endl; }
};

// 茶类
class Tea :public AbstractDrinking
{
	void boiling() { cout << "煮百岁山中......" << endl; }
	void brewing() { cout << "正在冲泡茶叶......" << endl; }
	void pouring() { cout << "倒入杯中......" << endl; }
	void addsth() { cout << "加入柠檬片......" << endl; }
};

void doDrinking(AbstractDrinking * abs)
{
	abs->making();
	delete abs;
}

void test001()
{
	cout << setw(30) << setfill('*') << "正在制作咖啡中" << setw(20) << "" << endl;
	doDrinking(new Coffee);

	cout << setw(30) << setfill('*') << "正在制作茶水中" << setw(20) << "" << endl;
	doDrinking(new Tea);
}

int main()
{
	test001();
	return 0;
}
4.7.5 虚析构和纯虚析构
问题

​ 多态使用时,如果子类中有属性开辟到堆区,那么父类指针在释放时无法调用到子类得析构函数代码

解决方法
	将父类中的析构函数改为==虚析构==或者==纯虚析构==
虚析构与纯虚析构共性
  • 可以解决父类指针释放子类对象
  • 都需要有具体的函数实现
虚析构和纯虚析构区别
  • 如果是纯虚析构,该类属于抽象类,无法实例化对象
语法

​ 虚析构语法:

virtual ~类名(){}

​ 纯虚析构语法:

​ 类内:virtual ~类名()=0

​ 类外:类名::~类名(){}

#include <iostream>
using namespace std;

// 动物类
class Animal
{
public:
	virtual void speak() = 0;
	// 虚析构与纯虚析构只能写其一
	// 虚析构
	//virtual ~Animal() { cout << "Animal析构函数" << endl; }

	// 纯虚析构
	virtual ~Animal() = 0;
};
// 纯虚析构实现
Animal::~Animal() { /* cout << "Animal析构函数" << endl;*/ }


class Cow:public Animal
{
public:
	void speak() { cout << "牛在叫" << endl; }

	// 没有在堆区开辟内存,不必写析构函数
	// ~Cow() { cout << "Cow析构函数" << endl; }
};

class Mouse :public Animal
{
public:
	Mouse(string name) :m_Name(new string(name)) {}
	void speak() { cout << *m_Name << "老鼠在说话" << endl; }

	// 在堆区开辟了内存必须得释放,必须写析构函数,但是释放父类指针并能释放子类在堆区开辟得数据,所以父类就得写虚析构或纯虚析构
	~Mouse() { if (m_Name != NULL) { delete m_Name; m_Name = NULL; /*cout << "Cat析构函数" << endl;*/ } }
public:
	string *m_Name;
};

void doSpeak(Animal* animal)
{
	animal->speak();
	delete animal;
}
void test002()
{
	doSpeak(new Mouse("Jerry"));

	doSpeak(new Cow);
}

int main()
{
	test002();
	return 0;
}
总结:
  • 虚析构或纯虚析构就是用来解决父类指针释放子类对象
  • 如果子类中没有堆区数据,可以不写虚析构或纯虚析构
  • 用有纯虚析构也属于抽象类
4.7.4案例改写
#include <iostream>
#include <iomanip>
using namespace std;

// 饮品抽象类
class AbstractDrinking
{
public:
	virtual void boiling() = 0;
	virtual void brewing() = 0;
	virtual void pouring() = 0;
	virtual void addsth() = 0;
	void making() 
	{
		boiling();
		brewing();
		pouring();
		addsth();
	}

	// 纯虚析构 需要声明也需要实现,类外实现
	virtual ~AbstractDrinking() = 0;

};
AbstractDrinking::~AbstractDrinking() { cout << "Animal类析构函数" << endl; }

// 咖啡类
class Coffee:public AbstractDrinking
{
public:
	void boiling() { cout << "煮农夫山泉中......" << endl; }
	void brewing() { cout << "正在冲泡咖啡......" << endl; }
	void pouring() { cout << "倒入杯中......" << endl; }
	void addsth() { cout << "加入奶和白糖......" << endl; }

	~Coffee() { cout << "Coffe析构函数" << endl; }
};

// 茶类
class Tea :public AbstractDrinking
{
public:
	Tea(string name) :m_Name(new string(name)) {}
	~Tea() { if (m_Name != NULL) { delete m_Name; m_Name = NULL; cout << "Tea析构函数" << endl; } }
	void boiling() { cout<< *m_Name << "正在煮百岁山......" << endl; }
	void brewing() { cout << *m_Name << "正在冲泡茶叶......" << endl; }
	void pouring() { cout << *m_Name << "正在将茶水倒入杯中......" << endl; }
	void addsth() { cout << *m_Name << "正在加入柠檬片......" << endl; }
private:
	string* m_Name;
};

// 制作制作饮品接口(模板)
void doDrinking(AbstractDrinking * abs)
{
	abs->making();
	delete abs; // 释放堆区的数据
}

// 测试
void test001()
{
	cout << setw(30) << setfill('*') << "正在制作咖啡中" << setw(20) << "" << endl;
	doDrinking(new Coffee);

	cout << setw(30) << setfill('*') << "正在制作茶水中" << setw(20) << "" << endl;
	doDrinking(new Tea("杰森"));
}

/*int main()
{
	test001();
	return 0;
}*/
4.7.6 案例-电脑组装
案例描述

​ 电脑主要组成部件为CPU(用计算),显卡(用于显示),内存条(用于存储)
​ 将每个零件封装成抽象基类,并且提供不同的厂商生产的不同的零件,例如intel厂商和Lenovo厂商
​ 创建电脑类提供让电脑工作的函数,并且调用每个零件工作的接口
​ 测试时组装三台不同的电脑进行工作

结构
image-20201103232644660
自我实现代码
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;

// 抽象cpu基类
class AbsCPU
{
public:
	virtual void caculate() = 0;
};

// 抽象显卡基类
class AbsVideoCard
{
public:
	virtual void show() = 0;
};

// 抽象内存条基类
class AbsMemory
{
public:
	virtual void storage() = 0;
};

// intel厂商类
class Intel :public AbsCPU, public AbsVideoCard, public AbsMemory
{
public:
	void caculate() { cout << "Intel CPU" << endl; }
	void show() { cout << "Intel video card" << endl; }
	void storage() { cout << "Intel memory" << endl; }
};

// Lenovo厂商类
class Lenovo :public AbsCPU, public AbsVideoCard, public AbsMemory
{
public:
	void caculate() { cout << "Lenovo CPU" << endl; }
	void show() { cout << "Lenovo video card" << endl; }
	void storage() { cout << "Lenovo memory" << endl; }
};

// 电脑类
class Computer
{
public:
	Computer(string desc) :m_desc(desc) {}
	void makeup(AbsCPU* cpu, AbsVideoCard* vc, AbsMemory* mem)
	{
		cout << setw(25) << setfill('*') <<"开始组装" << m_desc << setw(15) << "" << endl;
		cpu->caculate();
		vc->show();
		mem->storage();
		cout << setw(30) << setfill('*') << "组装完成" << setw(20) << "" << endl;

		// 释放堆区的数据
		delete cpu, vc, mem; 
	}
private:
	string m_desc;
};

void test003()
{
	Computer com1("Intel电脑");
	com1.makeup(new Intel, new Intel, new Intel);

	Computer com2("Lenovo电脑");
	com2.makeup(new Lenovo, new Lenovo, new Lenovo);

	Computer com3("混搭电脑");
	com2.makeup(new Intel, new Lenovo, new Lenovo);
}

int main()
{
	test003();
	return 0;
}
代码分析

厂商类:对于全用同一种类型的配件无影响,但是只用某一个买配件就会造成多余的内存,同时这样写没有充分利用类的多态特性

代码改造(老师代码)
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;

// 抽象cpu基类
class AbsCPU
{
public:
	virtual void caculate() = 0;
};

// 抽象显卡基类
class AbsVideoCard
{
public:
	virtual void show() = 0;
};

// 抽象内存条基类
class AbsMemory
{
public:
	virtual void storage() = 0;
};

// intel厂商类
class IntelCPU :public AbsCPU
{
public:
	void caculate() { cout << "Intel CPU is caculating." << endl; }
	
};

class IntelVideoCard :public AbsVideoCard
{
public:
	void show() { cout << "Intel video card is working." << endl; }
};

class IntelMemory :public AbsMemory
{
public:
	void storage() { cout << "Intel memory is to storage." << endl; }
};
// Lenovo厂商类
class LenovoCPU :public AbsCPU
{
public:
	void caculate() { cout << "Lenovo CPU is caculating." << endl; }

};

class LenovoVideoCard :public AbsVideoCard
{
public:
	void show() { cout << "Lenovo video card is working." << endl; }
};

class LenovoMemory :public AbsMemory
{
public:
	void storage() { cout << "Lenovo memory is to storage." << endl; }
};

// 电脑类
class Computer
{
public:
	Computer(string* desc, AbsCPU *cpu, AbsVideoCard *vc, AbsMemory *mem) :m_desc(desc), m_cpu(cpu), m_vc(vc), m_mem(mem){}
	~Computer()
	{
		if (m_cpu != NULL) { delete m_cpu; m_cpu = NULL; }
		if (m_vc != NULL) { delete m_vc; m_cpu = NULL; }
		if (m_mem != NULL) { delete m_mem; m_cpu = NULL; }
		if (m_desc != NULL) { delete m_desc; m_desc = NULL; }
	}
	void makeup()
	{
		cout << setw(25) << setfill('*') <<"开始组装" << *m_desc << setw(15) << "" << endl;
		m_cpu->caculate();
		m_vc->show();
		m_mem->storage();
		cout << setw(30) << setfill('*') << "组装完成" << setw(20) << "" << endl;
	}
private:
	string* m_desc;
	AbsCPU* m_cpu;
	AbsVideoCard* m_vc;
	AbsMemory* m_mem;
};

void test003()
{
	// 第一台电脑零件
	AbsCPU* intelCPU = new IntelCPU;
	AbsVideoCard* intelVideoCard = new IntelVideoCard;
	AbsMemory* intelMemory = new IntelMemory;

	// 创建第一台电脑
	string* computer_name1 = new string("Intel电脑");
	Computer* computer1 = new Computer(computer_name1, intelCPU, intelVideoCard, intelMemory);
	computer1->makeup();
	delete computer1;

	// 第二台电脑零件
	AbsCPU* lenovoCPU = new LenovoCPU;
	AbsVideoCard* lenovoVideoCard = new LenovoVideoCard;
	AbsMemory* lenovoMemory = new LenovoMemory;

	// 创建第二台电脑
	string* computer_name2 = new string("Lenovo电脑");
	Computer* computer2 = new Computer(computer_name2, lenovoCPU, lenovoVideoCard, lenovoMemory);
	computer2->makeup();
	delete computer2;

	// 创建第三台电脑
	string* computer_name3 = new string("混搭电脑  ");
	// 直接在传参过程中new对象
	Computer* computer3 = new Computer(computer_name3, new IntelCPU, nw LenovoVideoCard, new LenovoMemory);
	computer3->makeup();
	delete computer3;
}

int main()
{
	test003();
	return 0;
}

5.文件操作

程序运行时产生的数据都属于临时数据,程序一旦运行结束都会被释放

通过文件可以将数据持久化

C++中对文件操作需要包含头文件====

文件类型分为

  • 文本文件 - 文件以文本的ASCII码形式存储到计算机中
  • 二进制文件 - 文件以文本的二进制形式存储在计算机中,用户一般不能直接读懂

操作文件的三大类

  1. ofstream :写操作
  2. ifstream:读操作
  3. fstream:读写操作

5.1文本文件

5.1.1写文件
写文件步骤
  1. 包含头文件

    #include <fstream>

  2. 创建流对象

    ofstream ofs;

  3. 打开文件

    ofs.ope("文件路径", 打开方式);

  4. 写数据

    ofs << "写入的内容" << endl;

  5. 关闭文件

    ofs.close();

打开文件的方式
打开方式解释
ios::in为读文件而打开文件
ios::out为写文件而打开文件
ios::ate初始位置:文件尾
ios::app追加方式写文件
ios::trunc如果文件存在先删除
ios::binary二进制文件
**注意:**文件打开方式可以配合使用,利用|操作符

例如:用二进制写文件,ios::out | ios::binary

demo
#include <iostream>
 // 1.添加头文件
#include <fstream>
using namespace std;

int main()
{
    // 2.创建流对象
	fstream ofs;
    // 3.指定打开方式
	ofs.open("test.txt", ios::out);
    // 4.写入内容,endl后会换行
	ofs << "姓名:张三" << endl;
	ofs << "性别:男" << endl;
	ofs << "年龄:20" << endl;
    // 5.关闭文件
	ofs.close();
	return 0;
}
5.1.2读文件

读文件步骤

  1. 添加头文件

    #include <fstream>

  2. 创建流对象

    fstream ifs;

  3. 打开文件并判断文件是否打开成功

    ifs.open("文件路径", 读取方式);if (!ifs.is_open()){cout<< "文件打开失败"<< endl;}

  4. 读取内容

    四种方式

  5. 关闭文件

    ifs.close();

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

int main()
{
	fstream ifs;
	ifs.open("test.txt", ios::in);
	char buffer[1024];
	// 读取方式1
	//while (!ifs.eof())
	//{
	//	ifs.getline(buffer, 1024);
	//	cout << buffer << endl;
	//}
	// 读取方式2
	//while (ifs >> buffer)
	//{
	//	cout << buffer << endl;
	//}
	//ifs.close();
	// 读取方式3
	//while (ifs.getline(buffer, sizeof(buffer)))
	//{
	//	cout << buffer << endl;
	//}
	// 读取方式4:一次只读一个字符
	char c;
	while ((c = ifs.get()) != EOF)  // EOF : end of file
	{
		cout << c;
	}
	return 0;
}

总结

  1. 读文件可以用ifstream,或者用fstream
  2. 利用is_open()函数可以判断文件是否打开成功
  3. close()关闭文件
5.1.3案例-水仙花数写入txt
#include <iostream>
#include <fstream>
using namespace std;

int main()
{
	fstream ofs;
	ofs.open("水仙花数.txt", ios::out);
	int num = 100;
	while (num < 1000)
	{
		int hunderd = num / 100;
		int ten = num % 100 / 10;
		int n = num % 10;
		if (pow(hunderd, 3) + pow(ten, 3) + pow(n, 3) == num)
		{
			ofs << num << "\t";
		}
		num++;

	}
	ofs.close();
	return 0;
}

结果

image-20201104092324608

5.2二进制文件

打开方式指定为:ios::binary

5.2.1写文件

二进制方式写文件主要利用流对象调用成员函数write

函数原型:ostream& write(const char * buffer, int len);

参数解释:字符指针buffer指向内存中一段存储空间,len是读写的字节数

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

class Person
{
public:
	char m_Name[64];
	int m_Age;
};

void test01()
{
	fstream ofs("person.txt", ios::out | ios::binary);
	// 写入
	Person p = { "张三", 18 };
	ofs.write((const char *)&p, sizeof(Person));
	ofs.close();
}

int main()
{
	test01();
	return 0;
}
5.2.2 读文件

二进制方式读文件主要利用流对象调用成员函数read

函数原型:istream& read(char * buffer, int len)

参数解释:字符指针buffer指向内存中一段存储空间,len是读写的字节数

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

class Person
{
public:
	char m_Name[64];
	int m_Age;
};

int main()
{
	// 打开文件
	fstream ifs("person.txt", ios::in | ios::binary);
	if (!ifs.is_open()) { cout << "打开文件失败" << endl; return 0; }
	// 读文件
	Person p;
	ifs.read((char*)&p, sizeof(Person));
	// 打印内容
	cout << "姓名:" << p.m_Name << "\t年龄:" << p.m_Age << endl;
	ifs.clear();
	return 0;
}

fs << “姓名:张三” << endl;
ofs << “性别:男” << endl;
ofs << “年龄:20” << endl;
// 5.关闭文件
ofs.close();
return 0;
}


#### 5.1.2读文件

读文件步骤

1. 添加头文件

   `#include <fstream>`

2. 创建流对象

   `fstream ifs;`

3. 打开文件并判断文件是否打开成功

   `ifs.open("文件路径", 读取方式);if (!ifs.is_open()){cout<< "文件打开失败"<< endl;}`

4. 读取内容

   四种方式

5. 关闭文件

   `ifs.close();`

##### demo

```c++
#include <iostream>
#include <fstream>
using namespace std;

int main()
{
	fstream ifs;
	ifs.open("test.txt", ios::in);
	char buffer[1024];
	// 读取方式1
	//while (!ifs.eof())
	//{
	//	ifs.getline(buffer, 1024);
	//	cout << buffer << endl;
	//}
	// 读取方式2
	//while (ifs >> buffer)
	//{
	//	cout << buffer << endl;
	//}
	//ifs.close();
	// 读取方式3
	//while (ifs.getline(buffer, sizeof(buffer)))
	//{
	//	cout << buffer << endl;
	//}
	// 读取方式4:一次只读一个字符
	char c;
	while ((c = ifs.get()) != EOF)  // EOF : end of file
	{
		cout << c;
	}
	return 0;
}

总结

  1. 读文件可以用ifstream,或者用fstream
  2. 利用is_open()函数可以判断文件是否打开成功
  3. close()关闭文件
5.1.3案例-水仙花数写入txt
#include <iostream>
#include <fstream>
using namespace std;

int main()
{
	fstream ofs;
	ofs.open("水仙花数.txt", ios::out);
	int num = 100;
	while (num < 1000)
	{
		int hunderd = num / 100;
		int ten = num % 100 / 10;
		int n = num % 10;
		if (pow(hunderd, 3) + pow(ten, 3) + pow(n, 3) == num)
		{
			ofs << num << "\t";
		}
		num++;

	}
	ofs.close();
	return 0;
}

结果

image-20201104092324608

5.2二进制文件

打开方式指定为:ios::binary

5.2.1写文件

二进制方式写文件主要利用流对象调用成员函数write

函数原型:ostream& write(const char * buffer, int len);

参数解释:字符指针buffer指向内存中一段存储空间,len是读写的字节数

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

class Person
{
public:
	char m_Name[64];
	int m_Age;
};

void test01()
{
	fstream ofs("person.txt", ios::out | ios::binary);
	// 写入
	Person p = { "张三", 18 };
	ofs.write((const char *)&p, sizeof(Person));
	ofs.close();
}

int main()
{
	test01();
	return 0;
}
5.2.2 读文件

二进制方式读文件主要利用流对象调用成员函数read

函数原型:istream& read(char * buffer, int len)

参数解释:字符指针buffer指向内存中一段存储空间,len是读写的字节数

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

class Person
{
public:
	char m_Name[64];
	int m_Age;
};

int main()
{
	// 打开文件
	fstream ifs("person.txt", ios::in | ios::binary);
	if (!ifs.is_open()) { cout << "打开文件失败" << endl; return 0; }
	// 读文件
	Person p;
	ifs.read((char*)&p, sizeof(Person));
	// 打印内容
	cout << "姓名:" << p.m_Name << "\t年龄:" << p.m_Age << endl;
	ifs.clear();
	return 0;
}
  • 20
    点赞
  • 161
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 《C++面向对象高效编程 第2版》是一本关于C++编程的书籍。本书主要介绍了面向对象编程的概念、原则和技巧,以及如何利用C++语言进行高效的面向对象编程。 在书中,作者详细讲解了C++面向对象特性,包括封装、继承和多态等。同时,还介绍了C++中的其他重要特性,如模板、异常处理和STL库等。这些知识点的讲解结合了大量的例子和实践,使读者能够更好地理解和应用这些概念。 本书在第2版中进行了更新和扩充,添加了新的内容,涉及了现代C++的一些特性和技术,如智能指针、移动语义和并发编程等。这些新的内容可以帮助读者更好地应对现代软件开发的需求和挑战。 与此同时,本书还提供了大量的编程实践和案例,帮助读者掌握实际的编程技巧和经验。通过这些实践,读者可以学习到如何编写高效、可复用和易于维护的C++代码。 总之,《C++面向对象高效编程 第2版》是一本适合C++初学者和有一定经验的程序员阅读的书籍。它不仅介绍了面向对象编程的基本概念和技巧,还提供了大量的实例和编程实践,帮助读者掌握C++语言的高效应用。无论是想深入学习C++面向对象编程的基础知识,还是想了解C++中一些高级特性的使用,这本书都是一本值得推荐的参考书。 ### 回答2: 《C++面向对象高效编程 第2版》是一本关于C++面向对象编程的经典教材。本书的PDF版本可以在网络上进行搜索和获取。该书全面介绍了对象、类、继承、多态等C++面向对象编程的基础知识,同时还深入讲解了C++的高效编程技巧和最佳实践。 在本书中,作者详细解析了C++的语法和特性,并提供了大量的示例代码演示,以帮助读者更好地理解和掌握面向对象编程的概念和技术。此外,本书还介绍了一些高级主题,如异常处理、模板编程、STL等,在帮助读者提高编程能力的同时,也为读者拓宽了编程思维。 通过学习《C++面向对象高效编程 第2版》PDF版,读者可以系统地学习C++面向对象编程的知识,深入理解面向对象编程的原理和思想,并能够灵活运用这些知识进行开发和设计。同时,本书还提供了一些经验和技巧,帮助读者编写出高效、可维护的C++代码。 总而言之,该书是一本重要的学习资料,不仅适合初学者入门,也适合有一定C++编程基础的开发者进一步提升自己的编程水平。通过阅读《C++面向对象高效编程 第2版》PDF版,读者将能够系统学习C++面向对象编程,并在实际开发中运用所学知识,提升自己的编程能力。 ### 回答3: 《面向对象高效编程 第2版》是一本关于面向对象编程的书籍,主要介绍了面向对象编程的原理、思想和最佳实践。 在这本书中,作者首先介绍了面向对象编程的基本概念,包括类、对象、继承、多态等。然后,作者详细介绍了如何有效地使用面向对象编程进行软件开发。他提出了一些实用的技巧和方法,帮助读者更好地理解和应用面向对象编程的思想。 除了介绍基本的面向对象编程理论外,这本书还提供了大量的案例和示例代码,帮助读者实际运用所学知识解决实际问题。这些案例涵盖了不同领域的应用,包括图形界面、网络编程、数据库操作等,让读者能够从不同角度了解面向对象编程的实际应用场景。 此外,这本书还介绍了一些常用的面向对象编程语言,如Java、C++和Python等。作者通过比较不同的编程语言特点和应用场景,帮助读者选择合适的编程语言,并指导读者如何在具体的编程语言中高效地实践面向对象编程。 总之,《面向对象高效编程 第2版》是一本很好的面向对象编程学习资料。无论是初学者还是有一定经验的开发者,都能从中获取到宝贵的学习经验和实践技巧。通过阅读这本书,读者能够对面向对象编程有更深入的理解,提升自己在软件开发中的能力。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值