运算符重载(C++)
一、运算符重载机制:
一元运算符: @obj => operator @(obj)
二元运算符: obj@obj2 => operator @(obj,obj2)
注意:前置++、--与一元运算符处理方式相同,而后置++、--这样处理:obj++ => operator ++(obj,0)
二、除了.、.*、::、?:、sizeof这5个运算符之外,其他一概都可以重载。
三、普通运算符重载:
1.重载为类的友元函数:
1 #include <iostream> 2 using namespace std; 3 4 class Complex 5 { 6 private: 7 double real; 8 double image; 9 public: 10 Complex(double real=0,double image=0){ this->real=real,this->image=image; } 11 void display(){ cout<<"("<<real<<","<<image<<")"<<endl; } 12 friend Complex operator + (Complex A,Complex B){ return Complex(A.real+B.real,A.image+B.image); }//加法 13 friend Complex operator - (Complex A,Complex B);//减号 14 friend Complex operator - (Complex A);//负号 15 friend Complex operator ++ (Complex& A);//前置++ 16 friend Complex operator ++ (Complex& A,int);//后置++ 17 friend bool operator < (const Complex& A,const Complex& B);// 18 friend bool operator == (const Complex& A,const Complex& B);// 19 }; 20 Complex operator - (Complex A,Complex B) { return Complex(A.real-B.real,A.image-B.image); } 21 Complex operator - (Complex A) { return Complex(-A.real,-A.image); } 22 Complex operator ++(Complex& A) { return Complex(++A.real,A.image); } 23 Complex operator ++(Complex& A,int) { return Complex(A.real++,A.image); } 24 bool operator < (const Complex& A,const Complex& B) { return A.real*A.real+A.image*A.image<B.real*B.real+B.image*B.image; } 25 bool operator == (const Complex& A,const Complex& B) { return A.real==B.real&&A.image==B.image; } 26 27 int main() 28 { 29 Complex A(100.0,200.0),B(-10.0,20.0),C; 30 cout<<"A=";A.display(); 31 cout<<"B=";B.display(); 32 C=A+B; 33 cout<<"C=A+B=";C.display(); 34 C=A-B; 35 cout<<"C=A-B=";C.display(); 36 C=-A+B; 37 cout<<"C=-A+B=";C.display(); 38 C=A++; 39 cout<<"C=A++,C=";C.display(); 40 C=++A; 41 cout<<"C=++A,C=";C.display(); 42 C=A+5; 43 cout<<"C=A+5=";C.display(); 44 return 0; 45 }
2.重载为类的成员函数:
1 #include <iostream> 2 using namespace std; 3 4 class Complex 5 { 6 private: 7 double real; 8 double image; 9 public: 10 Complex(double real=0,double image=0){ this->real=real,this->image=image; } 11 void display(){ cout<<"("<<real<<","<<image<<")"<<endl; } 12 Complex operator + (Complex B);//加法 13 Complex operator - (Complex B);//减号 14 Complex operator - ();//负号 15 Complex operator ++ ();//前置++ 16 Complex operator ++ (int);//后置++ 17 bool operator < (const Complex &B)const;// 18 bool operator ==(const Complex &B)const;// 19 }; 20 Complex Complex::operator +(Complex B) { return Complex(real+B.real,image+B.image); } 21 Complex Complex::operator - (Complex B) { return Complex(real-B.real,image-B.image); } 22 Complex Complex::operator - () { return Complex(-real,-image); } 23 Complex Complex::operator ++() { return Complex(++real,image); } 24 Complex Complex::operator ++(int) { return Complex(real++,image); } 25 bool Complex::operator < (const Complex &B)const { return real*real+image*image<B.real*B.real+B.image*B.image; } 26 bool Complex::operator == (const Complex &B)const {return real==B.real&&image==B.image; } 27 28 int main() 29 { 30 Complex A(100.0,200.0),B(-10.0,20.0),C; 31 cout<<"A=";A.display(); 32 cout<<"B=";B.display(); 33 C=A+B; 34 cout<<"C=A+B=";C.display(); 35 C=A-B; 36 cout<<"C=A-B=";C.display(); 37 C=-A+B; 38 cout<<"C=-A+B=";C.display(); 39 C=A++; 40 cout<<"C=A++,C=";C.display(); 41 C=++A; 42 cout<<"C=++A,C=";C.display(); 43 C=A+5; 44 cout<<"C=A+5=";C.display(); 45 return 0; 46 }