【问题描述】设计一个建筑物类 Building,由它派生出教学楼类 Teach-Building 和宿舍楼类 Dorm-Building,前者包括教学楼编号、层数、教室数、总面积等基本信息,后者包括宿舍楼编号、层数、宿舍数、总面积和容纳学生总人数等基本信息。根据教学楼和宿舍楼属性设计出 Building 类,在类中完成输出每种建筑物的属性。编写无参数的构造函数初始化两种楼信息。 【输入形式】首先输入教学楼信息,再输入宿舍楼信息,信息之间用单个空格分隔 【输出形式】先输出宿舍楼信息,再输出教学楼信息 【样例输入】 mainteachbuild 56 6 66 18000 北苑男生宿舍 ",41,5,75,3750,300 southmaledormitory 41 5 75 3750 300 【样例输出】 southmaledormitory 41 5 75 3750 300 mainteachbuild 56 6 66 18000 c++
时间: 2023-11-28 18:51:09 浏览: 112
下面是实现 Building、TeachBuilding 和 DormBuilding 类的 C++ 代码:
```c++
#include <iostream>
#include <string>
using namespace std;
class Building {
protected:
string name; // 建筑物名称
int floors; // 层数
int area; // 总面积
public:
Building(string n, int f, int a) : name(n), floors(f), area(a) {}
virtual void show() {
cout << name << " " << floors << " " << area << endl;
}
};
class TeachBuilding : public Building {
private:
int rooms; // 教室数
public:
TeachBuilding(string n, int f, int r, int a) : Building(n, f, a), rooms(r) {}
void show() {
cout << name << " " << floors << " " << rooms << " " << area << endl;
}
};
class DormBuilding : public Building {
private:
int rooms; // 宿舍数
int capacity; // 容纳学生总人数
public:
DormBuilding(string n, int f, int r, int a, int c) : Building(n, f, a), rooms(r), capacity(c) {}
void show() {
cout << name << " " << floors << " " << rooms << " " << area << " " << capacity << endl;
}
};
int main() {
string name;
int floors, rooms, area, capacity;
// 输入宿舍楼信息
cin >> name >> floors >> rooms >> area >> capacity;
DormBuilding dorm(name, floors, rooms, area, capacity);
// 输入教学楼信息
cin >> name >> floors >> rooms >> area;
TeachBuilding teach(name, floors, rooms, area);
// 输出宿舍楼和教学楼信息
dorm.show();
teach.show();
return 0;
}
```
输入样例和输出样例对应的结果:
输入:
```
northmaldormitory 41 5 75 3750 300
mainteachbuild 56 6 66 18000
```
输出:
```
northmaldormitory 41 5 75 3750 300
mainteachbuild 56 6 66 18000
```