//2. 定义一个简单的Computer类,有数据成员芯片(cpu)、内存(ram)、光驱(cdrom)等等,
//有两个公有成员函数run、stop。cpu为CPU类的一个对象,ram为RAM类的一个对象,
//cdrom为CDROM类的一个对象,定义并实现这个类。
#include<iostream>
using namespace std;
enum CPU_Rank{P1=1,P2,P3,P4,P5,P6,P7};
class CPU
{
public:
CPU(CPU_Rank x,int y,float z)
{
rank=x;
frequency=y;
voltage=z;
cout<<"cpu create"<<endl;
}
CPU(CPU &c);
~CPU(){cout<<"cpu reset"<<endl;}
private:
CPU_Rank rank;
int frequency;
float voltage;
};
CPU::CPU(CPU &c)
{
rank=c.rank;
frequency=c.frequency;
voltage=c.voltage;
cout<<"cpu copy"<<endl;
}
class RAM
{
public:
RAM(int x=1)
{
ram=x;
cout<<"ram create"<<endl;
}
RAM(RAM &r);
~RAM(){cout<<"RAM reset"<<endl;}
private:
int ram;
};
RAM::RAM(RAM &r)
{
ram=r.ram;
cout<<"ram copy"<<endl;
}
class CDROM
{
public:
CDROM(int m)
{
cdrom=m;
cout<<"CD create "<<endl;
}
CDROM(CDROM &d);
~CDROM (){cout<<"CD reset"<<endl;}
private:
int cdrom;
};
CDROM::CDROM(CDROM &d)
{
cdrom=d.cdrom;
cout<<"cdrom copy"<<endl;o
}
class Computer
{
public:
Computer(CPU x,RAM y,CDROM z);
Computer(Computer &p);
~Computer(){cout<<"computer reset"<<endl;}
private:
CPU cpu;
RAM ram;
CDROM cdrom;
};
Computer::Computer(CPU x,RAM y,CDROM z):cpu(x),ram(y),cdrom(z)
{
cout<<"Computer create1"<<endl;
}
Computer::Computer(Computer &p):cpu(p.cpu),ram(p.ram),cdrom(p.cdrom)
{
cout<<"computer copy"<<endl;
}
int main (void)
{
CPU a(P1,2,3);
RAM b(3);
CDROM c(4);
Computer d(a,b,c);
Computer e(d);
return 0;
}
//书上讲的 调用内嵌对象的构造函数,调用顺序和定义的时候一样, 可是结果相反。
大家帮忙看看 我猜想是因为栈空间的影响 求大牛