#include <iostream>
#include <string>
#include <stdexcept>
const int MAX_EMPLOYEES = 5;
class Employee {
protected:
std::string name;
double salary;
int age;
public:
Employee(const std::string& n, double s, int a) : name(n), salary(s), age(a) {
if (name.empty()) {
throw std::invalid_argument("Name cannot be empty.");
}
if (salary < 0) {
throw std::invalid_argument("Salary cannot be negative.");
}
if (age < 0) {
throw std::invalid_argument("Age cannot be negative.");
}
}
virtual void displayDetails() const {
std::cout << "Name: " << name << ", Salary: " << salary << ", Age: " << age << std::endl;
}
};
class EmployeeManagement {
private:
Employee* employees[MAX_EMPLOYEES];
public:
EmployeeManagement() {
try {
employees[0] = new Employee("John", 50000, 30);
employees[1] = new Employee("Alice", 60000, 28);
employees[2] = new Employee("Bob", 55000, 35);
employees[3] = new Employee("Emily", 52000, 32);
employees[4] = new Employee("", 48000, 27); // Intentional error: Empty name
} catch (std::invalid_argument& ex) {
std::cout << "Exception caught: " << ex.what() << std::endl;
}
}
void displayAllDetails() const {
std::cout << "Details of Employees:" << std::endl;
for (int i = 0; i < MAX_EMPLOYEES; ++i) {
if (employees[i] != nullptr) {
employees[i]->displayDetails();
}
}
}
~EmployeeManagement() {
for (int i = 0; i < MAX_EMPLOYEES; ++i) {
delete employees[i];
}
}
};
int main() {
EmployeeManagement empManager;
empManager.displayAllDetails();
return 0;
}
To embed this project on your website, copy the following code and paste it into your website's HTML: