Employee Database with CRUD Operations in C++

 

Introduction

In this guide, we will create a simple employee database using C++ that performs CRUD (Create, Read, Update, Delete) operations. CRUD operations are essential for interacting with databases in modern applications, and they allow us to manage employee data in a simple and efficient way.

Objective

The objective of this program is to design a system where you can manage employee records by adding, viewing, updating, and deleting information. This will help you understand how basic database operations can be implemented in C++.

Code for Employee Database

#include 
#include 
#include 
using namespace std;

class Employee {
public:
    int id;
    string name;
    string position;
    double salary;

    Employee(int id, string name, string position, double salary) {
        this->id = id;
        this->name = name;
        this->position = position;
        this->salary = salary;
    }

    void display() {
        cout << "ID: " << id << ", Name: " << name << ", Position: " << position << ", Salary: " << salary << endl;
    }
};

class EmployeeDatabase {
private:
    vector employees;

public:
    void createEmployee() {
        int id;
        string name, position;
        double salary;
        cout << "Enter Employee ID: "; cin >> id;
        cin.ignore(); // to clear the buffer
        cout << "Enter Employee Name: ";
        getline(cin, name);
        cout << "Enter Employee Position: ";
        getline(cin, position);
        cout << "Enter Employee Salary: "; cin >> salary;

        Employee emp(id, name, position, salary);
        employees.push_back(emp);
        cout << "Employee added successfully!" << endl;
    }

    void readEmployees() {
        if (employees.empty()) {
            cout << "No employees in the database." << endl;
            return;
        }
        for (const auto& emp : employees) {
            emp.display();
        }
    }

    void updateEmployee() {
        int id;
        cout << "Enter Employee ID to update: "; cin >> id;
        bool found = false;
        for (auto& emp : employees) {
            if (emp.id == id) {
                found = true;
                string name, position;
                double salary;
                cout << "Enter new Employee Name: ";
                cin.ignore();
                getline(cin, name);
                cout << "Enter new Employee Position: ";
                getline(cin, position);
                cout << "Enter new Employee Salary: "; cin >> salary;

                emp.name = name;
                emp.position = position;
                emp.salary = salary;
                cout << "Employee updated successfully!" << endl;
                break;
            }
        }
        if (!found) {
            cout << "Employee not found." << endl;
        }
    }

    void deleteEmployee() {
        int id;
        cout << "Enter Employee ID to delete: "; cin >> id;
        bool found = false;
        for (auto it = employees.begin(); it != employees.end(); ++it) {
            if (it->id == id) {
                found = true;
                employees.erase(it);
                cout << "Employee deleted successfully!" << endl;
                break;
            }
        }
        if (!found) {
            cout << "Employee not found." << endl;
        }
    }
};

int main() {
    EmployeeDatabase db;
    int choice;
    do {
        cout << "\nEmployee Database Menu\n";
        cout << "1. Add Employee\n";
        cout << "2. View Employees\n";
        cout << "3. Update Employee\n";
        cout << "4. Delete Employee\n";
        cout << "5. Exit\n";
        cout << "Enter your choice: "; cin >> choice;

        switch (choice) {
            case 1:
                db.createEmployee();
                break;
            case 2:
                db.readEmployees();
                break;
            case 3:
                db.updateEmployee();
                break;
            case 4:
                db.deleteEmployee();
                break;
            case 5:
                cout << "Exiting the program...\n";
                break;
            default:
                cout << "Invalid choice! Please try again.\n";
        }
    } while (choice != 5);
    return 0;
}

Program Explanation

The program consists of two main classes:

  • Employee: This class holds the data for an employee such as ID, name, position, and salary. It also has a display method to output the employee’s details.
  • EmployeeDatabase: This class handles CRUD operations like adding, viewing, updating, and deleting employees. It uses a vector to store the employee records.

The program runs in a loop, presenting a menu of operations to the user. The user can perform CRUD actions based on their choice. The loop continues until the user chooses to exit.

How to Run the Program

  1. Open your C++ development environment or text editor (such as Visual Studio, Code::Blocks, or a terminal-based IDE).
  2. Copy and paste the provided code into a new C++ source file (e.g., employee_database.cpp).
  3. Compile the program using the C++ compiler.
  4. Run the compiled executable to interact with the employee database.
© 2025 Learn Programming. All rights reserved.

 

One Reply to “Employee Database with CRUD Operations in C++”

Leave a Reply

Your email address will not be published. Required fields are marked *