用c++写一个医院挂号系统的代码,并给出解释
时间: 2024-02-13 09:05:49 浏览: 24
以下是一个简单的用C++编写的医院挂号系统代码:
```
#include <iostream>
#include <string>
#include <vector>
using namespace std;
class Patient {
private:
string name;
int age;
string gender;
string disease;
public:
Patient(string n, int a, string g, string d) {
name = n;
age = a;
gender = g;
disease = d;
}
void display() {
cout << "Name: " << name << endl;
cout << "Age: " << age << endl;
cout << "Gender: " << gender << endl;
cout << "Disease: " << disease << endl;
}
};
class Hospital {
private:
vector<Patient> patients;
public:
void addPatient(Patient p) {
patients.push_back(p);
cout << "Patient added successfully!" << endl;
}
void displayPatients() {
for (int i = 0; i < patients.size(); i++) {
patients[i].display();
cout << endl;
}
}
};
int main() {
Hospital hospital;
string name, gender, disease;
int age;
int choice;
do {
cout << "1. Add Patient" << endl;
cout << "2. Display Patients" << endl;
cout << "3. Exit" << endl;
cout << "Enter your choice: ";
cin >> choice;
switch (choice) {
case 1:
cout << "Enter patient name: ";
cin >> name;
cout << "Enter patient age: ";
cin >> age;
cout << "Enter patient gender: ";
cin >> gender;
cout << "Enter patient disease: ";
cin >> disease;
Patient p(name, age, gender, disease);
hospital.addPatient(p);
break;
case 2:
hospital.displayPatients();
break;
case 3:
cout << "Exiting program..." << endl;
break;
default:
cout << "Invalid choice! Try again." << endl;
break;
}
} while (choice != 3);
return 0;
}
```
这个程序包括两个类:Patient和Hospital。Patient类表示患者,包含姓名、年龄、性别和疾病等信息,并有一个display方法用于显示患者信息。Hospital类表示医院,包含一个患者列表,有两个方法:addPatient用于添加患者到列表中,displayPatients用于显示所有患者的信息。
在主函数中,我们首先创建了一个Hospital对象,并通过一个do-while循环来提供用户菜单选择。用户可以选择添加患者、显示所有患者信息或退出程序。当用户选择添加患者时,程序会提示用户输入患者姓名、年龄、性别和疾病等信息,并创建一个Patient对象,然后将该对象添加到Hospital对象的患者列表中。当用户选择显示所有患者信息时,程序会遍历Hospital对象的患者列表,并调用每个患者对象的display方法来显示其信息。
这个程序可以作为医院挂号系统的一个简单实现,当然,实际的医院挂号系统可能需要更多的功能和复杂性。