读书人

跪求大神指点。小女软件工程大一生

发布时间: 2012-09-05 15:19:34 作者: rapoo

跪求大神指导。。小女软件工程大一生
声明一个CPU类,描述CPU的等级,频率,电压等属性,并且有运行和停止运行等功能。 其中,CPU的等级分为P1,P2,P3,P4,P5,P6,P7等几个等级。编写完整程序测试CPU类。

#include <iostream>
using namespace std;

class CPU
{
public:
enum {P1,P2,P3,P4,P5,P6,P7};
CPU(int level,float fre,float voltage,int state); 构造
~CPU();析构
void run();
void stop();
void print();
private:
int level;
float fre;
float voltage;
int state;
};

int main()
{
CPU c(CPU::P3,1.8,20,0),c2(CPU::P3,1.8,20,0);
c.run();
c.print();
c.stop();
c.print();

}

CPU::CPU(int level,float fre,float voltage,int state)
{
this->level=level;
this->fre=fre;
this->voltage=voltage;
this->state=state;
}

void CPU::run()
{
state=1;
cout<<"CPU is running...."<<endl;
}

void CPU::stop()
{
state=0;
cout<<"CPU has been stopped"<<endl;
}
int CPU:getState()
{
Return state;
}

Void CPU:setState(int state)
{
This->state=state;
}

void CPU::print()
{
cout<<"The P"<<level+1<<" CPU("<<fre<<"GHz,"<<voltage<<"V)";
if(state==0)
cout<<" has been stopped"<<endl;
else
cout<<" is running"<<endl;
}




老师出了这个题目。。到底是要我们做些什么事情。。我从主函数后面开始看不懂。。跪求大神解救啊

[解决办法]
这个题目两层含义:
1、写一个叫CPU的类。描述CPU的等级,频率,电压等“属性”,并且有运行和停止运行等功能(方法/函数)。
ps:你应该知道神马是类哈。

2、写一些函数,测试你写的类是否正确。参数打印呀,神马的。然后用main函数实例化你写的类,调用测试函数。
ps:这个有点像java的class即写即测,吼吼,扯远了。
[解决办法]

C/C++ code
#include <iostream>using namespace std;class CPU{public:    enum {P1,P2,P3,P4,P5,P6,P7};        CPU(int level,float fre,float voltage,int state); //构造        ~CPU();//析构        void run();    void stop();    void print();private:    int level;    float fre;    float voltage;    int state;};int main(){    CPU c(CPU::P3,1.8,20,0),c2(CPU::P3,1.8,20,0);//声明CPU类的对象,调用构造函数    c.run();    c.print();    c.stop();    c.print();}CPU::CPU(int level,float fre,float voltage,int state) //构造函数,赋值给CPU属性{    this->level=level;    this->fre=fre;    this->voltage=voltage;    this->state=state;}void CPU::run()    //CPU开始工作,stste设为1{    state=1;    cout<<"CPU is running...."<<endl;}void CPU::stop()    //CPU停止工作,state设为0{    state=0;    cout<<"CPU has been stopped"<<endl;}int CPU:getState()    //返回CPU当前状态{    Return state;}Void CPU:setState(int state)    //设定CPU的工作状态{    This->state=state;}void CPU::print()    //输出CPU的各种属性,以及当前的工作状态{    cout<<"The P"<<level+1<<" CPU("<<fre<<"GHz,"<<voltage<<"V)";    if(state==0)        cout<<" has been stopped"<<endl;    else        cout<<" is running"<<endl;} 

读书人网 >C++

热点推荐