Hospital Personnel Management System - C++ Inheritance
C++ Code for Hospital Staff Management
#include <iostream>
#include <string>
using namespace std;
// Base class
class Staff {
protected:
string name;
int id;
double salary;
public:
virtual void input() {
cout << "Enter Name: ";
getline(cin, name);
cout << "Enter ID: ";
cin >> id;
cout << "Enter Salary: ";
cin >> salary;
cin.ignore(); // Clear newline from buffer
}
virtual void display() const {
cout << "\nName: " << name << endl;
cout << "ID: " << id << endl;
cout << "Salary: $" << salary << endl;
}
virtual ~Staff() {}
};
// Derived class: Doctor
class Doctor : public Staff {
private:
string specialization;
int numSurgeries;
public:
void input() override {
Staff::input();
cout << "Enter Specialization: ";
getline(cin, specialization);
cout << "Enter Number of Surgeries Performed: ";
cin >> numSurgeries;
cin.ignore();
}
Hospital Personnel Management System - C++ Inheritance
void display() const override {
Staff::display();
cout << "Specialization: " << specialization << endl;
cout << "Number of Surgeries: " << numSurgeries << endl;
}
};
// Derived class: Admin
class Admin : public Staff {
private:
string department;
public:
void input() override {
Staff::input();
cout << "Enter Department (HR, Finance, etc.): ";
getline(cin, department);
}
void display() const override {
Staff::display();
cout << "Department: " << department << endl;
}
};
// Main function
int main() {
const int SIZE = 3; // You can change this as needed
Staff* staffList[SIZE];
// Create staff objects based on user input
for (int i = 0; i < SIZE; ++i) {
int choice;
cout << "\nEnter type of staff for entry " << i + 1 << " (1 - Doctor, 2 -
Admin): ";
cin >> choice;
cin.ignore(); // Clear input buffer
if (choice == 1) {
staffList[i] = new Doctor();
} else if (choice == 2) {
staffList[i] = new Admin();
} else {
cout << "Invalid choice, defaulting to Admin.\n";
staffList[i] = new Admin();
}
staffList[i]->input();
}
// Display staff details using runtime polymorphism
Hospital Personnel Management System - C++ Inheritance
cout << "\n--- Staff Details ---\n";
for (int i = 0; i < SIZE; ++i) {
staffList[i]->display();
cout << "------------------------\n";
}
// Free memory
for (int i = 0; i < SIZE; ++i) {
delete staffList[i];
}
return 0;
}