-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathItem10.cpp
More file actions
59 lines (48 loc) · 937 Bytes
/
Item10.cpp
File metadata and controls
59 lines (48 loc) · 937 Bytes
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
// Item 10: Have assignment operators return a reference to *this
#include <iostream>
using namespace::std;
class Animal
{
private:
int agility;
public:
/* Its a convention that assignment operators accepting any unconventional
* parameter type return a reference to *this.
*/
Animal& operator=(const Animal& rhs);
Animal& operator=(int agility);
Animal& operator+=(const Animal& rhs);
operator int();
};
Animal::operator int()
{
return this->agility;
}
Animal& Animal::operator=(const Animal& rhs)
{
this->agility = rhs.agility;
return *this;
}
Animal& Animal::operator=(int agility)
{
this->agility = agility;
return *this;
}
Animal& Animal::operator+=(const Animal& rhs)
{
this->agility += rhs.agility;
return *this;
}
int main()
{
Animal genX;
Animal genY;
Animal genZ;
genX = 10;
genZ = 20;
genY = genX;
genZ += genY;
cout << genX << endl;
cout << genY << endl;
cout << genZ << endl;
}