虚析构和纯虚析构
父类引用指向子类对象
AbstractDrinking coffee1 = new Coffee
多态使用时,如果子类中有属性开辟到堆区,那么父类指针在释放时无法调用到子类的析构代码
- 解决方式:将父类中的析构函数改为虚析构或者纯虚析构
虚析构和纯虚析构共性:
- 可以解决父类指针释放子类对象
- 都需要有具体的函数实现
虚析构和纯虚析构区别:
- 如果是纯虚析构,该类属于抽象类,无法实例化对象
虚析构语法:
virtual ~类名(){} // 相当于在析构函数中加了virtual
纯虚析构语法:
virtual ~类名() =0;
类名 ::~ 类名(){}
举个栗子
子类中属性有在堆区开辟空间, 这时多态调用不了
#include <iostream>
#include <cstring>
using namespace std;
class Animal{
public:
Animal(){
cout<<"Animal() constructor"<< endl;
}
~ Animal(){
cout<<"virtual ~ Animal() xigou"<< endl;
}
};
class Cat: public Animal{
public:
Cat(string name){
cout<<"Cat() constructor"<< endl;
m_Name = new string(name); // 堆区开辟空间
}
void Speak(){
cout<< *m_Name <<"cat is speaking"<< endl;
}
~Cat(){
cout<<"Cat() xiegou"<< endl;
if (this->m_Name != NULL){
delete m_Name;
m_Name = NULL;
}
}
public:
string *m_Name;
};
int main() {
// Animal *animal = new Cat("Tom"); 报错
Cat *animal = new Cat("Tom");
animal->Speak();
delete animal;;
}
解决方法
#include <iostream>
#include <cstring>
using namespace std;
class Animal{
public:
Animal(){
cout<<"Animal() constructor"<< endl;
}
// virtual ~ Animal(){
// cout<<"virtual ~ Animal() xigou"<< endl;
// }
virtual void Speak() = 0;
virtual ~Animal() = 0;
};
Animal::~Animal(){
cout<<"Animal chunxu xigou"<< endl;
}
class Cat: public Animal{
public:
Cat(string name){
cout<<"Cat() constructor"<< endl;
m_Name = new string(name); // 堆区开辟空间
}
virtual void Speak(){
cout<< *m_Name <<"cat is speaking"<< endl;
}
~Cat(){
cout<<"Cat() xiegou"<< endl;
if (this->m_Name != NULL){
delete m_Name;
m_Name = NULL;
}
}
public:
string *m_Name;
};
int main() {
Animal *animal = new Cat("Tom");
// Cat *animal = new Cat("Tom");
animal->Speak();
delete animal;
}
-
虚析构或纯虚析构就是用来解决通过父类指针释放子类对象
-
如果子类中没有堆区数据,可以不写为虚析构或纯虚析构
-
拥有纯虚析构函数的类也属于抽象类
电脑组装案例
案例描述:
电脑主要组成部件为 CPU(用于计算),显卡(用于显示),内存条(用于存储)
将每个零件封装出抽象基类,并且提供不同的厂商生产不同的零件,例如Intel厂商和Lenovo厂商
创建电脑类提供让电脑工作的函数,并且调用每个零件工作的接口
测试时组装三台不同的电脑进行工作
#include <iostream>
using namespace std;
//抽象CPU类
class CPU{
public:
//抽象的计算函数
virtual void calculate()=0;
};
//抽象显卡类
class VideoCard{
public:
//抽象显示函数
virtual void dispaly()=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->dispaly();
m_mem->storage();
}
~Computer(){
if (m_cpu != NULL){
delete m_cpu;
m_cpu = NULL;
}
if (m_mem != NULL){
delete m_mem;
m_mem = NULL;
}
if (m_vc != NULL){
delete m_vc;
m_vc = NULL;
}
}
private:
CPU * m_cpu; // CPU的零件指针
VideoCard * m_vc; //显卡的零件指针
Memory * m_mem; //内存的零件指针
};
//Intel厂商
class IntelCPU: public CPU{
public:
virtual void calculate(){
cout << "IntelCPU calculate"<< endl;
};
};
class IntelVideoCard: public VideoCard{
public:
virtual void dispaly(){
cout << "IntelVideoCard dispaly"<< endl;
};
};
class IntelMemory: public Memory{
public:
virtual void storage(){
cout << "IntelMemory storage"<< endl;
};
};
// Lenovo厂商
class LenovoCPU: public CPU{
public:
virtual void calculate(){
cout << "LenovoCPU calculate"<< endl;
};
};
class LenovoVideoCard: public VideoCard{
public:
virtual void dispaly(){
cout << "LenovoVideoCard dispaly"<< endl;
};
};
class LenovoMemory: public Memory{
public:
virtual void storage(){
cout << "LenovoMemory storage"<< endl;
};
};
void test01(){
//1
CPU *intelCpu = new IntelCPU;
VideoCard *intelCard = new IntelVideoCard;
Memory *intelMemory= new IntelMemory;
Computer *computer1 = new Computer(intelCpu, intelCard, intelMemory);
computer1->work();
delete computer1;
cout << "-----------------------------"<<endl;
//2
Computer *computer2 = new Computer(new LenovoCPU, new LenovoVideoCard, new LenovoMemory);
computer2->work();
delete computer2;
cout << "-----------------------------"<<endl;
//3
Computer *computer3 = new Computer(new LenovoCPU, new IntelVideoCard, new LenovoMemory);
computer3->work();
delete computer3;
};
int main() {
test01();
}
文件操作
程序运行时产生的数据都属于临时数据,程序一旦运行结束都会被释放
通过文件可以将数据持久化
C++中对文件操作需要包含头文件 <fstream>
操作文件的三大类:
- ofstream:写操作
- ifstream: 读操作
- fstream : 读写操作
文本文件
5.1.1写文件
写文件步骤如下:
- 包含头文件
#include - 创建流对象
ofstream ofs; - 打开文件
ofs.open(“文件路径”,打开方式); - 写数据
ofs << “写入的数据”; - 关闭文件
ofs.close();
#include <iostream>
#include <fstream>
using namespace std;
void test01(){
ofstream ofs;
ofs.open("test.txt", ios::out);
ofs << "name eric"<<endl;
ofs << "gender male"<<endl;
ofs << "age 18"<<endl;
ofs.close();
}
int main() {
test01();
}
总结:
- 文件操作必须包含头文件 fstream
- 读文件可以利用 ofstream ,或者fstream类
- 打开文件时候需要指定操作文件的路径,以及打开方式
- 利用<<可以向文件中写数据
- 操作完毕,要关闭文件
文件打开方式:
打开方式 解释
ios::in 为读文件而打开文件
ios::out 为写文件而打开文件
ios::ate 初始位置:文件尾
ios::app 追加方式写文件
ios::trunc 如果文件存在先删除,再创建
ios::binary 二进制方式
读文件
读文件与写文件步骤相似,但是读取方式相对于比较多
读文件步骤如下:
- 包含头文件
#include - 创建流对象
ifstream ifs; - 打开文件并判断文件是否打开成功
ifs.open(“文件路径”,打开方式); - 读数据
四种方式读取 - 关闭文件
ifs.close();
#include <iostream>
#include <fstream>
#include <cstring>
using namespace std;
void test01(){
ifstream ifs;
ifs.open("test.txt", ios::in);
if (!ifs.is_open()){
cout<< "file open fail"<< endl;\
return;
}
//1
// char buf[1024] = {0};
// while (ifs>>buf){
// cout<< buf << endl;
// }
// 2
// char buf[1024] = {0};
// while (ifs.getline(buf, sizeof(buf))){
// cout<< buf << endl;
// }
// 3
// string buf;
// while (getline(ifs, buf)){
// cout<< buf << endl;
//
// }
// 4
char c;
while ((c = ifs.get() )!= EOF ){
cout << c;
}
ifs.close();
}
int main() {
test01();
}