📄 computer.cpp
字号:
/*
实现一个计算机的Class的层次结构
类CPU
Method :设定厂商名称
:取得厂商名称
:取得价格
:设定主频
:取得价格实现方法:根据主频的范围和厂商确定(自己自由发挥)
类Memory
Method :设定厂商名称
:取得厂商名称
:取得价格(自己自由发挥)
:设定大小
:取得价格实现方法:根据内存大小的范围和厂商确定
类MainBoard
Method :设定厂商名称
:取得厂商名称
:取得价格(自己自由发挥)
:取得价格实现方法:根据厂商确定
:Plug(CCPU*,CMemory* ) (接受由Computer类得到的CCPU和CMemory)
:SelfCheck() (检查是否Plug过正确的CPU, Memory)
类Monitor
Method :设定厂商名称
:取得厂商名称
:取得价格
:设定大小
:设定类型:一般、液晶
:取得价格实现方法:根据大小,是否液晶和厂商确定(自己自由发挥)
Computer类:包含上面几个类的对象。
Method :设定CPU主频
:设定CPU厂商(A,B,C)
:设定Memory大小(128,256,512)
:设定Memory厂商(A,B,C)
:设定显示器大小(14,15,17,19),类型(一般,液晶)
:设定显示器厂商(A,B,C);
:设定主版的厂商(A,B,C);
:察看整机价格(打印到屏幕)
:察看配置(打印到屏幕)
:Init() (调用MainBoard.Plug())
:Start() (调用MainBoard.SelfCheck() )
写一个小程序,动态创建一个10个Computer的数组,设定察看每一台的配置,计算总价格等。
要求正确的释放对象的数组。
*/
#include <iostream>
#include <string>
using namespace std;
//类CPU
class cpu
{
public:
cpu();
virtual ~cpu();
float getvalue();
float getchangshang();
friend class computer;
private:
float changshang;
float value;
float zhupin;
float setzhupin();
float setchangshang();
};
//类Memory
class memory
{
public:
memory();
virtual ~memory();
float getvalue();
float getchangshang();
private:
float changshang;
float value;
float guige;
float setguige();
float setchangshang();
friend class computer;
};
//类MainBoard
class mainboard
{
public:
mainboard();
mainboard(cpu *ccpu,memory *cmemory);
virtual ~mainboard();
void selfCheck();
float getvalue();
friend class computer;
private:
float changshang;
float value;
int flag;
int plus(cpu *ccpu,memory *cmemory);
float setchangshang();
};
//类Monitor
class monitor
{
public:
monitor();
virtual ~monitor();
float getvalue();
char * getchangshang();
friend class computer;
private:
char changshang[10];
float value;
float guige;
float leixing;
float setleixing();
float setguige();
float setchangshang();
};
//Computer类
class computer
{
public:
computer();
virtual ~computer();
float getvalue();
void peizhi();
private:
cpu c1;
memory m1;
mainboard mb1;
monitor mo1;
float value;
};
cpu::cpu()
{
value=setchangshang()*100+setzhupin()*150;
}
cpu::~cpu()
{
}
float cpu::setzhupin()
{
float x;
cout<<"请选择CPU的主频"<<endl;
cout<<"1.400HZ"<<endl;
cout<<"2.500HZ"<<endl;
cout<<"3.600HZ"<<endl;
cin>>x;
if(x==1)
zhupin=400;
if(x==2)
zhupin=500;
if(x==3)
zhupin=600;
return x;
}
float cpu::setchangshang()
{
float x;
cout<<"请选择CPU的厂商"<<endl;
cout<<"1.英特尔"<<endl;
cout<<"2.AMD"<<endl;
cout<<"3.其它"<<endl;
cin>>x;
changshang=x;
return x;
}
float cpu::getvalue()
{
return value;
}
float cpu::getchangshang()
{
return changshang;
}
memory::memory()
{
value=setchangshang()*50+setguige()*60;
}
memory::~memory()
{
}
float memory::getvalue()
{
return value;
}
float memory::setchangshang()
{
float x;
cout<<"请选择内存的厂商"<<endl;
cout<<"1.现代"<<endl;
cout<<"2.金士顿"<<endl;
cout<<"3.威刚"<<endl;
cin>>x;
changshang=x;
return x;
}
float memory::setguige()
{
float x;
cout<<"请选择内存的大小"<<endl;
cout<<"1.256MB"<<endl;
cout<<"2.512MB"<<endl;
cout<<"3.1024MB"<<endl;
cin>>x;
if(x==1)
guige=256;
if(x==2)
guige=512;
if(x==3)
guige=1024;
return x;
}
float memory::getchangshang()
{
return changshang;
}
mainboard::mainboard()
{
flag=0;
value=setchangshang()*150;
}
mainboard::mainboard(cpu *ccpu,memory *cmemory)
{
flag=0;
value=setchangshang()*150;
plus(ccpu,cmemory);
}
mainboard::~mainboard()
{
}
float mainboard::setchangshang()
{
float x;
cout<<"请选择主板的厂商"<<endl;
cout<<"1.英特尔"<<endl;
cout<<"2.华硕"<<endl;
cout<<"3.微星"<<endl;
cin>>x;
changshang=x;
return x;
}
int mainboard::plus(cpu *ccpu,memory *cmemory)
{
if(ccpu->getchangshang()!=changshang)
{
flag=0;
return 0;
}
flag=1;
return 1;
}
void mainboard::selfCheck()
{
if(flag==1)
cout<<"主板与CPU和内存都匹配"<<endl;
if(flag==0)
cout<<"主板与CPU或内存不匹配或者尚未搭配CPU和内存"<<endl;
}
float mainboard::getvalue()
{
return value;
}
//monitor
monitor::monitor()
{
value=setchangshang()*200+setguige()*300+setleixing()*500;
}
monitor::~monitor()
{
}
float monitor::getvalue()
{
//cout<<jiage<<endl;
return value;
}
float monitor::setchangshang()
{
float x;
cout<<"请选择显示器的厂商"<<endl;
cout<<"1.三星"<<endl;
cout<<"2.VOC"<<endl;
cout<<"3.LG"<<endl;
cin>>x;
if(x==1)
strcpy(changshang,"三星");
if(x==2)
strcpy(changshang,"VOC");
if(x==3)
strcpy(changshang,"LG");
return x;
}
float monitor::setguige()
{
float x;
cout<<"请选择显示器的规格"<<endl;
cout<<"1.17寸"<<endl;
cout<<"2.19寸"<<endl;
cout<<"3.21寸"<<endl;
cin>>x;
if(x==1)
guige=17;
if(x==2)
guige=19;
if(x==3)
guige=21;
return x;
}
char * monitor::getchangshang()
{
return changshang;
}
float monitor::setleixing()
{
float x;
cout<<"请选择显示器型号"<<endl;
cout<<"1.CRT"<<endl;
cout<<"2.LRC"<<endl;
cin>>x;
if(x==1)
leixing=1;
if(x==2)
leixing=2;
return x;
}
computer::computer():mb1(&c1,&m1)
{
mb1.selfCheck();
value=c1.getvalue()+m1.getvalue()+mb1.getvalue()+mo1.getvalue();
}
computer::~computer()
{
}
float computer::getvalue()
{
return value;
}
void computer::peizhi()
{
cout<<"CPU主频为"<<c1.zhupin<<"HZ"<<endl;
cout<<"内存为"<<m1.guige<<"MB"<<endl;
cout<<"显示器为"<<mo1.guige<<"寸"<<endl;
}
void main()
{
int a,i;
computer *p;
cout<<"请输入您所需要计算机台数"<<endl;
cin>>a;
p=new computer[a];
for(i=0;i<a;i++)
cout<<"第"<<i+1<<"台计算机价格是"<<p[i].getvalue()<<endl;
cout<<"请输入您查看某台计算机的配置"<<endl;
cin>>a;
p[a-1].peizhi();
delete []p;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -