c++实验一1.1
.定义一个CPU类,包含等级(rank)、频率(frequency)、电压(voltage)等属性,有两个公有成员函数run、stop。其中,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(CPU_Rank newrank=P1,int newfrequency=0,float newvoltage=0.0) :rank(newrank),frequency(newfrequency),voltage(newvoltage) { cout<<"成功调用构造函数"<<endl; cout<<"等级:"<<rank<<endl; cout<<"频率:"<<frequency<<endl; cout<<"电压:" <<voltage<<endl; } void run(); void stop(); ~CPU(){ cout<<"成功调用析构函数"<<endl; } private: CPU_Rank rank; int frequency; float voltage; }; inline void CPU::run() { cout<<"cpu运行"<<endl; } inline void CPU::stop() { cout<<"cpu停止"<<endl; } int main() { CPU cpu(P3,60,220); cpu.run(); cpu.stop(); return 0; }