To create an advanced C++ Employment Management application with an SQL database, we’ll structure our code using classes for employees, managers, salaries, and departments. We’ll integrate SQLite3 as the database backend. Let’s start by creating the necessary classes.
1. Employee Class
#include <sqlite3.h>
#include <string>
class Employee {
private:
int id;
std::string name;
double salary;
public:
Employee(int id, std::string name, double salary) : id(id), name(name), salary(salary) {}
void updateEmployee(std::string newName, double newSalary) {
sqlite3 *db;
if (sqlite3_open("employees.db", &db) == SQLITE_OK) {
std::string sql = "UPDATE employees SET name=?, salary=? WHERE id=?";
sqlite3_stmt *stmt;
if (sqlite3_prepare_v2(db, sql.c_str(), -1, &stmt, NULL) == SQLITE_OK) {
sqlite3_bind_text(stmt, 1, newName.c_str(), -1, SQLITE_STATIC);
sqlite3_bind_double(stmt, 2, newSalary);
sqlite3_bind_int(stmt, 3, id);
sqlite3_step(stmt);
sqlite3_finalize(stmt);
}
sqlite3_close(db);
}
}
void deleteEmployee() {
sqlite3 *db;
if (sqlite3_open("employees.db", &db) == SQLITE_OK) {
std::string sql = "DELETE FROM employees WHERE id=?";
sqlite3_stmt *stmt;
if (sqlite3_prepare_v2(db, sql.c_str(), -1, &stmt, NULL) == SQLITE_OK) {
sqlite3_bind_int(stmt, 1, id);
sqlite3_step(stmt);
sqlite3_finalize(stmt);
}
sqlite3_close(db);
}
}
};
Explanation:
- The
Employee
class represents individual employees in the organization. - It contains attributes
id
,name
, andsalary
to store employee information. - The
updateEmployee
method updates employee details in the SQLite database. - The
deleteEmployee
method deletes the employee from the SQLite database.
2. Manager Class
#include <string>
class Manager : public Employee {
private:
std::string department;
public:
Manager(int id, std::string name, double salary, std::string department) : Employee(id, name, salary), department(department) {}
};
Explanation:
- The
Manager
class represents managers in the organization and inherits from theEmployee
class. - It adds an additional attribute
department
to store the department managed by the manager.
3. Salary Class
class Salary {
private:
int employeeId;
double amount;
public:
Salary(int employeeId, double amount) : employeeId(employeeId), amount(amount) {}
};
Explanation:
- The
Salary
class represents salary information for employees. - It contains attributes
employeeId
andamount
to store the employee’s ID and salary amount.
4. Department Class
#include <vector>
class Department {
private:
int id;
std::string name;
std::vector<Employee> employees;
public:
Department(int id, std::string name) : id(id), name(name) {}
void addEmployee(Employee employee) {
employees.push_back(employee);
}
void removeEmployee(Employee employee) {
employees.erase(std::remove(employees.begin(), employees.end(), employee), employees.end());
}
};
Explanation:
- The
Department
class represents departments in the organization. - It contains attributes
id
,name
, andemployees
to store department information and a vector of employees in the department. - The
addEmployee
method adds an employee to the department. - The
removeEmployee
method removes an employee from the department.
Database Integration:
We’ll use SQLite3 as the database backend. Below is a simple example of connecting to an SQLite database:
#include <sqlite3.h>
int main() {
sqlite3 *db;
if (sqlite3_open("employees.db", &db) == SQLITE_OK) {
char *errMsg = 0;
const char *sql = "CREATE TABLE IF NOT EXISTS employees (id INTEGER PRIMARY KEY, name TEXT NOT NULL, salary REAL NOT NULL)";
if (sqlite3_exec(db, sql, NULL, 0, &errMsg) == SQLITE_OK) {
// Table created successfully
} else {
// Failed to create table
}
sqlite3_close(db);
} else {
// Failed to open database
}
return 0;
}
Explanation:
- This code establishes a connection to an SQLite database named
employees.db
. - It creates a table named
employees
if it does not already exist, with columns forid
,name
, andsalary
. - After executing SQL commands, the database connection is closed.
View and Controller:
For the view and controller components, you can use frameworks like Qt or wxWidgets to create a graphical user interface (GUI) for the application. The controller will handle user actions and invoke appropriate methods in the model to perform operations on the data. With these components in place, users can view, edit, and delete employee details through the GUI.
Welcome to DevTechTutor.com, your ultimate resource for mastering web development and technology! Whether you're a beginner eager to dive into coding or an experienced developer looking to sharpen your skills, DevTechTutor.com is here to guide you every step of the way. Our mission is to make learning web development accessible, engaging, and effective.