编译器:C++ (g++) 文具盒里有铅笔、尺和橡皮擦。在下面的程序里,定义了铅笔类 PENCIL、尺类 RULER、橡皮擦类 ERASER 和文具盒类 BOX。请完成这四个类的成员函数的设计。 #include <iostream> #include <string> using namespace std; class PENCIL { public: PENCIL(int model); ~PENCIL(); void Show() const; private: int model; }; class RULER { public: RULER(int length); ~RULER(); void Show() const; private: int length; }; class ERASER { public: ERASER(int color); ~ERASER(); void Show() const; private: int color; }; class BOX { public: BOX(int pencil, int ruler, int eraser); ~BOX(); void Show() const; private: PENCIL pencil; RULER ruler; ERASER eraser; }; /* 你提交的代码将被嵌在这里 */ int main() { int p, r, e; cin >> p >> r >> e; BOX x(p, r, e); x.Show(); return 0; } 输入样例 5 15 3 输出样例 创建铅笔(型号: 5) 创建尺(长度: 15) 创建橡皮擦(颜色: 3) 创建文具盒 铅笔(型号: 5) 尺(长度: 15) 橡皮擦(颜色: 3) 销毁文具盒 销毁橡皮擦(颜色: 3) 销毁尺(长度: 15) 销毁铅笔(型号: 5)请你写出这个问题 的代码
时间: 2024-01-29 20:02:28 浏览: 25
下面是铅笔类 PENCIL 的成员函数的设计:
```cpp
PENCIL::PENCIL(int model) : model(model) {
cout << "创建铅笔(型号: " << model << ")" << endl;
}
PENCIL::~PENCIL() {
cout << "销毁铅笔(型号: " << model << ")" << endl;
}
void PENCIL::Show() const {
cout << "铅笔(型号: " << model << ")" << endl;
}
```
下面是尺类 RULER 的成员函数的设计:
```cpp
RULER::RULER(int length) : length(length) {
cout << "创建尺(长度: " << length << ")" << endl;
}
RULER::~RULER() {
cout << "销毁尺(长度: " << length << ")" << endl;
}
void RULER::Show() const {
cout << "尺(长度: " << length << ")" << endl;
}
```
下面是橡皮擦类 ERASER 的成员函数的设计:
```cpp
ERASER::ERASER(int color) : color(color) {
cout << "创建橡皮擦(颜色: " << color << ")" << endl;
}
ERASER::~ERASER() {
cout << "销毁橡皮擦(颜色: " << color << ")" << endl;
}
void ERASER::Show() const {
cout << "橡皮擦(颜色: " << color << ")" << endl;
}
```
下面是文具盒类 BOX 的成员函数的设计:
```cpp
BOX::BOX(int pencil, int ruler, int eraser)
: pencil(pencil), ruler(ruler), eraser(eraser) {
cout << "创建文具盒" << endl;
}
BOX::~BOX() {
cout << "销毁文具盒" << endl;
}
void BOX::Show() const {
pencil.Show();
ruler.Show();
eraser.Show();
}
```
最后是完整的代码:
```cpp
#include <iostream>
#include <string>
using namespace std;
class PENCIL {
public:
PENCIL(int model);
~PENCIL();
void Show() const;
private:
int model;
};
class RULER {
public:
RULER(int length);
~RULER();
void Show() const;
private:
int length;
};
class ERASER {
public:
ERASER(int color);
~ERASER();
void Show() const;
private:
int color;
};
class BOX {
public:
BOX(int pencil, int ruler, int eraser);
~BOX();
void Show() const;
private:
PENCIL pencil;
RULER ruler;
ERASER eraser;
};
PENCIL::PENCIL(int model) : model(model) {
cout << "创建铅笔(型号: " << model << ")" << endl;
}
PENCIL::~PENCIL() {
cout << "销毁铅笔(型号: " << model << ")" << endl;
}
void PENCIL::Show() const {
cout << "铅笔(型号: " << model << ")" << endl;
}
RULER::RULER(int length) : length(length) {
cout << "创建尺(长度: " << length << ")" << endl;
}
RULER::~RULER() {
cout << "销毁尺(长度: " << length << ")" << endl;
}
void RULER::Show() const {
cout << "尺(长度: " << length << ")" << endl;
}
ERASER::ERASER(int color) : color(color) {
cout << "创建橡皮擦(颜色: " << color << ")" << endl;
}
ERASER::~ERASER() {
cout << "销毁橡皮擦(颜色: " << color << ")" << endl;
}
void ERASER::Show() const {
cout << "橡皮擦(颜色: " << color << ")" << endl;
}
BOX::BOX(int pencil, int ruler, int eraser)
: pencil(pencil), ruler(ruler), eraser(eraser) {
cout << "创建文具盒" << endl;
}
BOX::~BOX() {
cout << "销毁文具盒" << endl;
}
void BOX::Show() const {
pencil.Show();
ruler.Show();
eraser.Show();
}
int main() {
int p, r, e;
cin >> p >> r >> e;
BOX x(p, r, e);
x.Show();
return 0;
}
```