建立一个基类Building,用来存储一座楼房的层数、房间数以及它的总平方英尺数。建立派生类Housing,继承Building,并存储下面的内容:卧室和浴室的数量,另外,建立派生类Office,继承Building,并存储灭火器和电话的数目。然后,编制应用程序,建立住宅楼对象和办公楼对象,并输出它们的有关数据。
时间: 2024-03-11 21:51:33 浏览: 33
好的,我会为你解答这个问题。以下是实现代码:
```
#include <iostream>
using namespace std;
class Building {
protected:
int numFloors; // 层数
int numRooms; // 房间数
int totalSqft; // 总面积
public:
Building(int f, int r, int s) {
numFloors = f;
numRooms = r;
totalSqft = s;
}
void display() {
cout << "Number of floors: " << numFloors << endl;
cout << "Number of rooms: " << numRooms << endl;
cout << "Total square footage: " << totalSqft << endl;
}
};
class Housing : public Building {
private:
int numBedrooms; // 卧室数量
int numBathrooms; // 浴室数量
public:
Housing(int f, int r, int s, int b, int ba) : Building(f, r, s) {
numBedrooms = b;
numBathrooms = ba;
}
void display() {
Building::display();
cout << "Number of bedrooms: " << numBedrooms << endl;
cout << "Number of bathrooms: " << numBathrooms << endl;
}
};
class Office : public Building {
private:
int numFireExtinguishers; // 灭火器数量
int numPhones; // 电话数量
public:
Office(int f, int r, int s, int fe, int p) : Building(f, r, s) {
numFireExtinguishers = fe;
numPhones = p;
}
void display() {
Building::display();
cout << "Number of fire extinguishers: " << numFireExtinguishers << endl;
cout << "Number of phones: " << numPhones << endl;
}
};
int main() {
Housing house(2, 5, 2000, 3, 2);
Office office(3, 10, 5000, 10, 20);
cout << "Housing data: " << endl;
house.display();
cout << endl;
cout << "Office data: " << endl;
office.display();
return 0;
}
```
这个程序首先定义了一个名为`Building`的基类,其中包含三个成员变量:层数、房间数以及总面积。然后,它创建了两个派生类:`Housing`和`Office`。`Housing`类包含卧室和浴室的数量,`Office`类包含灭火器和电话的数量。这两个派生类都通过`public`继承了基类`Building`。
在`main`函数中,程序创建了一个`Housing`对象和一个`Office`对象,并分别调用它们的`display`函数来输出它们的数据。由于`Housing`和`Office`类都重载了`display`函数,因此它们会输出自己的数据,同时也会调用基类的`display`函数输出继承自基类的数据。
相关推荐
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)