#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();
}
~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;//内存条指针
};
class intelCPU :public CPU
{
virtual void calcualte()
{
cout << "intel的CPU开始计算" << endl;
}
};
class intelVideoCard :public VideoCard
{
virtual void dispaly()
{
cout << "intel的显卡开始计算" << endl;
}
};
class intelMemory :public Memory
{
virtual void storage()
{
cout << "intel的内存条开始计算" << endl;
}
};
void test01()
{
//创建零件
CPU * intelCPU = new intelCPU ;
VideoCard* intelCard = new intelVideoCard;
Memory * intelMem = new intelMemory;
cout << "开始工作" << endl;
//创建电脑
Computer * coumputer1 = new Computer(intelCPU, intelCard, intelMem);
coumputer1 -> work();
delete coumputer1;
}
int main() {
test01();
system("pause");
return 0;
}
创建零件部分错误
3个中Memory成功,其他两个错误
求解