定义一个CPU类,包含等级(rank)、频率(frequency)、电压(voltage)等属性,有两个共有成员函数run、stop。其中,rank为枚举类型定义为enum CPU_Ranl{P1=1,P2,P3,P4,P4,P5,P6,P7},frequency为单位是MHz的整数,voltage为浮点型的电压值。观察构造函数和析构函数的调用顺序。

#include<iostream>

using namespace std;

enum CPU_Rank {P1=1,P2=2,P3=3,P4=4,P5=5,P6=6,P7=7};

class CPU

{

private: 

    CPU_Rank rank;

 int frequency;

 float voltage;

public:

 CPU(int newrank,int newfrequency,float newvoltage);

 void run();

 void stop();

 ~CPU(){cout<<"成功调用析构函数"<<endl;}

};

void CPU::run()

{

 cout<<"程序开始执行"<<endl;

}

void CPU::stop()

{

 cout<<"程序结束"<<endl;

}

CPU::CPU(int newrank,int newfrequency,float newvoltage)

{ 

 rank=(CPU_Rank)newrank;

 frequency=newfrequency;

 voltage=newvoltage;

    cout<<"成功调用构造函数"<<endl;

 cout<<"等级:"<<P1<<endl;

 cout<<"频率:"<<frequency<<endl;

 cout<<"电压:"<<voltage<<endl;

} 

int main()

{

 CPU cpu(2,60,220);

 cpu.run();

 cpu.stop();

 return 0;

}
Logo

欢迎大家加入成都城市开发者社区,“和我在成都的街头走一走”,让我们一起携手,汇聚IT技术潮流,共建社区文明生态!

更多推荐