标题:大家看看这个怎么实现
只看楼主
yc0708
Rank: 1
等 级:新手上路
帖 子:4
专家分:0
注 册:2008-10-27
 问题点数:0 回复次数:1 
大家看看这个怎么实现
#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的信息),可是不知道该怎么实现,麻烦哪位高手指点一下。这里先谢过。
搜索更多相关主题的帖子: frequency include public double 
2008-10-27 13:27
braveboy2008
Rank: 1
等 级:新手上路
威 望:1
帖 子:21
专家分:0
注 册:2008-10-14
得分:0 
你可以将对象放在vector<cpu>中然后再输出
2008-10-27 23:48



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-240845-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.877795 second(s), 7 queries.
Copyright©2004-2025, BCCN.NET, All Rights Reserved