Answer to Question #326257 in C++ for gayathri

Question #326257

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. 



1
Expert's answer
2022-04-11T02:07:20-0400
#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;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS