相关内容:
把 +=
运算符定义为成员函数,把 +
运算符定义为非成员函数;这是使用习惯;因为 +=
一般改变了原有对象的数据,而 +
则没有,所以 +=
定义为成员函数,而 +
定义为非成员函数;(-=
与 -
做类似处理)。
代码涉及内容:
- 算术运算符:
+
(加),-
(减),*
(乘),/
(除),%
(取模),+=
,-=
,*=
,/=
,% =
- 为了与内置操作符保持一致,算术操作符通常产生一个新值;
- 一般应使用复合赋值实现算术操作符。例如:用
+=
来实现+
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
88
89
90
91
92
93
94
95
96
97
98
99
using namespace std;
class Dog
{
};
class Cat
{
};
class Sales_item {
public:
Sales_item(const std::string &book, const unsigned units, const double amount)
:isbn(book), units_sold(units), revenue(amount) {}
Sales_item& operator+=(const Sales_item&);
Sales_item& operator-=(const Sales_item&);
friend std::ostream& operator<< (std::ostream&, const Sales_item&);
private:
std::string isbn;
unsigned units_sold;
double revenue;
};
// 把 += 运算符定义为成员函数,把 + 运算符定义为非成员函数;这是使用习惯;
// 因为 += 一般改变了原有对象的数据,而 + 则没有,所以 += 定义为成员函数,而 + 定义为非成员函数;
Sales_item operator+(const Sales_item&, const Sales_item&);
Sales_item operator-(const Sales_item&, const Sales_item&);
std::ostream& operator<< (std::ostream& out, const Sales_item& s)
{
out << s.isbn << "\t" << s.units_sold << "\t" << s.revenue;
return out;
}
Sales_item& Sales_item::operator+=(const Sales_item& rhs)
{
// 此处默认使用 this 指针,等效于:this->units_sold += rhs.units_sold;
units_sold += rhs.units_sold;
revenue += rhs.revenue;
return *this;
}
// 注意此处绝对不能返回一个引用,因为加的结果存到了一个新的对象里;
Sales_item operator+(const Sales_item& lhs, const Sales_item& rhs)
{
Sales_item ret(lhs);
ret += rhs;
return ret;
}
Sales_item& Sales_item::operator-=(const Sales_item& rhs)
{
// 此处第一个参数默认使用 this 指针;
units_sold -= rhs.units_sold;
revenue -= rhs.revenue;
return *this;
}
// 注意此处绝对不能返回一个引用,因为加的结果存到了一个新的对象里;
Sales_item operator-(const Sales_item& lhs, const Sales_item& rhs)
{
Sales_item ret(lhs);
ret -= rhs;
return ret;
}
int main()
{
Dog dog;
Cat cat;
int a = 12, b = 6, c;
c = a + b;
Sales_item item1(string("0-201-12345-X"), 10, 120.0);
Sales_item item2(string("0-201-12345-X"), 20, 200.0);
// 验证重载的 + 运算符;
Sales_item result = item1 + item2;
cout << result << endl;
// 验证重载的 += 运算符;
Sales_item item3(string("0-201-12345-X"), 5, 70.0);
result += item3;
cout << result << endl;
// 测试 重载的 -= 运算符;
result -= item2;
cout << result << endl;
// 测试 重载的 - 运算符;
result = item2 - item1;
cout << result << endl;
return 0;
}代码执行结果: