I need some help on implementing the friend functions in the following program that overload the operators == and <<. I am pretty sure I have them declared right, but I am getting stuck when trying to implement them. Thanks
#ifndef EMPLOYEE_H
#define EMPLOYEE_H
#include <string>
usingnamespace std;
class Employee
{
public:
// Constructor to assign employee id and name.
Employee(int Idin, string Namein);
// Member function to return the employee id
#ifndef EMPLOYEE_H
#define EMPLOYEE_H
#include <string>
usingnamespace std;
class Employee
{
public:
// Constructor to assign employee id and name.
Employee(int Idin, string Namein);
// Member function to return the employee id
int getId();
// Member function to return the employee name
string getName();
// Member function to set the salary of the employee
void setSalary(doublr theSalary);
// Member function to return the salary of the employee
double getSalary();
// Overload the comparison operator == to compare the salary of two employees.
friendbooloperator == (const Employee& emp1, const Employee& emp2);
// Overload the stream operator << to display the employee's id, name and salary.
friend ostream& operator << (ostream& ostr, const Employee& emp);
private: // Declare member variables
int Id;
string Name;
double Salary;
};
// Implementation of the constructor Employee::Employee(int Idin, string Namein)
{
Id=Idin;
Name=Namein;
}
// Implementation of the get ID function
int Employee::getId()
{
return Id;
}
// Implementation of the get Name function
string Employee::getName()
{
return Name;
}
// Implementation of the set Salary function
void Employee::setSalary(double theSalary)
{
salary=theSalary;
}
// Implementationof the get Salary function
double Employee::getSalary()
{
return Salary;
}
// Implementation of the friend function that overloads operator ==
booloperator == (const Employee& emp1, const Employee& emp2)
{
Need Help
}
// Implementation of the friend function that overloads operator <<
ostream& operator << (ostream& ostr, const Employee& emp)
{
Need Help
}
#endif
On line 78 (your equality operator), you need to return true or false depending on whether or not emp1 and emp2 are the same employee. I would simply check to see that their employee IDs are the same.
For line 84 (your insertion operator), you need to decide how to represent an employee in file. It might be as simple as
1234
John Jones
12.80
(Note the extra line at the end there. This way, humans can read the employee file more easily.)
Don't forget to return ostr; at the end of the function.
I am sorry, I should have been more clearer. The overloaded == (friend function) is used to compare the salaries of two employees. The overloaded << (friend function) is used to display the id, name and salary of the employee. All of this is in a header file. I will use it in another main program.
I can not figure out how the function will evaluate the salaries using emp1 and emp2.
Though I would have to say that I would not use operator== in this case.
Forget your program. If you asked me how to tell if two employees were the same, I'd say to
compare their IDs. Can't compare names, because the company might have two employees
with the same first and last name. Certainly comparing salaries doesn't make sense either.