C++多态实现电脑组装

#include<iostream>
using namespace std;

class CPU {
public:
virtual void nCPU() = 0;
};
class VideoCard {
public:
virtual void nVideoCard() = 0;
};
class Memorry {
public:
virtual void nMemory() = 0;
};
class computer{
private :
CPU* cpu;
public:
computer(CPU * cpu) {
this->cpu = cpu;
}
void work() {
cpu->nCPU();
}
~computer() {
if (cpu != NULL) {
delete cpu;
cpu = NULL;
}
}
};

class InerNetCpu :public CPU {
void nCPU() {
cout<<"inter的CPU"<<endl;
}
};

void test() {
CPU* intercpu = new InerNetCpu;

computer* c1 = new computer(intercpu);
c1->work();

}


int main() {
test();

system("pause");
return 0;

}

posted @ 2021-01-31 10:39  云小道  阅读(48)  评论(0编辑  收藏  举报