#include<iostream>
#include<string>
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_mem = mem;
m_vc = vc;
}
//提供工作的函数
void work()
{ //让零件工作起来 调用接口
m_cpu->calculate();
m_vc->display();
m_mem->storage();
}
//提供析构函数 释放3个电脑零件
~Computer()
{
//释放CPU零件
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 start work" << endl;
}
};
class IntelVideoCard :public VideoCard
{
public:
virtual void display()
{
cout << "IntelVideoCard start work" << endl;
}
};
class IntelMemory :public Memory
{
public:
virtual void storage()
{
cout << "IntelMemory start work" << endl;
}
};
//lenovo厂商
class LenovoCPU :public CPU
{
public:
virtual void calculate()
{
cout << "LenovoCPU start work" << endl;
}
};
class LenovoVideoCard :public VideoCard
{
public:
virtual void display()
{
cout << "LenovoVideoCard start work" << endl;
}
};
class LenovoMemory :public Memory
{
public:
virtual void storage()
{
cout << "LenovoMemory start work" << endl;
}
};
void test01()
{
//第一台电脑零件
CPU* intelCPU = new IntelCPU; //父类指针指向子类对象
VideoCard* intelvidoecard = new IntelVideoCard;
Memory* intelmemory = new IntelMemory;
//创建第一台电脑
Computer* computer1 = new Computer(intelCPU, intelvidoecard, intelmemory);
cout << "the first computer start work" << endl;
computer1->work();
delete computer1;
//第二台电脑组装
Computer* computer2 = new Computer(new LenovoCPU, new LenovoVideoCard, new LenovoMemory);
cout << "------------------" << endl;
cout << "the second computer start work" << endl;
//第二台电脑工作
computer2->work();
delete computer2;
//第三台电脑组装
Computer* computer3 = new Computer(new IntelCPU, new LenovoVideoCard, new IntelMemory);
cout << "------------------" << endl;
cout << "the third computer start work" << endl;
//第三台电脑工作
computer3->work();
delete computer3;
}
int main()
{
test01();
system("pause");//按任意键继续
return 0;//关闭程序
}