C++ //多态案例三 ---电脑组装
1 //多态案例三 ---电脑组装 2 3 #include <iostream> 4 #include <string> 5 using namespace std; 6 7 //抽象不同零件类 8 //抽象CPU类 9 10 class CPU 11 { 12 public: 13 //抽象的计算函数 14 virtual void calculate() = 0; 15 16 17 18 19 20 }; 21 //抽象显卡类 22 class VideoCard 23 { 24 public: 25 //抽象的显示函数 26 virtual void display() = 0; 27 28 29 30 31 32 }; 33 34 //抽象内存条类 35 class Memory 36 { 37 public: 38 //抽象的存储函数 39 virtual void storage() = 0; 40 41 42 43 44 45 }; 46 47 48 //电脑类 49 class Computer 50 { 51 public: 52 Computer(CPU* cpu, VideoCard* vc, Memory* mem) 53 { 54 m_cpu = cpu; 55 m_mem = mem; 56 m_vc = vc; 57 } 58 59 //提供工作的函数 60 void work() 61 { 62 63 //让零件工作起来,调用接口 64 m_cpu->calculate(); 65 66 m_vc->display(); 67 68 m_mem->storage(); 69 } 70 71 //提供析构函数 释放3个电脑零件 72 ~Computer() 73 { 74 //释放CPU零件 75 if (m_cpu != NULL) 76 { 77 delete m_cpu; 78 m_cpu = NULL; 79 } 80 //释放显卡零件 81 if (m_vc != NULL) 82 { 83 delete m_vc; 84 m_vc = NULL; 85 } 86 //释放内存零件 87 if (m_mem != NULL) 88 { 89 delete m_mem; 90 m_mem = NULL; 91 } 92 93 } 94 95 private: 96 97 CPU* m_cpu; //CPU的零件指针 98 VideoCard* m_vc; //显卡的零件指针 99 Memory* m_mem; //内存条零件指针 100 101 102 }; 103 //具体厂商 104 //Intel 厂商 105 106 class IntelCPU :public CPU 107 { 108 public: 109 virtual void calculate() 110 { 111 cout << "Intel的CPU开始计算了" << endl; 112 } 113 }; 114 115 class IntelVideoCard :public VideoCard 116 { 117 public: 118 virtual void display() 119 { 120 cout << "Intel的显卡开始显示了" << endl; 121 } 122 }; 123 124 class IntelMemory :public Memory 125 { 126 public: 127 virtual void storage() 128 { 129 cout << "Intel的内存条开始储存了" << endl; 130 } 131 }; 132 133 134 //Lenovo厂商 135 class LenovoCPU :public CPU 136 { 137 public: 138 virtual void calculate() 139 { 140 cout << "Lenovo的CPU开始计算了" << endl; 141 } 142 }; 143 144 class LenovoVideoCard :public VideoCard 145 { 146 public: 147 virtual void display() 148 { 149 cout << "Lenovo的显卡开始显示了" << endl; 150 } 151 }; 152 153 class LenovoMemory :public Memory 154 { 155 public: 156 virtual void storage() 157 { 158 cout << "Lenovo的内存条开始储存了" << endl; 159 } 160 }; 161 162 void test01() 163 { 164 //第一台电脑的零件 165 cout << "创建第一台电脑" << endl; 166 CPU* intelCpu = new IntelCPU; 167 VideoCard* intelCard = new IntelVideoCard; 168 Memory* intelMem = new IntelMemory; 169 170 //创建第一台电脑 171 Computer* computer1 = new Computer(intelCpu,intelCard,intelMem); 172 computer1->work(); 173 delete computer1; 174 175 //delete intelCpu; 176 //delete intelCard; 177 //delete intelMem; 178 179 //第二台电脑的组装 180 181 //CPU* lenovoCpu = new LenovoCPU; 182 //VideoCard* lenovoCard = new LenovoVideoCard; 183 //Memory* lenovoMem = new LenovoMemory; 184 185 186 //Computer* computer2 = new Computer(lenovoCpu, lenovoCard, lenovoMem); 187 //computer2->work(); 188 //delete computer2; 189 cout << "*************************************"<<endl; 190 cout << "创建第二台电脑" << endl; 191 192 //第二台电脑的组装 193 Computer* computer2 = new Computer(new LenovoCPU, new LenovoVideoCard, new LenovoMemory); 194 computer2->work(); 195 delete computer2; 196 197 198 cout << "*************************************" << endl; 199 cout << "创建第三台电脑" << endl; 200 201 //第二台电脑的组装 202 Computer* computer3 = new Computer(new LenovoCPU, new IntelVideoCard, new LenovoMemory); 203 computer3->work(); 204 delete computer3; 205 206 207 208 209 } 210 211 int main() 212 { 213 214 test01(); 215 216 217 system("pause"); 218 219 return 0; 220 221 }
本文来自博客园,作者:Bytezero!,转载请注明原文链接:https://www.cnblogs.com/Bytezero/p/15117891.html