定义一个房间类(CRoom),要求拥有的私有数据成员:门牌号(m_id),门 (m_pdoor),窗户(m_window)。其中,门是门类(CDoor)定义的对象指针成员, 窗户是窗户类(CWindow)定义的对象成员。要求:每个类单独定义在两个文件中(.h 和.cpp),房间类包括:构造函数、析构函数,InstallDoor 成员函数能够实现安装门(new CDoor)的功能。要求窗户类的构造函数必须带参数,CDoor 类具有宽度,高度及修改 宽度高度功能,CWindow 类具有宽度,高度及修改宽度高度功能
时间: 2023-07-10 19:09:51 浏览: 118
C语言课程设计用于医院门诊候诊的队列管理程序.pdf
5星 · 资源好评率100%
CRoom.h 文件:
```c++
#ifndef CROOM_H
#define CROOM_H
#include "CDoor.h"
#include "CWindow.h"
class CRoom {
public:
CRoom(int id);
~CRoom();
void InstallDoor(double width, double height);
private:
int m_id;
CDoor* m_pdoor;
CWindow m_window;
};
#endif // CROOM_H
```
CRoom.cpp 文件:
```c++
#include "CRoom.h"
CRoom::CRoom(int id) : m_id(id), m_pdoor(nullptr), m_window(1.0, 1.0) {}
CRoom::~CRoom() {
if (m_pdoor) {
delete m_pdoor;
}
}
void CRoom::InstallDoor(double width, double height) {
if (m_pdoor) {
delete m_pdoor;
}
m_pdoor = new CDoor(width, height);
}
```
CDoor.h 文件:
```c++
#ifndef CDOOR_H
#define CDOOR_H
class CDoor {
public:
CDoor(double width, double height);
~CDoor();
void SetWidth(double width);
void SetHeight(double height);
private:
double m_width;
double m_height;
};
#endif // CDOOR_H
```
CDoor.cpp 文件:
```c++
#include "CDoor.h"
CDoor::CDoor(double width, double height) : m_width(width), m_height(height) {}
CDoor::~CDoor() {}
void CDoor::SetWidth(double width) {
m_width = width;
}
void CDoor::SetHeight(double height) {
m_height = height;
}
```
CWindow.h 文件:
```c++
#ifndef CWINDOW_H
#define CWINDOW_H
class CWindow {
public:
CWindow(double width, double height);
void SetWidth(double width);
void SetHeight(double height);
private:
double m_width;
double m_height;
};
#endif // CWINDOW_H
```
CWindow.cpp 文件:
```c++
#include "CWindow.h"
CWindow::CWindow(double width, double height) : m_width(width), m_height(height) {}
void CWindow::SetWidth(double width) {
m_width = width;
}
void CWindow::SetHeight(double height) {
m_height = height;
}
```
阅读全文