#include<iostream>
#include<cstdlib>
#include<ctime>
#include<string>
#include<GL/glew.h>
#include <GL/glut.h>
#include<GLFW/glfw3.h>
#define MAX 1000
using namespace std;
#define numVAOs 1
class CPU
{
public:
virtual void calculator() = 0;
};
class VideoCard
{
public:
virtual void display() = 0;
};
class Memory
{
public:
virtual void storage() = 0;
};
class Computer
{
//构造函数中传入三个零件的指针,提供一个工作的函数,调用每个零件工作的接口
public:
Computer(CPU* cp_u, VideoCard* vc, Memory* me)
{
c_pu = cp_u;
v_c = vc;
m_e = me;
}
void work()
{
c_pu->calculator();
v_c->display();
m_e->storage();
}
~Computer()
{
if (c_pu != NULL)
{
delete c_pu;
c_pu = NULL;
}
else if (v_c != NULL)
{
delete v_c;
v_c = NULL;
}
else if (m_e != NULL)
{
delete m_e;
m_e = NULL;
}
}
private:
CPU* c_pu;
VideoCard* v_c;
Memory* m_e;
};
//厂商
//Inter
class InterCPU :public CPU
{
public:
virtual void calculator()
{
cout << "Inter的CPU在计算" << endl;
}
};
class InterVideocard :public VideoCard
{
public:
virtual void display()
{
cout << "Inter的显卡在显示" << endl;
}
};
class InterMemory :public Memory
{
public:
virtual void storage()
{
cout << "Inter的内存条在存储" << endl;
}
};
//AMD
class AMDCPU :public CPU
{
public:
virtual void calculator()
{
cout << "AMD的CPU在计算" << endl;
}
};
class AMDVideocard :public VideoCard
{
public:
virtual void display()
{
cout << "AMD的显卡在显示" << endl;
}
};
class AMDMemory :public Memory
{
public:
virtual void storage()
{
cout << "AMD的内存条在存储" << endl;
}
};
void test01()
{
CPU* interCpu = new InterCPU;
VideoCard* interCard = new InterVideocard;
Memory* interMem = new InterMemory;
CPU* amdCpu = new AMDCPU;
VideoCard* amdCard = new AMDVideocard;
Memory* amdMem = new AMDMemory;
//创建第一台电脑
Computer* computer1 = new Computer(interCpu, interCard, interMem);
computer1->work();
delete computer1;
//创建第二台电脑
cout << "-------------------" << endl;
cout << "第二台电脑开始工作" << endl;
Computer* computer2 = new Computer(amdCpu, amdCard, amdMem);
computer2->work();
delete computer2;
//创建第三台电脑
cout << "-------------------" << endl;
cout << "第三台电脑开始工作" << endl;
Computer* computer3 = new Computer(amdCpu, interCard, interMem);
computer3->work();
delete computer3;
}
int main()
{
test01();
}
【C++】利用多态进行电脑组装
最新推荐文章于 2024-08-12 16:55:46 发布
本文探讨了计算机硬件组件的抽象模型,通过CPU、VideoCard和Memory类的组合,展示了不同厂商(Inter和AMD)硬件在计算机中的协作。Inter和AMD的实例展示了各自的计算、显示和存储功能,通过Computer类的工作流程体现了它们的交互作用。
摘要由CSDN通过智能技术生成