01电脑组装me.cpp
需要实现所有的虚函数,不台灵活。架构函数无法写,设计不够成熟
#if 1
#include<iostream>
using namespace std;
class Computer {
public:virtual void caculate() = 0;virtual void display() = 0;virtual void storage() = 0;
};class Inter :public Computer {
public:virtual void caculate() {cout << "英特尔cpu" << endl;}virtual void display() {cout << "英特尔card" << endl;}virtual void storage() {cout << "英特尔内存条" << endl;}
};
class NVIDA :public Computer {
public:virtual void caculate() {cout << "NVIDAcpu" << endl;}virtual void display() {cout << "NVIDAcard" << endl;}virtual void storage() {cout << "NVIDA内存条" << endl;}
};
class Kingston :public Computer {
public:virtual void caculate() {cout << "Kingstoncpu" << endl;}virtual void display() {cout << "Kingstoncard" << endl;}virtual void storage() {cout << "Kingston内存条" << endl;}
};//架构函数不会写
//void OperatorComputer(Computer *cpu, Computer *card, Computer *storage) {
// cpu->caculate();
// card->display();
// storage->storage();
//}int main() {Computer *inter = new Inter;Computer *nvida= new NVIDA;Computer *kingston = new Kingston;//第一台电脑inter->caculate();inter->display();inter->storage();//第二台电脑inter->caculate();kingston->storage();nvida->display();}
/*
英特尔cpu
英特尔card
英特尔内存条
英特尔cpu
Kingston内存条
NVIDAcard
*/
#endif
02电脑组装ok.cpp
#if 1
#include<iostream>
using namespace std;//-----抽象层---------
class CPU {
public:virtual void caculate() = 0;virtual ~CPU() {}
};
class Card {
public:virtual void display() = 0;virtual ~Card() {}
};
class Memory {
public:virtual void storage() = 0;virtual ~Memory() {}
};
//要去重写所有的虚函数。多台电脑多次执行。 设计不合理
//class Computer :public CPU, public Card, public Memory {
//
//};//架构类
class Computer {
public:Computer(CPU *cpu, Card *card, Memory *memory) {this->card = card;this->cpu = cpu;this->memory = memory;}void work() {this->card->display();this->cpu->caculate();this->memory->storage();}~Computer() {cout << "~Computer" << endl;if (this->cpu != NULL) {delete this->cpu;this->cpu = NULL;}if (this->card != NULL) {delete this->card;this->card = NULL;}if (this->memory != NULL) {delete this->memory;this->memory = NULL;}}
private:CPU *cpu;Card *card;Memory *memory;
};//-----------实现层-----------class InterCPU :public CPU {
public:virtual void caculate() {cout << "InterCPU" << endl;}~InterCPU() {cout << "~InterCPU()" << endl;}
};
class InterCard :public Card {
public:virtual void display() {cout << "InterCard" << endl;}~InterCard() {cout << "~InterCard()" << endl;}
};
class InterMem :public Memory {
public:virtual void storage() {cout << "Intermemory" << endl;}~InterMem() {cout << "~InterMem()" << endl;}
};class NVIDACard :public Card {
public:virtual void display() {cout << "NVIDACard" << endl;}~NVIDACard() {cout << "~NVIDACard()" << endl;}
};
class KingtonMem :public Memory {
public:virtual void storage() {cout << "Kingtonmemory" << endl;}~KingtonMem() {cout << "~KingtonMem()" << endl;}
};//-------------业务层-----------
void test01() {//1 组装第一台inter系列电脑CPU *interCpu = new InterCPU;Card *interCard = new InterCard;Memory *interMem = new InterMem;Computer c1(interCpu, interCard, interMem); c1.work();//delete interCpu; //在Computer中已经完成对interCpu的析构,//所以再次delete程序崩溃//delete c1; //c1不是在堆上,所以不能用delete//cout << endl;2 组装第二台电脑//Card *nvidaCard = new NVIDACard;//Memory *kingtonMem = new KingtonMem;//Computer c2(interCpu, nvidaCard, kingtonMem);//c2.work();/*delete interCpu;delete interCard;delete interMem;*/
}
/*
InterCard
InterCPU
Intermemory
~Computer
~InterCPU()
~InterCard()
~InterMem()
*/
void test02() { //传递匿名//1 组装第一台inter系列电脑Computer *c1=new Computer(new InterCPU, new InterCard, new InterMem);c1->work();delete c1; //调用析构函数
}
/*
InterCard
InterCPU
Intermemory
~Computer
~InterCPU()
~InterCard()
~InterMem()
*/
int main() {//test01();test02();return 0;
}
#endif