用c++设计两个酒店管理员客房管理的类:一个是Person类,要求有储存房号,客户姓名和身份证号的信息;另一个类是Client类,要求新增客户的订房、退房和消费金额等信息。并给出相关测试算法
时间: 2023-09-25 20:06:39 浏览: 126
C++课程设计-酒店客房管理系统.zip
4星 · 用户满意度95%
### 回答1:
#include <iostream>
#include <string>
using namespace std;
class Person {
public:
int roomNum;
string name;
string IDNumber;
};
class Client : public Person {
public:
bool isRent;
double cost;
Client() {
isRent = false;
cost = 0;
}
void rentRoom(int num, string n, string id) {
if (!isRent) {
isRent = true;
roomNum = num;
name = n;
IDNumber = id;
cout << "Client " << name << " has successfully rented room " << roomNum << endl;
} else {
cout << "Sorry, the room has been rented out." << endl;
}
}
void returnRoom() {
if (isRent) {
isRent = false;
cout << "Client " << name << " has successfully returned room " << roomNum << endl;
} else {
cout << "Sorry, the room has not been rented out." << endl;
}
}
void addCost(double c) {
if (isRent) {
cost += c;
cout << "Client " << name << " has added " << c << " yuan to his/her bill" << endl;
} else {
cout << "Sorry, the room has not been rented out." << endl;
}
}
};
int main() {
Client c;
c.rentRoom(101, "Tom", "123456");
c.addCost(100);
c.returnRoom();
c.addCost(50);
c.rentRoom(102, "Lucy", "654321");
c.addCost(200);
c.returnRoom();
return 0;
}
### 回答2:
首先,我们可以设计一个名为Person的类,该类具有储存客房管理相关信息的属性,包括房号、客户姓名和身份证号。以下是Person类的C代码示例:
```c
#include<stdio.h>
struct Person {
int roomNumber;
char customerName[50];
char IDNumber[20];
};
```
接下来,我们可以设计一个名为Client的类,该类继承于Person类,它可以新增客户的订房、退房和消费金额等信息。以下是Client类的C代码示例:
```c
#include<stdio.h>
#include<string.h>
struct Person {
int roomNumber;
char customerName[50];
char IDNumber[20];
};
struct Client {
struct Person person;
float totalCost;
};
void bookRoom(struct Client* client, int roomNumber, char customerName[], char IDNumber[]) {
client->person.roomNumber = roomNumber;
strcpy(client->person.customerName, customerName);
strcpy(client->person.IDNumber, IDNumber);
printf("已预订房间号:%d\n", client->person.roomNumber);
printf("客户姓名:%s\n", client->person.customerName);
printf("客户身份证号:%s\n", client->person.IDNumber);
}
void checkOut(struct Client* client) {
printf("已退房房间号:%d\n", client->person.roomNumber);
printf("客户姓名:%s\n", client->person.customerName);
printf("客户身份证号:%s\n", client->person.IDNumber);
}
void setTotalCost(struct Client* client, float totalCost) {
client->totalCost = totalCost;
}
int main() {
struct Client client;
bookRoom(&client, 101, "张三", "1234567890");
checkOut(&client);
setTotalCost(&client, 1000.00);
printf("消费金额:%f\n", client.totalCost);
return 0;
}
```
以上代码示例中,我们使用了结构体来表示类的属性,通过指针传递参数来修改类的属性值。在主函数中,我们创建了一个Client对象,调用了bookRoom、checkOut和setTotalCost函数来执行相关操作,并输出了相关信息。
该测试算法的运行结果为:
```
已预订房间号:101
客户姓名:张三
客户身份证号:1234567890
已退房房间号:101
客户姓名:张三
客户身份证号:1234567890
消费金额:1000.000000
```
以上就是使用C语言设计两个酒店管理员客房管理的类的实现。
### 回答3:
Person类设计如下:
```c
#include <iostream>
#include <string>
using namespace std;
class Person {
private:
int roomNumber;
string clientName;
string idNumber;
public:
Person() {}
Person(int roomNum, string name, string id) {
roomNumber = roomNum;
clientName = name;
idNumber = id;
}
void setRoomNumber(int roomNum) {
roomNumber = roomNum;
}
void setClientName(string name) {
clientName = name;
}
void setIdNumber(string id) {
idNumber = id;
}
int getRoomNumber() {
return roomNumber;
}
string getClientName() {
return clientName;
}
string getIdNumber() {
return idNumber;
}
};
```
Client类设计如下:
```c
class Client : public Person {
private:
float totalCost;
public:
Client() {}
Client(int roomNum, string name, string id, float cost) : Person(roomNum, name, id) {
totalCost = cost;
}
void setTotalCost(float cost) {
totalCost = cost;
}
float getTotalCost() {
return totalCost;
}
void bookRoom(int roomNum, string name, string id) {
setRoomNumber(roomNum);
setClientName(name);
setIdNumber(id);
}
void checkOut() {
setRoomNumber(-1);
setClientName("");
setIdNumber("");
setTotalCost(0.0);
}
};
```
测试算法如下:
```c
int main() {
Client client;
client.bookRoom(101, "张三", "120102199001012345");
cout << "客户信息:" << client.getClientName() << " " << client.getIdNumber() << " 房间号:" << client.getRoomNumber() << endl;
client.setTotalCost(500.0);
cout << "消费金额:" << client.getTotalCost() << endl;
client.checkOut();
cout << "客户信息:" << client.getClientName() << " " << client.getIdNumber() << " 房间号:" << client.getRoomNumber() << endl;
return 0;
}
```
该测试算法首先创建一个Client对象,调用bookRoom方法订房,并输出客户信息和房间号。然后设置消费金额并输出。最后调用checkOut方法退房,并输出客户信息和房间号。
阅读全文