1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
#include <iostream> #include <vector> #include <string> #include <algorithm> class Employee { public: Employee(std::string name, int performance) : name(name), performance(performance) {} std::string getName() const { return name; } int getPerformance() const { return performance; } void setPerformance(int newPerformance) { performance = newPerformance; } private: std::string name; int performance; }; class HRSystem { public: void addEmployee(const Employee& employee) { employees.push_back(employee); } void evaluatePerformance() { for (auto& employee : employees) { int newPerformance = employee.getPerformance() + (std::rand() % 21 - 10); // Random change between -10 and +10 employee.setPerformance(std::clamp(newPerformance, 0, 100)); // Ensure performance is between 0 and 100 } } void printEmployees() const { for (const auto& employee : employees) { std::cout << "Name: " << employee.getName() << ", Performance: " << employee.getPerformance() << '\n'; } } private: std::vector<Employee> employees; }; int main() { std::srand(static_cast<unsigned>(std::time(0))); HRSystem hr; // Adding employees hr.addEmployee(Employee("Alice", 80)); hr.addEmployee(Employee("Bob", 70)); hr.addEmployee(Employee("Charlie", 90)); std::cout << "Initial employee performance:\n"; hr.printEmployees(); int months = 6; for (int i = 0; i < months; ++i) { hr.evaluatePerformance(); std::cout << "\nPerformance after month " << (i + 1) << ":\n"; hr.printEmployees(); } return 0; } |
Explanation
- Class Definitions:
Employee
Class:- Private Members:
name
: The name of the employee.performance
: The performance rating of the employee (0 to 100).
- Public Methods:
Employee(std::string name, int performance)
: Constructor initializes the employee’s name and performance rating.std::string getName() const
: Returns the employee’s name.int getPerformance() const
: Returns the employee’s performance rating.void setPerformance(int newPerformance)
: Sets a new performance rating.
- Private Members:
HRSystem
Class:- Private Members:
employees
: A vector storingEmployee
objects.
- Public Methods:
void addEmployee(const Employee& employee)
: Adds anEmployee
to the system.void evaluatePerformance()
: Simulates performance evaluation by randomly changing the performance rating of each employee. The change is between -10 and +10, clamped to a range of 0 to 100.void printEmployees() const
: Prints the names and performance ratings of all employees.
- Private Members:
main
Function:- Initializes the random seed with the current time.
- Creates an
HRSystem
object and adds several employees. - Prints the initial performance of the employees.
- Simulates performance evaluation over a number of months, updating and printing employee performance after each month.