|
1. 类的内部重载 (Overload)
6 + 1
3.14 + 3.45
5 + 3.14
实际上以上已经实现了重载。
class MyComlex
{
public:
MyComlex(double real = 0.0f, double image = 0.0f);
~MyComlex(){}
void Display() const;
private:
double m_real; // 实部
double m_image; // 虚部
};
MyComlex::MyComlex(double real, double image):
m_real(real),m_image(image){ }
void MyComlex::Display() const{
cout << &#34;(&#34; << m_real << &#34; + &#34; << m_image << &#34;i)&#34; << endl;
}
int main(){
MyComlex obj_complex(3,5);
obj_complex.Display();
MyComlex obj_complex2(6, 7);
obj_complex2.Display();
// MyComlex obj_add = obj_complex1 + obj_complex2;
// 上式子无法进行相加
// 没有与这些操作数匹配的 &#34;+&#34; 运算符C/C++(349)
return 0;
}
输出:
(3 + 5i)
(6 + 7i)如果要对复数进行运算就应该进行运算符重载。使用operator关键词
class MyComlex
{
public:
MyComlex(double real = 0.0f, double image = 0.0f);
~MyComlex(){}
void Display() const;
// 重载运算符 返回值类型 函数名称 (形参列表){}
// 函数名:operator+(运算符名称)
MyComlex operator+(const MyComlex& other) const;
private:
double m_real; // 实部
double m_image; // 虚部
};
MyComlex::MyComlex(double real, double image):
m_real(real),m_image(image){ }
void MyComlex::Display() const{
cout << &#34;(&#34; << m_real << &#34; + &#34; << m_image << &#34;i)&#34; << endl;
}
MyComlex MyComlex::operator+(const MyComlex& other) const {
MyComlex obj;
obj.m_real = this->m_real + other.m_real;
obj.m_image = this->m_image + other.m_image;
return obj;
}
int main(){
MyComlex obj_complex1(3,5);
obj_complex1.Display();
MyComlex obj_complex2(6, 7);
obj_complex2.Display();
MyComlex obj_add = obj_complex1 + obj_complex2;
obj_add.Display();
return 0;
}
输出:
(3 + 5i)
(6 + 7i)
(9 + 12i)
疑问:对于加法重载而言,究竟是
obj_complex1.operator+(obj_complex2);
还是:
obj_complex2.operator+(obj_complex1);
对于甲方重载可以混用,但是对于减法却有不同的效果。
实际上:
obj_complex1.operator+(obj_complex2); // 相当于是obj_complex1 + obj_complex2;
obj_complex2.operator+(obj_complex1); // 相当于是obj_complex2 + obj_complex1;
2. 全局重载 (Overload)
class MyComlex
{
public:
MyComlex(double real = 0.0f, double image = 0.0f);
~MyComlex(){}
void Display() const;
// 友元可以从外部访问私有变量
friend MyComlex operator+(const MyComlex &obj_1, const MyComlex &obj_2);
private:
double m_real; // 实部
double m_image; // 虚部
};
MyComlex::MyComlex(double real, double image):
m_real(real),m_image(image){ }
void MyComlex::Display() const{
cout << &#34;(&#34; << m_real << &#34; + &#34; << m_image << &#34;i)&#34; << endl;
}
MyComlex operator+(const MyComlex& obj_1, const MyComlex& obj_2){
return MyComlex(obj_1.m_real + obj_2.m_real, obj_1.m_image + obj_2.m_image);
}
int main(){
MyComlex obj_complex1(3,5);
obj_complex1.Display();
MyComlex obj_complex2(6, 7);
obj_complex2.Display();
MyComlex obj_add = obj_complex1 + obj_complex2;
obj_add.Display();
return 0;
}
输出:
(3 + 5i)
(6 + 7i)
(9 + 12i)3. 运算符重载规则
可以重载和不可以重载的运算符
下面是可重载的运算符列表:
- 双目算术运算符 + (加),-(减),*(乘),/(除),% (取模)
- 关系运算符 == (等于),!= (不等于),< (小于),> (大于),<=(小于等于),>=(大于等于)
- 逻辑运算符 ||(逻辑或),&&(逻辑与),!(逻辑非)
- 单目运算符 + (正),-(负),*(指针),&(取地址)
- 自增自减运算符 ++(自增),--(自减)
- 位运算符 | (按位或),& (按位与),~ (按位取反),^ (按位异或), << (左移),>>(右移)
- 赋值运算符 =, +=, -=, *=, /= , %= , &=, |=, ^=, <<=, >>=
- 空间申请与释放 new, delete, new[] , delete[]
- 其他运算符 ()(函数调用),->(成员访问),,(逗号),[](下标)
下面是不可重载的运算符列表:
运算符 | 符号 | 1 | 成员访问运算符 | . | 2 | 成员指针访问运算符 | .*, ->* | 3 | 域运算符 | :: | 4 | 长度运算符 | sizeof | 5 | 条件运算符 | ?: | 6 | 条件运算符 | ?: | 7 | 预处理符号 | # | 重载不可以改变优先级
obj = obj1 + obj2 * obj3;
// 重载之后依然会线运算乘法再运行加法运算
重载不会改变运算符用法
例如把加法重载成减法运算符
重载不可以有默认参数
这样会改变了运算符的目数。
重载可以在类内部写,也可以作为全局函数。
二者是有区别的
特殊运算符
()(函数调用),->(成员访问),=(赋值),[](下标) 只能以成员函数的形式重载。
4. 输入>>输出<<重载
输入输出参数,最好作为友元函数重载,以全局函数的形式来写。
class MyComlex
{
public:
MyComlex(double real = 0.0f, double image = 0.0f)
: m_real(real),m_image(image) { }
void Display() const;
friend istream &operator>>(istream& in, MyComlex & obj);
friend ostream& operator<<(ostream& out, MyComlex& obj);
private:
double m_real; // 实部
double m_image; // 虚部
};
void MyComlex::Display() const{
cout << &#34;(&#34; << m_real << &#34; + &#34; << m_image << &#34;i)&#34; << endl;
}
// 对输入输出运算符的重载格式
istream& operator>>(istream& in, MyComlex& obj)
{
in >> obj.m_real >> obj.m_image;
return in;
}
ostream& operator<<(ostream& out, MyComlex& obj)
{
out << &#34;(&#34; << obj.m_real << &#34; + &#34; << obj.m_image << &#34;i)&#34; << endl;
return out;
}
int main(){
MyComlex obj;
cin >> obj; // 隐式调用
cout << obj << endl;
operator>>(cin, obj); // 显式调用
operator<<(cout, obj);
obj.Display();
return 0;
}
输入输出:
6 7
(6 + 7i)
8 9
(8 + 9i)
(8 + 9i) |
|