适用于c++5.11版本 设计一个建筑物类Buliding,由他派生出教学楼类TeachBuliding和宿舍楼类DormBuliding
时间: 2024-03-14 20:45:43 浏览: 146
c++build 课程设计
4星 · 用户满意度95%
好的,下面是一个适用于C++11版本的实现代码:
```c++
class Building {
public:
Building(int building_id, int floors, double area) : building_id(building_id), floors(floors), area(area) {}
virtual void print() const {
std::cout << "Building ID: " << building_id << std::endl;
std::cout << "Floors: " << floors << std::endl;
std::cout << "Area: " << area << std::endl;
}
protected:
int building_id;
int floors;
double area;
};
class TeachBuilding : public Building {
public:
TeachBuilding(int building_id, int floors, double area, int classrooms) : Building(building_id, floors, area), classrooms(classrooms) {}
void print() const override {
Building::print();
std::cout << "Classrooms: " << classrooms << std::endl;
}
private:
int classrooms;
};
class DormBuilding : public Building {
public:
DormBuilding(int building_id, int floors, double area, int dorms, int capacity) : Building(building_id, floors, area), dorms(dorms), capacity(capacity) {}
void print() const override {
Building::print();
std::cout << "Dorms: " << dorms << std::endl;
std::cout << "Capacity: " << capacity << std::endl;
}
private:
int dorms;
int capacity;
};
```
这个实现与之前的实现类似,不同之处在于使用了C++11的override关键字来重写虚函数print,同时使用了std命名空间来输出信息。这样,我们就可以方便地对不同类型的建筑物进行管理和操作。
阅读全文