定义一个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(float a,int b,int c) { voltage=a; frequency=b; rank=(enum CPU_Rank) c; } void run(); void stop(); Cpu(Cpu &q); ~Cpu(){} private: enum CPU_Rank rank; float voltage; int frequency; }; Cpu::Cpu(Cpu &q){ rank=q.rank; frequency=q.frequency; voltage=q.voltage; cout<<"复制函数运行成功"<<endl; } void Cpu::run(){ cout<<"cpu 等级:"<<rank<<endl; cout<<"cpu 频率:"<<frequency<<"Mhz"<<endl; cout<<"cpu 电压:"<<voltage<<"V"<<endl; } void Cpu::stop(){ } void main() { Cpu core_i1(220,3000,5); core_i1.run(); Cpu core_i2(core_i1); }