C++ Hospital Management Project
Patients, doctors, appointments, and an emergency queue that jumps the line.
What you will build
Run a hospital's front desk: patient records, doctor schedules, appointment booking, and an emergency intake where severity — not arrival order — decides who is seen next. It's a rich modelling project where several subsystems (records, scheduling, triage) interact.
What you will learn
- Priority queues for triage by severity
- Schedule conflict detection
- Multi-entity data modelling (patient/doctor/appointment)
- Search across large record sets
- Persisting an interconnected dataset
Feature checklist
- Patient registration with medical history summary
- Doctor directory with specialisations
- Book appointments; double-booking is refused
- Emergency queue ordered by severity score
- Discharge summary and billing total
- Daily report: admissions, discharges, occupancy
Starter code
#include <queue>
#include <string>
#include <vector>
struct EmergencyCase {
std::string patient;
int severity; // 1 (minor) .. 5 (critical)
long arrival; // tie-breaker: earlier first
};
struct WorstFirst {
bool operator()(const EmergencyCase& a, const EmergencyCase& b) const {
if (a.severity != b.severity) return a.severity < b.severity;
return a.arrival > b.arrival;
}
};
class EmergencyRoom {
std::priority_queue<EmergencyCase, std::vector<EmergencyCase>,
WorstFirst> queue_;
long clock_ = 0;
public:
void admit(std::string patient, int severity) {
queue_.push({std::move(patient), severity, clock_++});
}
std::string treatNext() { // most severe, then longest waiting
if (queue_.empty()) return "";
auto top = queue_.top(); queue_.pop();
return top.patient;
}
size_t waiting() const { return queue_.size(); }
};
// Appointment booking pairs this with a per-doctor calendar
// that rejects overlapping time slots.Full walkthrough
Step 1: Model the people and the place
Patients (id, name, history summary), Doctors (id, name, specialisation), Appointments (patient, doctor, time slot), and the EmergencyRoom. class Hospital owns the registries as maps by ID. Registration validates uniqueness of IDs like every registry in this catalogue — the patterns are now familiar, and this project spends its new budget on the two hard parts: appointment conflicts and the emergency queue.
Step 2: Book appointments with double-booking refusal
An appointment claims a (doctor, slot). Before inserting, check no existing appointment holds the same doctor AND an overlapping slot — the check is a scan over that doctor's appointments. Refusals must say WHY ('Dr. Bello is booked at 10:00 — nearest free slot 10:30'), because a bare 'no' at a hospital desk is a queue of angry people.
Step 3: The emergency queue: a priority queue with a comparator
struct EmergencyCase { patientId, severity, arrivalOrder } goes into a std::priority_queue with struct WorstFirst — highest severity on top, ties broken by arrivalOrder (earlier first). Like the Huffman comparator, priority_queue is a max-heap, so 'worst first' means the comparator reports lower priority for the case that should surface sooner. Severity is a scored triage number (say 1–100) computed at admission from the presenting complaint's category.
struct WorstFirst {
bool operator()(const EmergencyCase& a, const EmergencyCase& b) const {
if (a.severity != b.severity) return a.severity < b.severity;
return a.arrivalOrder > b.arrivalOrder; // earlier arrival wins ties
}
};Step 4: admit() and treatNext(): the emergency workflow
admit() scores severity, stamps arrivalOrder from a counter, pushes the case, and prints queue position. treatNext() pops the TOP — worst severity, not first arrival — assigns the next free doctor or treatment bay, and logs it. The whole moral weight of the system sits in that pop order: a stable, explainable queue is a fairness mechanism, and the arrivalOrder tiebreak is what stops two equal severities from being served randomly.
Step 5: Discharge with a billing total
Discharging a patient assembles their charges — consultation, bed days, procedures recorded during the stay — into an itemised total. Items are appended to the patient's record as events happen (the transaction-history pattern from the bank project), so the bill is a printout of history, never a reconstruction.
Step 6: The daily report
Admissions today, discharges today, current occupancy (admitted minus discharged — derived from the patient states), emergencies treated, and the longest current wait. Management reports are aggregations over the same registries; if a number cannot be derived from your data, the data model is missing a field — find it now, not at report time.
Step 7: Test the triage story
Admit three emergencies in order: severity 40 (sprained ankle), then 90 (chest pain), then 40 (fever). treatNext() must take the 90 FIRST despite arriving second, then the ankle (arrival tiebreak over the fever). Then double-book a doctor and watch the refusal; discharge the chest-pain patient and verify the itemised bill sums exactly.
Common mistakes
- Breaking severity ties randomly (or by heap luck) instead of by arrival order — an unfair queue in the one building where fairness is triaged by protocol.
- Allowing a doctor two appointments in one slot because the conflict check scanned patients instead of the doctor's own schedule.
- Reconstructing the bill at discharge from memory instead of itemising charges as events when they occurred.
Compile and run
g++ -std=c++17 -o hospital hospital.cppSample run
$ ./hospital
1) Register 2) Book appointment 3) Emergency admit 4) Treat next 5) Discharge + bill 6) Daily report 0) Exit
> 3
Patient ID:
> P209
Presenting complaint:
> Sprained ankle
Triage severity scored: 40 — queued (position 1)
1) Register 2) Book appointment 3) Emergency admit 4) Treat next 5) Discharge + bill 6) Daily report 0) Exit
> 3
Patient ID:
> P210
Presenting complaint:
> Chest pain
Triage severity scored: 90 — queued (position 1, ahead of lower severities)
> 4
Treating next: P210 (chest pain, severity 90) — arrived second, treated first: worst first.
> 2
Refused: Dr. Bello is already booked at 10:00. Nearest free slot: 10:30.
> 6
Today: 12 admissions · 5 discharges · occupancy 18 beds · emergencies treated 4
> 0Try it in your browser
No browser demo is included for this project. Build the C++ version below — that is the real program.