完整代码:
/* * @Author: Stylle * @Date: 2020-08-20 21:05:50 * @LastEditors: Stylle * @LastEditTime: 2020-08-21 19:29:10 * @FilePath: \C-_learningc:\Users\23999\Desktop\text01.cpp */ #include <iostream> using namespace std; //p 显卡的抽象类 class videoCard { public: virtual void videoWork() = 0; }; //? amd显卡的继承 这里就体现了多态的好处如果市场加入新的品牌只需要继承到CPU类中即可 class AmdVideoCard : public videoCard { public: virtual void videoWork() { cout << "AMD的显卡开始工作了" << endl; } }; //? intel显卡的继承 class IntelVideoCard : public videoCard { public: virtual void videoWork() { cout << "Intel的显卡开始工作了" << endl; } }; //p cpu的抽象类 class cpu { public: virtual void cpuWork() = 0; }; //? amd的cpu的继承 这里就体现了多态的好处如果市场加入新的品牌只需要继承到CPU类中即可 class AmdCpu : public cpu { public: virtual void cpuWork() { cout << "AMD的CPU开始工作了" << endl; } }; //? intel的cpu的继承 class IntelCpu : public cpu { public: virtual void cpuWork() { cout << "Intel的CPU开始工作了" << endl; } }; //p 内存条的抽象类 class memory { public: virtual void memoryWork() = 0; }; //? amd的内存条的继承 这里就体现了多态的好处如果市场加入新的品牌只需要继承到CPU类中即可 class AmdMemory : public memory { public: virtual void memoryWork() { cout << "AMD的内存条开始工作了" << endl; } }; //? intel的内存条的继承 class IntelMemory : public memory { public: virtual void memoryWork() { cout << "Intel的内存条开始工作了" << endl; } }; class computer { public: //电脑组装 computer(cpu *CPU, videoCard *VideoCard, memory *Memory) { this->myCpu = CPU; this->MyMemory = Memory; this->MyVideoCard = VideoCard; } //释放申请的机箱内存 ~computer() { if (myCpu != NULL) { delete myCpu; myCpu = NULL; //防止野指针出现 } if (MyMemory != NULL) { delete MyMemory; MyMemory = NULL; } if (MyVideoCard != NULL) { delete MyVideoCard; MyVideoCard = NULL; } } //电脑开机工作 void work() { this->myCpu->cpuWork(); this->MyMemory->memoryWork(); this->MyVideoCard->videoWork(); cout << endl; } private: memory *MyMemory; videoCard *MyVideoCard; cpu *myCpu; }; int main(int arcg, char **argv) { cout << "组装电脑方式1" << endl; computer *MyComputer1 = new computer(new IntelCpu, new IntelVideoCard, new IntelMemory); MyComputer1->work(); delete MyComputer1; cout << "组装电脑方式2" << endl; cpu *MyCpu = new IntelCpu; videoCard *MyVideo = new AmdVideoCard; memory *Mymemory = new IntelMemory; computer MyComputer2(MyCpu, MyVideo, Mymemory); MyComputer2.work(); return 0; }
电脑单件拆分(CPU):
//p cpu的抽象类基类 class cpu { public: virtual void cpuWork() = 0; }; //? amd的cpu的继承 这里就体现了多态的好处如果市场加入新的品牌只需要继承到CPU类中即可 class AmdCpu : public cpu { public: virtual void cpuWork() { cout << "AMD的CPU开始工作了" << endl; } }; //? intel的cpu的继承 class IntelCpu : public cpu { public: virtual void cpuWork() { cout << "Intel的CPU开始工作了" << endl; } };
电脑单件拆分(显卡):
//p 显卡的抽象类基类 class videoCard { public: virtual void videoWork() = 0; }; //? amd显卡的继承 这里就体现了多态的好处如果市场加入新的品牌只需要继承到CPU类中即可 class AmdVideoCard : public videoCard { public: virtual void videoWork() { cout << "AMD的显卡开始工作了" << endl; } }; //? intel显卡的继承 class IntelVideoCard : public videoCard { public: virtual void videoWork() { cout << "Intel的显卡开始工作了" << endl; } };
电脑单件拆分(内存条):
//p 内存条的抽象类基类 class memory { public: virtual void memoryWork() = 0; }; //? amd的内存条的继承 这里就体现了多态的好处如果市场加入新的品牌只需要继承到CPU类中即可 class AmdMemory : public memory { public: virtual void memoryWork() { cout << "AMD的内存条开始工作了" << endl; } }; //? intel的内存条的继承 class IntelMemory : public memory { public: virtual void memoryWork() { cout << "Intel的内存条开始工作了" << endl; } };
总结:在电脑类中申请了三个组件的指针,所以在电脑类中析构函数中就需要释放指针内存并将其指向NULL防止野指针的出现,在这个程序中完美的体现了C++多态属性的好处,如果市场加入新的品牌只需要继承到单独的组件类中即可。