前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >C++定义一个简单的Computer类

C++定义一个简单的Computer类

作者头像
Enterprise_
发布2018-05-18 15:18:43
2.6K0
发布2018-05-18 15:18:43
举报
文章被收录于专栏:小L的魔法馆小L的魔法馆
代码语言:javascript
复制
/*定义一个简单的Computer类
有数据成员芯片(cpu)、内存(ram)、光驱(cdrom)等等,
有两个公有成员函数run、stop。cpu为CPU类的一个对象,
ram为RAM类的一个对象,cdrom为CDROM类的一个对象,
定义并实现这个类。
2018.4.3
*/
  • 代码如下
代码语言:javascript
复制
#include<iostream>
#include<string>
using namespace std;
class CPU{
public:
    CPU(int sta,string tp);
    CPU(const CPU &ad);
    ~CPU();
    void details();
private:
    int standard;
    string brand;
};
CPU::CPU(int sta,string tp){
    this->standard = sta;
    this->brand = tp;
}
CPU::CPU(const CPU &ad) {
    cout << endl << "Warnning:This Copy constructors.!!!" << endl;
    this->brand = ad.brand;
    this->standard = ad.standard;
}
CPU::~CPU(){

};
void CPU::details(){
    cout << "The details of CPU:" << endl;
    cout << "The brand is " << brand << endl;
    cout << "The standard is " << standard << endl << endl;
}
class RAM{
public:
    RAM(int mem,int bit, string tp);
    RAM(RAM &ad);
    ~RAM();
    void details();
private:
    int memory;
    int bits;
    string brand;
};
RAM::RAM(int mem, int bit, string tp){
    this->memory = mem;
    this->bits = bit;
    this->brand = tp;
}
RAM::RAM(RAM &ad){
    cout << endl << "Warnning:This Copy constructors.!!!" << endl;
    this->memory = ad.memory;
    this->bits =ad.bits;
    this->brand =ad.brand;
}
RAM::~RAM(){

}
void RAM::details(){
    cout << "The details of RAM:" << endl;
    cout << "The brand is " << brand << endl;
    cout << "The memory is " << memory<< endl;
    cout << "The bits are " << bits << endl << endl;
}
class CDROM
{
public:
    CDROM(int st, string bra);
    CDROM(CDROM &ad);
    ~CDROM();
    void details();
private:
    int standard;
    string brand;
};
CDROM::CDROM(int st, string bra){
    this->brand = bra;
    this->standard = st;
}
CDROM::CDROM(CDROM &ad) {
    cout << endl << "Warnning:This Copy constructors.!!!" << endl;
    this->brand = ad.brand;
    this->standard = ad.standard;
}
CDROM::~CDROM(){

}
void CDROM::details(){
    cout << "The details of CDROM:" << endl;
    cout << "The brand is " << brand << endl;
    cout << "The standard is " << standard << endl << endl;
}
class Computer {
public:
    Computer(CPU cp,RAM ra,CDROM cdro);
    Computer(Computer &ad);
    ~Computer();
    void stop();
    void run();
    void details();
private:
    CPU cpu;
    RAM ram;
    CDROM cdrom;
};
Computer::Computer(CPU cp, RAM ra, CDROM cdro):cpu(cp),ram(ra),cdrom(cdro){
    cout << "Computer is OK!" << endl;
}
Computer::Computer(Computer &ad): cpu(ad.cpu), ram(ad.ram), cdrom(ad.cdrom) {
    cout << endl << "Warnning:This Copy constructors.!!!" << endl;
    cout << "Computer is OK!" << endl;
}
Computer::~Computer() {

}
void Computer::run(){
    cout << "Computer is running!" << endl;
}
void Computer::stop(){
    cout << "Computer stoped!" << endl;
}
void Computer::details(){
    cpu.details();
    ram.details();
    cdrom.details();
}
int main(void){
    CPU cp(1,"!@!");
    RAM ra(1024,10,"!#@!$");
    CDROM cd(2561,"$#%$#^");
    Computer cs(cp, ra, cd);
    cs.run();
    cs.details();
    cs.stop();
    return 0;
}
  • 测试截图
这里写图片描述
这里写图片描述
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2018年04月06日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档