一.电脑组装项目各文件的关系和cpp文件目录
二.使用父类指针和new关键字实例化子类时无法调用子类的析构函数
即使用如下代码,不会调用IntelCpu的析构函数。导致无法释放子类申请的空间。
AbstractCpu* cpu = new IntelCpu;
delete cpu;
完整代码如下:
AbstractCpu.h
#pragma once
#include<iostream>
using namespace std;
class AbstractCpu
{
public:
virtual void calculate() = 0;
~AbstractCpu()
{
cout<<"我调用了AbstractCpu的析构函数"<<endl;
}
};
IntelCpu.h
#pragma once
#include "AbstractCpu.h"
#include "iostream"
using namespace std;
class IntelCpu: public AbstractCpu
{
public:
IntelCpu()
{
cout<<"我调用了IntelCPU的构造函数"<<endl;
}
~IntelCpu();
{
cout<<"我调用了IntelCPU的西狗函数"<<endl;
}
void calculate();
};
IntelCpu.cpp
#include "IntelCpu.h"
void IntelCpu::calculate()
{
cout<<"Intel的CPU开始计算了"<<endl;
}
main.cpp
#include <iostream>
#include "Computer.h"
#include "AbstractCpu.h"
#include "IntelCpu.h"
using namespace std;
int main()
{
AbstractCpu* cpu = new IntelCpu;
delete cpu;
return 0;
}
g++ main.cpp src/IntelCpu.cpp src/IntelMemory.cpp src/IntelVideoCard.cpp src/Computer.cpp -Iinclude
发现根本没调用IntelCpu的析构函数。
因为我们用父类的指针指向子类IntelCpu,当delete cpu;时,父类调用自己的析构函数并不会执行子类的析构函数。
三.在父类中使用虚析构,解决上面的问题
AbstractCpu.cpp
#pragma once
#include<iostream>
using namespace std;
class AbstractCpu
{
public:
AbstractCpu()
{
cout<<"我调用了AbstractCpu的构造函数"<<endl;
}
virtual void calculate() = 0;
virtual ~AbstractCpu()
{
cout<<"我调用了AbstractCpu的析构函数"<<endl;
}
};
IntelCpu.cpp
#pragma once
#include "AbstractCpu.h"
#include "iostream"
using namespace std;
class IntelCpu: public AbstractCpu
{
public:
IntelCpu()
{
cout<<"我调用了IntelCPU的构造函数"<<endl;
}
~IntelCpu();
{
cout<<"我调用了IntelCPU的西狗函数"<<endl;
}
void calculate();
};
main.cpp
#include <iostream>
#include "Computer.h"
#include "AbstractCpu.h"
#include "IntelCpu.h"
using namespace std;
int main()
{
AbstractCpu* cpu = new IntelCpu;
delete cpu;
return 0;
}
g++ main.cpp src/IntelCpu.cpp src/IntelMemory.cpp src/IntelVideoCard.cpp src/Computer.cpp -Iinclude
四.电脑组装项目:方法一: Computer类中调用子类的析构函数
include文件下
AbstractCpu.h
#pragma once
#include<iostream>
using namespace std;
class AbstractCpu
{
public:
AbstractCpu()
{
cout<<"我调用了AbstractCpu的构造函数"<<endl;
}
virtual void calculate() = 0;
virtual ~AbstractCpu()
{
cout<<"我调用了AbstractCpu的析构函数"<<endl;
}
};
AbstractMemory.h
#pragma once
#include<iostream>
using namespace std;
class AbstractMemory
{
public:
AbstractMemory()
{
cout<<"我调用了AbstractMemory的构造函数"<<endl;
}
virtual void storage() = 0;
virtual ~AbstractMemory()
{
cout<<"我调用了AbstractMemory的析构函数"<<endl;
}
};
AbstractVideoCard.h
#pragma once
#include<iostream>
using namespace std;
class AbstractVideoCard
{
public:
AbstractVideoCard()
{
cout<<"我调用了AbstractVideoCard的构造函数"<<endl;
}
virtual void display() = 0;
virtual ~AbstractVideoCard()
{
cout<<"我调用了AbstractVideoCard的构造函数"<<endl;
}
};
Computer.h
#pragma once
#include <iostream>
#include "AbstractCpu.h"
#include "AbstractMemory.h"
#include "AbstractVideoCard.h"
using namespace std;
class Computer
{
private:
AbstractCpu* m_cpu;
AbstractVideoCard* m_vc;
AbstractMemory* m_mem;
public:
Computer(AbstractCpu* cpu,AbstractVideoCard* video_card,AbstractMemory* memory)
{
cout<<"我调用了Computer的构造函数"<<endl;
this->m_cpu = cpu;
this->m_vc = video_card;
this->m_mem = memory;
}
void run();
//如果不在头文件中写函数的实现,在cpp文件中写文件的实现则必须声明。
~Computer();
};
IntelCpu.h
#pragma once
#include "AbstractCpu.h"
#include<iostream>
using namespace std;
class IntelCpu: public AbstractCpu
{
public:
IntelCpu()
{
cout<<"我调用了IntelCPU的构造函数"<<endl;
}
~IntelCpu()
{
cout<<"我调用了IntelCPU的西狗函数"<<endl;
}
void calculate();
};
IntelMemory.h
#pragma once
#include "AbstractMemory.h"
#include<iostream>
using namespace std;
class IntelMemory: public AbstractMemory
{
public:
IntelMemory()
{
cout<<"我调用了IntelMemory的构造函数"<<endl;
}
~IntelMemory()
{
cout<<"我调用了IntelMemory的西狗函数"<<endl;
}
void storage();
};
IntelVideoCard.h
#pragma once
#include "AbstractVideoCard.h"
#include "iostream"
using namespace std;
class IntelVideoCard: public AbstractVideoCard
{
public:
IntelVideoCard()
{
cout<<"我调用了IntelVideoCard的构造函数"<<endl;
}
~IntelVideoCard()
{
cout<<"我调用了IntelVideoCard的西狗函数"<<endl;
}
void display();
};
src
Computer.cpp
#include "Computer.h"
void Computer::run()
{
m_cpu->calculate();
m_vc->display();
m_mem->storage();
}
Computer::~Computer()
{
cout<<"我调用了Computer的西狗函数"<<endl;
if(m_cpu!=NULL)
{
cout<<"delete m_cpu"<<endl;
delete m_cpu;
m_cpu = NULL;
}
if(m_vc!=NULL)
{
cout<<"delete m_vc"<<endl;
delete m_vc;
m_vc = NULL;
}
if(m_mem!=NULL)
{
cout<<"delete m_mem"<<endl;
delete m_mem;
m_mem = NULL;
}
}
IntelCpu.cpp
#include "IntelCpu.h"
void IntelCpu::calculate()
{
cout<<"Intel的CPU开始计算了"<<endl;
}
IntelMemory.cpp
#include "IntelMemory.h"
void IntelMemory::storage()
{
cout<<"Intel的memory开始计算了"<<endl;
}
IntelVideoCard.cpp
#include "IntelVideoCard.h"
void IntelVideoCard::display()
{
cout<<"Intel的VideoCard开始计算了"<<endl;
}
main.cpp
#include <iostream>
#include "Computer.h"
#include "AbstractCpu.h"
#include "AbstractMemory.h"
#include "AbstractVideoCard.h"
#include "IntelCpu.h"
#include "IntelMemory.h"
#include "IntelVideoCard.h"
using namespace std;
int main()
{
AbstractCpu* cpu = new IntelCpu;
AbstractMemory* memory = new IntelMemory;
AbstractVideoCard* videocard = new IntelVideoCard;
//为什么这样报错?
// Computer c = Computer(cpu,memory,videocard);
Computer* computer = new Computer(cpu,videocard,memory);
computer->run();
delete computer;
return 0;
}
五.电脑组装项目:方法二: main文件中delete创建的子类
只需要修改main.cpp, Computer的头文件对析构函数的声明和Computer.cpp
mian.cpp
#include <iostream>
#include "Computer.h"
#include "AbstractCpu.h"
#include "AbstractMemory.h"
#include "AbstractVideoCard.h"
#include "IntelCpu.h"
#include "IntelMemory.h"
#include "IntelVideoCard.h"
using namespace std;
int main()
{
AbstractCpu* cpu = new IntelCpu;
AbstractMemory* memory = new IntelMemory;
AbstractVideoCard* videocard = new IntelVideoCard;
Computer* computer = new Computer(cpu,videocard,memory);
computer->run();
delete cpu;
delete memory;
delete videocard;
delete computer;
return 0;
}
Computer.h
#pragma once
#include <iostream>
#include "AbstractCpu.h"
#include "AbstractMemory.h"
#include "AbstractVideoCard.h"
using namespace std;
class Computer
{
private:
AbstractCpu* m_cpu;
AbstractVideoCard* m_vc;
AbstractMemory* m_mem;
public:
Computer(AbstractCpu* cpu,AbstractVideoCard* video_card,AbstractMemory* memory)
{
cout<<"我调用了Computer的构造函数"<<endl;
this->m_cpu = cpu;
this->m_vc = video_card;
this->m_mem = memory;
}
void run();
//如果不在头文件中写函数的实现,在cpp文件中写文件的实现则必须声明。
// ~Computer();
};
Computer.cpp
#include "Computer.h"
void Computer::run()
{
m_cpu->calculate();
m_vc->display();
m_mem->storage();
}
// Computer::~Computer()
// {
// cout<<"我调用了Computer的西狗函数"<<endl;
// if(m_cpu!=NULL)
// {
// cout<<"delete m_cpu"<<endl;
// delete m_cpu;
// m_cpu = NULL;
// }
// if(m_vc!=NULL)
// {
// cout<<"delete m_vc"<<endl;
// delete m_vc;
// m_vc = NULL;
// }
// if(m_mem!=NULL)
// {
// cout<<"delete m_mem"<<endl;
// delete m_mem;
// m_mem = NULL;
// }
// }