CPU类的定义和使用——9-02.cpp 946 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. #include <iostream>
  2. using namespace std;
  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. // 构造函数
  11. CPU(CPU_Rank r = P1, int f = 100, float v = 2.0) : rank(r), frequency(f), voltage(v) {
  12. cout << "one CPU is created!" << endl;
  13. }
  14. // 析构函数
  15. ~CPU() {
  16. cout << "one CPU is distoried!" << endl;
  17. }
  18. // 运行
  19. void run() {
  20. cout << "CPU is running!" << endl;
  21. }
  22. // 停止
  23. void stop() {
  24. cout << "CPU stop!" << endl;
  25. }
  26. // 显示CPU信息
  27. void display() const {
  28. cout << "rank:P" << rank << endl;
  29. cout << "frequency:" << frequency << endl;
  30. cout << "voltage:" << voltage << endl;
  31. }
  32. };
  33. int main() {
  34. int f;
  35. float v;
  36. cin >> f >> v;
  37. CPU myCPU(P6, f, v);
  38. myCPU.run();
  39. myCPU.display();
  40. myCPU.stop();
  41. return 0;
  42. }