The primary objective of this project is to apply and demonstrate fundamental Object-Oriented Programming (OOP) principles to solve a real-world problem.
- Role Management: Differentiates between Doctors and Patients with distinct attributes and functionalities.
- Patient Specialization: Handles two types of patients—Patient and EmergencyPatient—with different billing logic.
- Dynamic Record Keeping: Uses dynamic memory allocation to manage lists of doctors and patients, allowing the system to handle a variable number of records.
- Automated Doctor Recommendation: Suggests the most cost-effective doctor based on the patient's illness and severity.
- Resource Management: Tracks the availability of hospital rooms and assigns them to incoming patients.
- Data Persistence: Saves the entire state of the system (doctors, patients, and room occupancy) to a file, allowing sessions to be resumed later.
Abstraction is achieved through the Person class, which serves as an abstract base class.
- Purpose: It defines a common, generic interface for any entity that is a "person" within the system. It establishes a contract that all derived classes must follow.
- Implementation:
- It contains a common attribute, name.
- It declares pure virtual functions (
display(),save(),load()), which forces any concrete subclass to provide its own implementation for these actions. - An object of type Person can never be instantiated on its own.
Inheritance is used to create a logical "is-a" relationship between classes, promoting code reuse and extensibility.
- Single-Level Inheritance: The Doctor and Patient classes inherit publicly from the Person class. They inherit the name attribute and the obligation to implement the pure virtual functions defined in Person.
- Multi-Level Inheritance: The EmergencyPatient class inherits from the Patient class. This allows EmergencyPatient to reuse all the properties and methods of a Patient while adding its own specialized behavior (a modified billing calculation).
Polymorphism allows objects of different classes to be treated as objects of a common parent class. The system leverages this powerfully through virtual functions.
- Implementation: The patients vector in the Hospital class is of type
vector<Patient*>. It can hold pointers to both Patient objects and EmergencyPatient objects. - Runtime Behavior:
- When
patient->display()is called on an element in the vector, C++ determines at runtime whether to execute thedisplay()method from the Patient class or the overridden version from the EmergencyPatient class. - Similarly,
patient->calculateBill()invokes the correct billing logic based on the actual object type, automatically applying the 1.5x multiplier for emergency cases.
- When
Encapsulation is the bundling of data (attributes) and the methods that operate on that data into a single unit (a class).
- Implementation: In all classes (Doctor, Patient, Hospital), data members are declared private or protected. This prevents direct, uncontrolled access from outside the class.
- Data Integrity: Public methods (e.g.,
getPatientCount(),setPatientCount()) are provided to allow controlled access to the object's data, ensuring data integrity. The main logic for managing the hospital is encapsulated within the Hospital class.