Create a class Employee with following data members, name, id, and salary as private data member. Create array of objects for four employees, compare their salary using operator overloading, and display the records of the student who is getting less salary.
#include <iostream>
#include <string>
class Employee {
private:
std::string name, id;
int salary;
public:
Employee() {}
Employee(std::string _name, std::string _id, int _salary) : name(_name), id(_id), salary(_salary) {}
friend bool operator <(const Employee& a, const Employee& b) {
return a.salary < b.salary;
}
friend std::ostream& operator <<(std::ostream& os, const Employee& e) {
os << "Name: " << e.name << "\nID: " << e.id << "\nSalary: " << e.salary << '\n';
return os;
}
~Employee() {}
};
int main() {
const int AMOUNT = 4;
Employee employees[AMOUNT];
for (int i = 0; i < AMOUNT; ++i) {
std::string name, id;
int salary;
int index = i + 1;
std::cout << "\nEnter employee[" << index << "] name: ";
std::cin >> name;
std::cout << "Enter employee[" << index << "] id: ";
std::cin >> id;
std::cout << "Enter employee[" << index << "] salary: ";
std::cin >> salary;
employees[i] = Employee(name, id, salary);
}
Employee min = employees[0];
for (int i = 1; i < AMOUNT; ++i) {
if (employees[i] < min) {
min = employees[i];
}
}
std::cout << "\nEmployee with the smallest salary:\n----------------------------------\n" << min;
return 0;
}
Comments
Leave a comment