0% found this document useful (0 votes)
52 views3 pages

Hospital Staff Management CPP

The document presents a C++ implementation of a Hospital Personnel Management System using inheritance. It defines a base class 'Staff' and two derived classes 'Doctor' and 'Admin', allowing for input and display of staff details. The main function facilitates the creation of staff objects based on user input and demonstrates runtime polymorphism for displaying their information.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
52 views3 pages

Hospital Staff Management CPP

The document presents a C++ implementation of a Hospital Personnel Management System using inheritance. It defines a base class 'Staff' and two derived classes 'Doctor' and 'Admin', allowing for input and display of staff details. The main function facilitates the creation of staff objects based on user input and demonstrates runtime polymorphism for displaying their information.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

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;
}

You might also like