编写一个名为cpu的类,描述一个cpu的以下信息:时针频率,最大不会超过3000MHz;字长,可以是32位或64位;核数,可以是单核,双核或四核;是否支持超线程。各项信息要求使用位域来表示。通过输出sizeof(cpu)来观察该类所占的字节数
#include <iostream>
using namespace std;
enum core{single,dual,quad};
enum words{bit32,bit64};
enum hyperthread{support,notsupport};
class cpu{
public:
cpu(unsigned frequence,core type,words length,hyperthread mode)
:frequence(frequence),coretype(type),wordlen(length),mode(mode){}
void show();
private:
unsigned frequence:32;
core coretype:3;
words wordlen:2;
hyperthread mode:2;
};
void cpu::show(){
cout<<"Frequence: "<<frequence<<endl;
cout<<"Core: ";
switch((unsigned)coretype){
case single: cout<<"single-core";break;
case dual: cout<<"dual-core";break;
case quad: cout<<"quad-core";break;
}
cout<<endl;
cout<<"words: ";
switch((unsigned)wordlen){
case bit32: cout<<"32-bits len";break;
case bit64: cout<<"64-bits len";break;
}
cout<<endl;
cout<<"hyperthread: ";
switch((unsigned)mode){
case support: cout<<"support hyper-thread";break;
case notsupport: cout<<"not support hyper-thread";break;
}
cout<<endl;
};
int main(){
cpu c(3000000000, quad,bit64,support);
cout<<"size of class cpu:"<<sizeof(cpu)<<endl;
c.show();
return 0;
}