OJ 2072 Problem B 建造一间教室

Description

一间教室包括很多物品。这里只考虑灯(Light)和椅子(Chair)。定义Light类,只有一个int类型的参数,表示灯的瓦数;定义Chair类,只有一个字符串类型的参数,表示椅子的颜色。定义ClassRoom类,包括四个属性:两个int类型的属性,分别为灯的个数和椅子的个数。一个Light类的对象和一个Chair类的对象,分别为教室中灯的种类和椅子的种类。

Input

输入有6行,第1行是一个正整数,表示灯的瓦数。第2行是一个不含空白符的字符串,表示椅子的颜色。第3、4行表示教室中灯的个数和椅子的数量。第5行是一个正整数,表示教室中灯的瓦数,第6行是一个不含空白符的字符串,表示教室中椅子的颜色。

Output

见样例。

Sample Input

20
blue
16
100
25
red

Sample Output

A 20w light is created.
A blue chair is created.
A 25w light is created.
A red chair is created.
A classroom having 16 lights and 100 chairs is created.
A classroom having 16 lights and 100 chairs is erased.
A red chair is created.
A 25w light is erased.
A blue chair is created.
A 20w light is erased.

HINT

鬼知道为什么析构函数里面是created

Append Code

int main()
{
    int nl, nc;
    int w;
    string color;
    cin>>w>>color;
    Light light(w);
    Chair chair(color);
    cin>>nl>>nc;
    cin>>w>>color;
    ClassRoom room(nl, nc, w, color);
    return 0;
}

Accepted Code

#include <iostream>
#include <string>
using namespace std;

class Light {
protected:
    int W;
public:
    Light(int w) : W(w) {
        cout << "A " << W << "w light is created.\n";
    }
    ~Light() {
        cout << "A " << W << "w light is erased.\n";
    }
};

class Chair {
protected:
    string str;
public:
    Chair(string s) : str(s) {
        cout << "A " << str << " chair is created.\n";
    }
    ~Chair() {
        cout << "A " << str << " chair is created.\n";
    }
};

class ClassRoom  {
private:
    int num_L;
    int num_C;
    Light light;
    Chair chair;
public:
    ClassRoom(int nl, int nc, int w, string color) : light(w), chair(color), num_L(nl), num_C(nc) {
        cout << "A classroom having " << num_L << " lights and " << num_C << " chairs is created.\n";
    }
    ~ClassRoom() {
        cout << "A classroom having " << num_L << " lights and " << num_C << " chairs is erased.\n";
    }
};
int main()
{
    int nl, nc;
    int w;
    string color;
    cin>>w>>color;
    Light light(w);
    Chair chair(color);
    cin>>nl>>nc;
    cin>>w>>color;
    ClassRoom room(nl, nc, w, color);
    return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值