-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathoperator_overload.cpp
165 lines (135 loc) · 4.44 KB
/
operator_overload.cpp
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
/*
* @Author: Chacha
* @Date: 2022-02-24 17:47:37
* @Last Modified by: Chacha
* @Last Modified time: 2022-02-25 14:16:43
*/
/**
* 来源: https://www.runoob.com/cplusplus/cpp-overloading.html
*
* C++ 中的运算符重载
* 重载运算符是带有特殊名称的函数,函数名是由关键字 operator 和 其后要重载的运算符符号构成,
* 与其他函数一样,重载运算符有一个返回类型和一个参数列表。例如:
* Box operator+(const Box&);
* 声明加法运算符用于把两个 Box 对象相加,返回最终的 Box 对象。大多数的重载运算符可被定义为普通的
* 非成员函数或者被定义为类成员函数。如果我们定义上面的函数为类的非成员函数,那么我们需要为每次操作传递两个参数。
* 如下所示:
* Box operator+(const Box&, const Box&);
*
* 可重载运算符:
* 1. 双目运算符: + (加),-(减),*(乘),/(除),% (取模)
* 2. 关系运算符: ==(等于),!= (不等于),< (小于),> (大于),<=(小于等于),>=(大于等于)
* 3. 逻辑运算符: ||(逻辑或),&&(逻辑与),!(逻辑非)
* 4. 单目运算符: + (正),-(负),*(指针),&(取地址)
* 5. 自增自减运算符: ++(自增),--(自减)
* 6. 位运算符: | (按位或),& (按位与),~(按位取反),^(按位异或),,<< (左移),>>(右移)
* 7. 赋值运算符: =, +=, -=, *=, /= , % = , &=, |=, ^=, <<=, >>=
* 8. 空间申请与释放: new, delete, new[], delete[]
* 9. 其他运算符: ()(函数调用), ->(成员访问), (逗号), [](下标)
*
* 不可重载运算符:
* 1. 成员访问运算符: .
* 2. 成员指针访问运算符: .*, ->*
* 3. 域运算符: ::
* 4. 长度运算符: sizeof
* 5. 条件运算符: ?:
* 6. 预处理符号: #
*
*/
#include <iostream>
using namespace std;
class OperatorOverload
{
private:
/* data */
double width;
double height;
double length;
public:
OperatorOverload();
OperatorOverload(double, double, double);
/**
* 重载 + 运算符,用于把两个对象相加
* 当重载的运算符函数是全局函数时,需要在类中将该函数声明为友元
* 例如:
* friend OperatorOverload operator+(const OperatorOverload &a, const OperatorOverload &b)
* 因为其是全局函数,对应的参数个数为2个
*/
OperatorOverload operator+(const OperatorOverload &o);
// 重载 - 运算符,用于把两个对象相减
OperatorOverload operator-(const OperatorOverload &o);
// 重载 = 赋值运算符
void operator=(const OperatorOverload &o);
// 获取对象成员数据 width height length 的乘积
double getValume();
void showInfo(char const str[]);
~OperatorOverload();
};
OperatorOverload::OperatorOverload()
{
width = 0;
height = 0;
length = 0;
};
OperatorOverload::OperatorOverload(double w, double h, double l)
{
width = w;
height = h;
length = l;
cout << "构造函数" << endl;
};
OperatorOverload OperatorOverload::operator+(const OperatorOverload &o)
{
OperatorOverload a;
a.width = this->width + o.width;
a.height = this->height + o.height;
a.length = this->length + o.length;
return a;
};
OperatorOverload OperatorOverload::operator-(const OperatorOverload &o)
{
OperatorOverload a;
a.width = this->width - o.width;
a.height = this->height - o.height;
a.length = this->length - o.length;
return a;
};
void OperatorOverload::operator=(const OperatorOverload &o)
{
width = o.width + 1;
height = o.height + 2;
length = o.length + 3;
}
void OperatorOverload::showInfo(char const str[])
{
cout << str << " width: " << width;
cout << " height: " << height;
cout << " length: " << length << endl;
};
double OperatorOverload::getValume()
{
return width * height * length;
};
OperatorOverload::~OperatorOverload()
{
cout << "析构函数" << endl;
};
int main(int argc, char const *argv[])
{
/* code */
OperatorOverload obj1(1, 1, 1);
OperatorOverload obj2(2, 2, 2);
OperatorOverload obj3;
OperatorOverload obj4;
obj1.showInfo("obj1");
obj2.showInfo("obj2");
obj3 = obj1 + obj2;
obj4 = obj3 - obj1;
obj3.showInfo("obj3");
obj4.showInfo("obj4");
cout << "obj1 valume " << obj1.getValume() << endl;
cout << "obj2 valume " << obj2.getValume() << endl;
cout << "obj3 valume " << obj3.getValume() << endl;
cout << "obj4 valume " << obj4.getValume() << endl;
return 0;
}