操作符重载
定义:
就是把已经定义的、有一定功能的操作符进行重新定义,来完成更为细致具体的运算等功能。操作符重载可以将概括性的抽象操作符具体化,便于外部调用而无需知晓内部具体运算过程。就是把操作符(比如'+,-,*,/'这些运算符)赋于新的意义。
目的:
C++有许多内置的数据类型,包括int,char,double等,每一种类型都有许多运算符,例如加,减,乘,除等。当用户定义了类的对象时,两个对象之间是不能进行这些操作的,比如Complex类的对象a+b,这样的语句如果没有重载+运算符就会出错。但C++允许用户把这些运算符添加到自已的类中以方便类对象之间的运算就像内置类型的运算一样方便,比如对象a+b这样就很明白更容易懂,当然也可以在类中定义一个对象间相加的函数,比如a.add(b)调用函数add()以实现两个对象a和b相加,但是这条语句没有比a+b更容易让人理解。
重载限制
- 并不是所有的操作符都能被重载。除了"sizeof"、"."、".*"、"::"、"?:"、"typeid"这几个运算符不能被重载,其他运算符都能被重载。
- 重载不能改变该运算符用于内置类型时的函义,程序员不能改变运算符+用于两个int型时的含义。
- 运算符函数的参数至少有一个必须是类的对象或者类的对象的引用。这种规定可以防止程序员运用运算符改变内置类型的函义。
- 重载不能改变运算符的优先级。
- 重载不能改变运算符的结合律。
- 重载不能改变运算符操作数的个数。比如+需要两个操作数,则重载的+也必须要有两个操作数。
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 # ifndef _COMPLEX_H_ 2 # define _COMPLEX_H_ 3 #include <iostream> 4 using namespace std; 5 6 class Complex 7 { 8 public: 9 Complex(double r = 0.0, double i = 0.0); 10 Complex(const Complex &); 11 void Show(); 12 Complex operator + (const Complex &); 13 Complex operator * (const double); 14 Complex & operator ++ (); //front 15 Complex operator ++ (int); //Postposition 16 Complex & operator += (const Complex &); 17 Complex & operator = (const Complex &); 18 friend ostream & operator<<(ostream &, const Complex &); 19 private: 20 double real; 21 double imag; 22 }; 23 24 # endif
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 # include "complex.h" 2 3 Complex::Complex(double r, double i) 4 { 5 real = r; 6 imag = i; 7 } 8 9 Complex::Complex(const Complex & comp) 10 { 11 real = comp.real; 12 imag = comp.imag; 13 } 14 15 void Complex::Show() 16 { 17 if(imag > 0) 18 cout << real << " + " 19 << imag << "i" << endl; 20 else if(imag < 0) 21 cout << real << " - " 22 << -imag << "i" << endl; 23 else 24 cout << real << endl; 25 } 26 27 Complex Complex::operator + (const Complex & comp) 28 { 29 Complex temp; 30 temp.real = real + comp.real; 31 temp.imag = imag + comp.imag; 32 return temp; 33 } 34 35 Complex Complex::operator * (const double numb) 36 { 37 Complex temp; 38 temp.real = real * numb; 39 temp.imag = imag; 40 return temp; 41 } 42 43 Complex & Complex::operator ++ () 44 { 45 real++; 46 imag++; 47 return *this; 48 } 49 50 Complex Complex::operator ++ (int) 51 { 52 Complex temp = *this; 53 ++(*this); 54 return temp; 55 } 56 57 Complex & Complex::operator += (const Complex & comp) 58 { 59 (*this) = (*this) + comp; 60 return *this; 61 } 62 63 Complex & Complex::operator = (const Complex & comp) 64 { 65 real = comp.real; 66 imag = comp.imag; 67 return *this; 68 } 69 70 ostream & operator << (ostream & os, const Complex & comp) 71 { 72 os << comp.real; 73 if(comp.imag > 0) 74 { 75 os << " + " << comp.imag << "i"; 76 return os; 77 } 78 else if(comp.imag < 0) 79 { 80 os << " - " << comp.imag << "i"; 81 return os; 82 } 83 return os; 84 }
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 # include "complex.h" 2 3 int main() 4 { 5 Complex comp1(1.1, 2.2); 6 Complex comp2(comp1); 7 Complex comp3; 8 cout << comp1 << endl 9 << comp2 << endl 10 << comp3 << endl; 11 cout << "--------" << endl; 12 Complex comp4; 13 comp3 = comp1 + comp2; 14 comp4 = comp1 * 2.0; 15 comp3.Show(); 16 comp4.Show(); 17 cout << "--------" << endl; 18 cout << ++comp1 << endl; 19 comp1.Show(); 20 cout << comp2++ << endl; 21 comp2.Show(); 22 cout << "--------" << endl; 23 comp2 += comp1; 24 cout<<comp2<<endl; 25 return 0; 26 }
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1.1 + 2.2i 1.1 + 2.2i 0 -------- 2.2 + 4.4i 2.2 + 2.2i -------- 2.1 + 3.2i 2.1 + 3.2i 1.1 + 2.2i 2.1 + 3.2i -------- 4.2 + 6.4i