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

/*定义一个简单的Computer类
有数据成员芯片(cpu)、内存(ram)、光驱(cdrom)等等,
有两个公有成员函数run、stop。cpu为CPU类的一个对象,
ram为RAM类的一个对象,cdrom为CDROM类的一个对象,
定义并实现这个类。
2018.4.3
*/
  • 代码如下
#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;
}
  • 测试截图

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

发表于

我来说两句

0 条评论
登录 后参与评论

相关文章

来自专栏CreateAMind

WGAN 代码测试-人脸数据集和mnist

对 keras : tensorflow https://github.com/zdx3578/DeepLearningImplementations/tree...

1493
来自专栏weixuqin 的专栏

深度学习之 TensorFlow(三):TensorFlow 源代码解析

4266
来自专栏debugeeker的专栏

《coredump问题原理探究》windows版9.2节dll hell导致的虚函数偏移

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/xuzhina/article/detai...

701
来自专栏陈仁松博客

在Windows下安装TensorFlow

目前TensorFlow在Windows下只支持Python 3.x。 1.下载Python 首先安装Pythone 3.6.4 下载地址 升级pip pyth...

4304
来自专栏GAN&CV

深度学习优化器算法详解:梯度更新规则+缺点+如何选择

转载自:https://mp.weixin.qq.com/s/RZc1PUgZs1BqRPeyBmTELA

3112
来自专栏人工智能LeadAI

译文 | 与TensorFlow的第一次接触 第六章:并发

第一版TensorFlow第一版发布于2015年11月,它可以运行在多台服务器的GPU上,同时并在其上面进行训练。2016年2月,更新版中增加了分布式与并发处理...

4197
来自专栏专知

基于网页的图像标注工具——imglab

【导读】imglab是一种基于Web的工具,用于标记可用于训练dlib或其他对象检测器的对象的图像。

9163
来自专栏debugeeker的专栏

《coredump问题原理探究》windows版5.2节数组

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/xuzhina/article/detai...

1013
来自专栏debugeeker的专栏

《coredump问题原理探究》windows版8.5节堆布局空闲堆块遍历

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/xuzhina/article/detai...

571
来自专栏debugeeker的专栏

《coredump问题原理探究》windows版6.3节虚函数

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/xuzhina/article/detai...

831

扫码关注云+社区

领取腾讯云代金券