#include<iostream>
using namespace std;
class CPU {
private:
int rank;
int frequency;
float voltage;
public:
void run() { cout << "CPU开始运行" << endl; }
void stop() { cout << "CPU停止运行" << endl; }
~CPU() { cout << "CPU析构函数被调用" << endl; }
CPU(int r, int f, float v)
{
this->rank = r;
this->frequency = f;
this->voltage = v;
}
};
class RAM {
private:
int size_ram;
public:
void run() { cout << "RAM开始运行" << endl; }
void stop() { cout << "RAM停止运行" << endl; }
~RAM() { cout << "RAM析构函数被调用" << endl; }
RAM(int s)
{
this->size_ram = s;
}
};
class CDROM {
private:
int size_cdrom;
public:
void run() { cout << "CDROM开始运行" << endl; }
void stop() { cout << "CDROM停止运行" << endl; }
~CDROM() { cout << "CDROM析构函数被调用" << endl; }
CDROM(int s)
{
this->size_cdrom = s;
}
};
class Computer {
private:
CPU cpu;
CDROM cdrom;
RAM ram;
public:
Computer(CPU c, CDROM cd, RAM r):cpu(c),cdrom(cd),ram(r)
{
cout << "Computer构造函数被调用" << endl;
}
~Computer() { cout << "Computer析构函数被调用" << endl; }
void run() {
cout << "Computer开始运行" << endl;
cpu.run();
cdrom.run();
ram.run();
}
void stop() {
cpu.stop();
cdrom.stop();
ram.stop();
cout << "Computer停止运行" << endl;
}
};
int main()
{
CPU cp(2, 100, 200);
CDROM cdr(128);
RAM r(256);
Computer c(cp, cdr, r);
c.run();
c.stop();
return 0;
}
小记一则(3)
于 2023-04-18 17:32:57 首次发布