要求:
分析:
代码:
#include<iostream>
using namespace std;
//3个抽象零件类
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;
}
~Computer()
{
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;
}
}
void work()
{
m_cpu->calculate();
m_vc->display();
m_mem->storage();
}
private:
CPU* m_cpu;
VideoCard* m_vc;
Memory* m_mem;
};
class IntelCpu : public CPU
{
void calculate()
{
cout << "Intel的CPU开始运行了" << endl;
}
};
class IntelVideoCard : public VideoCard
{
void display()
{
cout << "Intel的VideoCard开始运行了" << endl;
}
};
class IntelMemory : public Memory
{
void storage()
{
cout << "Intel的Memory开始存储了" << endl;
}
};
void test01()
{
CPU* intelCpu = new IntelCpu;
VideoCard* intelCard = new IntelVideoCard;
Memory* intelMemory = new IntelMemory;
Computer* computer = new Computer(intelCpu, intelCard, intelMemory);
computer->work();
}
int main() {
test01();
}