C++第四天

定义一个CPU类,包含等级(Rank)、频率(frequency)、电压(voltage)等属性。其中,rank为枚举类型CPU__Rank,定义为enum CPU_Rank{P1=1,P2,P3,P4,P5,P6,P7},frequency为单位是MHz的整型数,voltage为浮点型的电压值。

代码
#include<iostream>

using namespace std;

enum CPU_Rank{P1=1,P2,P3,P4,P5,P6,P7};
class CPU
{
public:
CPU()
{
Rank=1;
frequency=2;
voltage=100;
cout<<"create a CPU!"<<endl;
}
CPU(CPU_Rank rank,int f,double v)
{
Rank=rank;
frequency=f;
voltage=v;
cout<<"create a CPU!"<<endl;
}
CPU(CPU &c)
{
Rank=c.Rank;
frequency=c.frequency;
voltage=c.voltage;
cout<<"copy create a CPU!"<<endl;
}
void showinfo()
{
cout<<"rank:"<<Rank<<endl;
cout<<"frequency:"<<frequency<<endl;
cout<<"voltage:"<<voltage<<endl;
}
~CPU()
{
cout<<"destruct a CPU!"<<endl;
}
private:
int Rank;
int frequency;
double voltage;
};


int main()
{
CPU a(P6,3,300);

cout<<"cpu a's parameter"<<endl;
a.showinfo(); //显示性能参数

CPU b;
cout<<"cpu b's parameter"<<endl;
b.showinfo(); //显示性能参数

CPU c(a);
cout<<"cpu c's parameter"<<endl;
c.showinfo(); //显示性能参数
}

输出结果:

create a CPU!
cpu a's parameter
rank:6
frequency:3
voltage:300
create a CPU!
cpu b's parameter
rank:1
frequency:2
voltage:100
copy create a CPU!
cpu c's parameter
rank:6
frequency:3
voltage:300
destruct a CPU!
destruct a CPU!
destruct a CPU!

 

posted @ 2023-04-14 19:29  涨涨涨张  阅读(22)  评论(0编辑  收藏  举报