代码拉取完成,页面将自动刷新
同步操作将从 GodOuO/Code_C++ 强制同步,此操作会覆盖自 Fork 仓库以来所做的任何修改,且无法恢复!!!
确定后同步将在后台操作,完成时将刷新页面,请耐心等待。
#include<iostream>
#include<string>
using namespace std;
/* 多态训练
电脑组件:CPU,显卡,内存条
计算,显示,储存
每个零件抽象基类,不同厂商:Intel,Lenovo
提供电脑工作函数,调用每个零件工作接口
测试组装三台电脑
*/
//零件层,都抽象纯虚函数,基类(多态)
class CPU
{
private:
/* data */
public:
virtual void calculate() = 0; //抽象纯虚函数
virtual ~CPU(){}
};
class VideoCard
{
private:
/* data */
public:
virtual void display() = 0; //抽象纯虚函数
virtual ~VideoCard(){}
};
class Memory
{
private:
/* data */
public:
virtual void storage() = 0; //抽象纯虚函数
virtual ~Memory (){}
};
//电脑类,三个指针接收零件,通过函数使零件工作
class PC
{
private:
CPU * m_cpu;
VideoCard *m_vc;
Memory *m_mem;
public:
PC(CPU *cpu,VideoCard *vc,Memory *mem); //(多态)
void work(); //父类指针 调用接口
~PC(){ //手动 释放电脑三零件
if(m_cpu != NULL || m_mem != NULL || m_vc != NULL){
delete m_cpu;
m_cpu = NULL;
delete m_mem;
m_mem = NULL;
delete m_vc;
m_vc = NULL;
}
}
};
class IntelCPU :public CPU
{
private:
/* data */
public:
virtual void calculate() {
cout<<"IntelCPU is Calculating..."<<endl;
}
};
class IntelVideoCard :public VideoCard
{
private:
/* data */
public:
virtual void display() {
cout<<"IntelVideoCard is displaying..."<<endl;
}
};
class IntelMemory :public Memory
{
private:
/* data */
public:
virtual void storage() {
cout<<"IntelMemory is storaging..."<<endl;
}
};
class LenovoCPU :public CPU
{
private:
/* data */
public:
virtual void calculate() {
cout<<"LenovoCPU is Calculating..."<<endl;
}
};
class LenovoVideoCard :public VideoCard
{
private:
/* data */
public:
virtual void display() {
cout<<"LenovoVideoCard is displaying..."<<endl;
}
};
class LenovoMemory :public Memory
{
private:
/* data */
public:
virtual void storage() {
cout<<"LenovoMemory is storaging..."<<endl;
}
};
PC::PC(CPU *cpu,VideoCard *vc,Memory *mem)
{
m_cpu = cpu;
m_vc = vc;
m_mem = mem;
}
void PC::work(){
m_cpu->calculate();
m_vc->display();
m_mem->storage();
}
void test01(){
CPU * intelCPU = new IntelCPU;
VideoCard * videoCard = new IntelVideoCard;
Memory * memory = new IntelMemory;
cout<<"PC1----------------------"<<endl;
PC * pc = new PC(intelCPU,videoCard,memory);
pc->work();
delete pc;
cout<<"PC2----------------------"<<endl;
pc = new PC(new LenovoCPU,new LenovoVideoCard,new LenovoMemory);
pc->work();
delete pc;
cout<<"PC3----------------------"<<endl;
pc = new PC(new IntelCPU,new LenovoVideoCard,new IntelMemory);
pc->work();
delete pc;
}
int main(){
test01();
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。