类,函数,声明.

#include "bits/stdc++.h"
using namespace std;

enum CPU_Rank{P1 = 1,P2,P3,P4,P5,P6,P7};

class CPU {
public:
    CPU(CPU_Rank rank,int frequency,float voltnumber)
         : rank(rank),frequency(frequency),voltnumber(voltnumber){//初始化列表
        cout<< &quot;CPU's constructor is called!&quot; <<endl;
    }

    ~CPU() {//析构函数 清理对象资源,销毁对象
        cout<< &quot;CPU's destructor is called!&quot; <<endl;
    }

    void run(){
        cout<<&quot;I am working, my rank is &quot; << rank
        << &quot;, my frequency is &quot; << frequency
        <<&quot;, my voltnumber is &quot; << fixed << setprecision(1) << voltnumber << &quot;.&quot; << endl;
    }

    void stop(){
        cout << &quot;I stopped working!&quot; << endl;
    }
private://私有成员
    CPU_Rank rank;
    int frequency;
    float voltnumber;
};

int main(){
    int rank, frequency;
    float voltnumber;
    cin >> rank >> frequency >> voltnumber;

    CPU cpu((CPU_Rank)rank,frequency,voltnumber);// 将整数转换为 CPU_Rank 类型
    cpu.run();
    cpu.stop();
    // 对象销毁时,析构函数自动调用
    return 0;
}
全部评论

相关推荐

06-04 09:27
门头沟学院 Java
点赞 评论 收藏
分享
评论
点赞
1
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务