-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy path14.17.h
62 lines (55 loc) · 1.64 KB
/
14.17.h
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
/*
* Exercise 14.17: Should the class you chose for exercise 7.40 from § 7.5.1
* (p. 291) define the equality operators? If so, implement them. If not,
* explain why not.
*
* By Faisal Saadatmand
*/
#ifndef Employee_data_H
#define Employee_data_H
#include <iostream>
#include <string>
class Employee {
friend std::ostream& operator<<(std::ostream &, const Employee &);
friend std::istream& operator>>(std::istream &, Employee &);
friend bool operator==(const Employee &, const Employee &);
friend bool operator!=(const Employee &, const Employee &);
public:
Employee() = default;
Employee(const std::string &num) : employeeNo(num) {}
Employee(const std::string &fn, const std::string &ln,
const std::string &num, unsigned sal) :
firstName(fn), lastName(ln),
employeeNo(num), salary(sal) {}
Employee(std::istream &is) { operator>>(is, *this); }
private:
std::string firstName;
std::string lastName;
std::string employeeNo;
double salary = 0.0;
};
// non-member functions
std::istream& operator>>(std::istream &is, Employee &obj)
{
is >> obj.firstName >> obj.lastName
>> obj.employeeNo >> obj.salary;
if (!is)
obj = Employee();
return is;
}
std::ostream& operator<<(std::ostream &os, const Employee &obj)
{
os << obj.firstName << " " << obj.lastName
<< " " << obj.employeeNo << " " << obj.salary;
return os;
}
bool operator==(const Employee &lhs, const Employee &rhs)
{
return lhs.firstName == rhs.firstName && lhs.lastName == rhs.lastName
&& lhs.employeeNo == rhs.employeeNo && lhs.salary == rhs.salary;
}
bool operator!=(const Employee &lhs, const Employee &rhs)
{
return !(lhs == rhs);
}
#endif