Overloading Operators

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

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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#ifndef EMPLOYEE_H
#define EMPLOYEE_H

#include <string>
using namespace 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>
using namespace 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.
      friend bool operator == (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 ==
bool operator == (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.

Hope this helps.
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.
Just access the salaries like you would normally and compare them directly, then return true or false.
 
return static_cast<unsigned long>( emp1.Salary * 100 ) == static_cast<unsigned long>( emp2.Salary * 100 );


?
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.
Topic archived. No new replies allowed.