CPU类的定义和使用——9-02.cpp 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. #include <iostream>
  2. #include <string>
  3. enum CPU_Rank { P1 = 1, P2, P3, P4, P5, P6, P7 };
  4. class CPU {
  5. private:
  6. CPU_Rank rank;
  7. int frequency;
  8. float voltage;
  9. public:
  10. CPU(CPU_Rank r = P1, int f = 100, float v = 2.0) : rank(r), frequency(f), voltage(v) {
  11. std::cout << "one CPU is created!" << std::endl;
  12. }
  13. ~CPU() {
  14. std::cout << "one CPU is distoried!" << std::endl;
  15. }
  16. void display() {
  17. std::string rank_str;
  18. switch (rank) {
  19. case P1: rank_str = "P1"; break;
  20. case P2: rank_str = "P2"; break;
  21. case P3: rank_str = "P3"; break;
  22. case P4: rank_str = "P4"; break;
  23. case P5: rank_str = "P5"; break;
  24. case P6: rank_str = "P6"; break;
  25. case P7: rank_str = "P7"; break;
  26. default: rank_str = "Unknown Rank";
  27. }
  28. std::cout << "rank: " << rank_str << std::endl;
  29. std::cout << "frequency: " << frequency << std::endl;
  30. std::cout << "voltage: " << voltage << std::endl;
  31. }
  32. void run() {
  33. std::cout << "CPU is running!" << std::endl;
  34. }
  35. void stop() {
  36. std::cout << "CPU stop!" << std::endl;
  37. }
  38. };
  39. int main() {
  40. int rank_input, frequency_input;
  41. float voltage_input;
  42. std::cin >> rank_input >> frequency_input >> voltage_input;
  43. CPU myCPU(static_cast<CPU_Rank>(rank_input), frequency_input, voltage_input);
  44. myCPU.run();
  45. myCPU.display();
  46. myCPU.stop();
  47. return 0;
  48. }