大家看看这个怎么实现
#include<iostream>#include<string>
using namespace std;
enum Cpu_rank{p1=1,p2,p3,p4,p5};
class cpu
{
public:
cpu()
{
rank=p1;
frequency=0.0;
voltage=0.0;
cpuid=0;
cout<<"默认构造函数被调用"<<endl;
cout<<"cpu"<<getcpuid()<<"被构造"<<endl;
}
cpu(enum Cpu_rank rk,double fq,double vl,int cid)
{
rank=rk;
frequency=fq;
voltage=vl;
cpuid=cid;
cout<<"构造函数被调用"<<endl;
cout<<"cpu"<<getcpuid()<<"被构造"<<endl;
}
void stop();
void run();
void control();
enum Cpu_rank getrank()
{
return rank;
}
double getfrequency()
{
return frequency;
}
double getvoltage()
{
return voltage;
}
int getcpuid()
{
return cpuid;
}
private:
double frequency;
enum Cpu_rank rank;
double voltage;
int cpuid;
};
void cpu::stop()
{
cout<<"cpu"<<getcpuid()<<" is stoped!"<<endl<<"enter 'run' to start it;enter 'out' to run another!"<<endl;
}
void cpu::run()
{
cout<<"cpu"<<getcpuid()<<" is running!"<<endl<<"enter 'stop' to stop it;enter 'out' to run another!"<<endl ;
}
void cpu::control()
{
char ans[5];
run();
while(1)
{
cin>>ans;
if(strcmp(ans,"stop")==0)
stop();
else if(strcmp(ans,"run")==0)
run();
else if(strcmp(ans,"out")==0)
break;
else cout<<"Illegal entering!"<<endl;
}
}
int main()
{
cpu cpu1;
cpu cpu2(p2,1.8,1.2,2);
for(int i=1;i<3;i++)
{
cout<<"Information about cpu"<<i<<endl;
cout<<"Rank="<<cpu1.getrank()<<" Frequency="<<cpu1.getfrequency()<<" Voltage="<<cpu1.getvoltage()<<endl;
}
cpu1.control();
cpu2.control();
return 0;
}
在main中本想用循环控制变量i来控制输出不同对象的信息(在变量i的控制下输出cpu1和cpu2的信息),可是不知道该怎么实现,麻烦哪位高手指点一下。这里先谢过。