1. 程式人生 > >C++組合類的簡單練習

C++組合類的簡單練習

uil 組合 code lin ios 提升 stream set enum

這是第一次在博客上發表隨筆,希望可以敦促自己不要浪費時間,努力提升基本功。
 1 #include <iostream>
 2 
 3 class CPU
 4 {
 5     enum CPU_rank{p1=1,p2,p3,p4,p5,p6,p7};
 6 public:
 7     CPU()= default;
 8     CPU(enum CPU_rank);
 9     ~CPU(){}
10     void run(){std::cout<<"CPU運行";}
11     void stop(){std::cout<<"CPU停止"
;} 12 }; 13 14 enum RAM_type {DDR4=1,DDR3,DDR2}; 15 class RAM 16 { 17 enum RAM_type type; 18 int size; 19 float ping; 20 public: 21 RAM()= default; 22 RAM(enum RAM_type,int size,float ping); 23 ~RAM(){} 24 void setType(RAM_type t){ type=t;} 25 inline void run(){std::cout<<"
RAM運行";} 26 inline void stop(){std::cout<<"RAM停止";} 27 }; 28 29 enum CD_ROM_type{SATA,USB}; 30 class CD_ROM 31 { 32 33 int size; 34 enum install{external=1,built_in}; 35 public: 36 CD_ROM()= default; 37 CD_ROM(enum install,enum CD_ROM_type, int size); 38 ~CD_ROM(){} 39
inline void run(){std::cout<<"CD_ROM運行/n";} 40 inline void stop(){std::cout<<"CD_ROM停止/n";} 41 }; 42 43 class COMPUTER 44 { 45 CPU myCpu; 46 RAM myRam; 47 CD_ROM myCdRom; 48 float prize; 49 public: 50 COMPUTER()= default; 51 COMPUTER(CPU c,RAM r,CD_ROM cd,float prize=0) 52 :myCpu(c),myCdRom(cd),myRam(r) 53 { 54 std::cout<<"COMPUTER創建成功"; 55 } 56 ~COMPUTER(){} 57 inline void run(){std::cout<<"COMPUTER運行";} 58 inline void stop(){std::cout<<"COMPUTER停止";} 59 }; 60 61 int main() { 62 RAM myram; 63 myram.setType(DDR2); 64 COMPUTER mycomputer; 65 mycomputer.run(); 66 mycomputer.stop(); 67 }

C++組合類的簡單練習