声明一个简单的函数Computer类,有数据成员芯片(cpu)、内存(ram)、光驱(cdrom)等,有两个公有成员函
enum CPU_rank{p1=1,p2,p3,p4,p5,p6,p7};
class CPU
{
private:
CPU_rank rank;
int frequency;
float voltage;
public:
CPU (CPU_rank r,int f,float v)
{
rank=r;
frequency=f;
voltage=v;
cout<<"构造了一个cpu!"<<endl;
}
~CPU()
{ cout<<"其构了一个cpu!"<<endl;}
CPU_rank getrank() const
{ return rank;}
void setrank(CPU_rank r)
{ rank=r;}
void run()
{cout<<"cpu开始运行!"<<endl;
}
void stop()
{cout<<"cpu停止运行!"<<endl;}
};
class RAM
{
private:
int ram;
public:
RAM(int r)
{ cout<<"构造了一个ram!"<<endl;}
RAM(){ ram=0;cout<<"构造了一个ram!"<<endl;}
~RAM(){cout<<"析构了一个ram!"<<endl;}
void run()
{cout<<"ram开始运行!"<<endl;}
void stop()
{cout<<"ram停止运行!"<<endl;}
};
class CDRAM
{
private:
int cdram;
public:
CDRAM(int c)
{ cdram=c;
cout<<"构造了一个cdram!"<<endl;}
CDRAM(){ cout<<"构造了一个cdram!"<<endl;}
~CDRAM(){cout<<"析构了一个cdram!"<<endl;}
void run()
{cout<<"cdram开始运行"<<endl;}
void stop()
{cout<<"cdram停止运行"<<endl;}
};
class computer
{private:
CPU cpu;RAM ram;CDRAM cdram;
public:
computer(CPU c,RAM r,CDRAM cd);
computer();
~computer(){cout<<"析构了一个computer"<<endl;}
computer(computer &p);
void run()
{cout<<"computer开始运行"<<endl;}
void stop()
{cout<<"computer停止运行"<<endl;}
};
computer::computer():cpu(p6,100,float(1.3)),ram(20),cdram(30)
{cout<<"构造了一个 cpu00"<<endl;}
computer::computer(CPU c,RAM r,CDRAM cd):cpu(c),ram(r),cdram(cd)
{cout<<"构造了一个computer"<<endl;}
computer::computer(computer &p): cpu(p.cpu),ram(p.ram),cdram(p.cdram)
{ cout<<"调用了复制构造函数"<<endl;}
void main()
{
CPU m(p6,300,float(2.8));
RAM n(1);
CDRAM q(2);
computer com1;
computer com2(m,n,q);
}
// class.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include<iostream>
using namespace std;
enum CPU_Rank{p1=1,p2,p3,p4,p5,p6,p7};
class CPU
{
private:
CPU_Rank rank;
int frequency;
float voltage;
public:
CPU(CPU_Rank r, int f, float v)
{
rank = r;
frequency = f;
voltage = v;
cout << "构造了一个CPU!" << endl;
}
~CPU(){ cout << "析构了一个CPU!" << endl; }
CPU_Rank GetRank() const { return rank; }// 外部接口函数
int GetFrequency() const{ return frequency; }
float GetVoltage() const{ return voltage; }
void SetRank(CPU_Rank r){ rank = r; }//
void SetFrequency(int f){ frequency = f; }
void SetVoltage(float v){ voltage = v; }
void Run(){ cout << "CPU开始运行!" << endl; }
void Stop(){ cout << "CPU停止运行!" << endl; }
};
enum RAM_Type{DDR2=2, DDR3, DDR4 };
class RAM
{
private:
int capacity;
RAM_Type type;
float Mfrequency;
public:
RAM(int c, RAM_Type t, float mf)
{
capacity = c;
type = t;
Mfrequency = mf;
cout << "构造了一个RAM!" << endl;
}
~RAM(){ cout << "析构了一个RAM!" << endl; }
RAM_Type GetType() const { return type; }
int GetCapacity() const{ return capacity; }
float GetMfrequency() const { return Mfrequency; }
void SetType(RAM_Type t) { type = t; }
void SetCapacity(int c){ capacity = c; }
void SetMfrequency(float mf){ Mfrequency = mf; }
void Run(){ cout << "RAM开始运行!" << endl; }
void Stop(){ cout << "RAM停止运行!" << endl; }
};
enum CDRAM_Interface{SATA,USB};
enum CDRAM_Install_type{external,built_in};
class CDRAM
{
private:
CDRAM_Interface interface;
int cache_size;
CDRAM_Install_type install_type;
public:
CDRAM(CDRAM_Interface i,int cs,CDRAM_Install_type ty)
{
interface = i;
cache_size = cs;
install_type = ty;
cout << "构造了一个CDRAM!" << endl;
}
~CDRAM(){ cout << "析构了CDRAM!" << endl; }
CDRAM_Interface GetInterface() const { return interface; }
int GetCache_size() const{ return cache_size; }
CDRAM_Install_type GetInstall_type() const { return install_type; }
void SetInterface(CDRAM_Interface i){ interface = i; }
void SetCache_size(int cs){ cache_size = cs; }
void SetInstall_type(CDRAM_Install_type ty){ install_type = ty; }
void Run(){ cout << "CDROM开始运行!" << endl; }
void Stop(){ cout << "CDROM停止运行!" << endl; }
};
class COMPUTER
{
private:
CPU my_cpu;
RAM my_ram;
CDRAM my_cdram;
unsigned int storage_size;
unsigned int bandwidth;
public:
COMPUTER::COMPUTER(CPU c, RAM r, CDRAM cdr, int sto, int ban) :my_cpu(c), my_ram(r), my_cdram(cdr)
{
storage_size = sto;
bandwidth = ban;
cout << "构造了一个COMPUTER!" << endl;
}
~COMPUTER(){ cout << "析构了COMPUTER!" << endl; }
void Run()
{
my_cpu.Run();
my_ram.Run();
my_cdram.Run();
cout << "COMPUTER开始运行!" << endl;
}
void Stop()
{
my_cpu.Stop();
my_ram.Stop();
my_cdram.Stop();
cout << "COMPUTER停止运行!" << endl;
}
};
/*COMPUTER::COMPUTER(CPU c, RAM r, CDRAM cdr, int sto, int ban) :my_cpu(c), my_ram(r), my_cdram(cdr)
{
//my_cpu = c;
//my_ram = r;
//my_cdram = cdr;
storage_size = sto;
bandwidth = ban;
cout << "构造了一个COMPUTER!" << endl;
}*/
int _tmain(int argc, _TCHAR* argv[])
{
CPU a(p6, 300, 220);//声明一个CPU对象,下面都是对这个对象的操作
a.Run();
a.Stop();
cout << "**************************" << endl;
RAM b(1600, DDR3, 220.34);
b.Run();
b.Stop();
cout << "**************************" << endl;
CDRAM c(USB, 400, external);
c.Run();
c.Stop();
cout << "**************************" << endl;
COMPUTER mycomputer(a, b, c, 300, 200);
mycomputer.Run();
mycomputer.Stop();
cout << "**************************" << endl;
return 0;
}
2011-03-30
using namespace std;
enum CPU_Rank {P1=1,P2,P3,P4,P5,P6,P7};
class CPU
{
private:
CPU_Rank rank;
int frequency;
float voltage;
public:
CPU (CPU_Rank r, int f, float v)
{
rank = r;
frequency = f;
voltage = v;
cout << "构造了一个CPU!" << endl;
}
CPU()
{
cout << "构造了一个CPU!" << endl;
};
~CPU ()
{
cout << "析构了一个CPU!" << endl;
}
CPU_Rank GetRank() const { return rank; }
int GetFrequency() const { return frequency; }
float GetVoltage() const { return voltage; }
void SetRank(CPU_Rank r) { rank = r; }
void SetFrequency(int f) { frequency = f; }
void SetVoltage(float v) { voltage = v; }
void Run() {cout << "CPU开始运行!" << endl; }
void Stop() {cout << "CPU停止运行!" << endl; }
};
class RAM
{
public:
RAM ()
{
cout << "构造了一个RAM!" << endl;
}
~RAM ()
{
cout << "析构了一个RAM!" << endl;
}
void Run() {cout << "RAM开始运行!" << endl; }
void Stop() {cout << "RAM停止运行!" << endl; }
};
class CDROM
{
public:
CDROM (){ cout << "构造了一个CDROM!" << endl; }
~CDROM ()
{
cout << "析构了一个CDROM!" << endl;
}
void Run() {cout << "CDROM开始运行!" << endl; }
void Stop() {cout << "CDROM停止运行!" << endl; }
};
class COMPUTER
{
private:
CPU cpu;
RAM ram;
CDROM cdrom;
public:
COMPUTER()
{
cout << "构造了一个COMPUTER!" << endl;
}
~COMPUTER ()
{
cout << "析构了一个COMPUTER!" << endl;
}
void Run()
{
cout << "COMPUTER开始运行!" << endl;
cpu.Run();
ram.Run();
}
void Stop()
{
ram.Stop();
cpu.Stop();
cout << "COMPUTER停止运行!" << endl;
}
};
int main()
{
COMPUTER a;
a.Run();
a.Stop();
}
2019-04-21
private:
Cpu cpu;
Ram ram;
Cdrom cdrom;
public:
void run()
{}
void stop()
{}
}