5.3.1类以外的运算符重载 对基本的数据类型,C++提供了许多预定义的运算符,如“+”、“ 等,若有一个复数类 complex: class complex public double real, imag complex(double r=0, double i=o) real=r; imag=i,] 若要把类 complex的两个对象com和com2加在一起,下面的语句 是不能实现的 void maino complex com1(1.1, 2.2), com2 (3.3, 4.4), total; total=com1+com2 ∥错误 错误原因是类 complex的类型不是基本数据类型,而是用户自定义 的数据类型。C++还是无法直接将两个 complex类对象相加
6 5.3.1 类以外的运算符重载 对基本的数据类型,C++提供了许多预定义的运算符,如“+” 、 “- ” 、 “*” 、 “/” 、 “=” 等,若有一个复数类 complex: class complex{ public: double real,imag; complex(double r=0,double i=0) {real=r;imag=i;} }; 若要把类complex的两个对象com1和com2加在一起,下面的语句 是不能实现的: void main() { complex com1(1.1,2.2), com2(3.3,4.4), total; total=com1+com2; //错误 //... } 错误原因是类complex的类型不是基本数据类型,而是用户自定义 的数据类型。C++还是无法直接将两个complex类对象相加
运算符函数 为了表达上的方便,人们希望预定义的内部运算符(如“+”、“-” “米”、“/”等)在特定类的对象上以新的含义进行解释,如希 望能够实现 total=com+com2,这就需要用重载运算符来解决。 C++为运算符重载提供了一种方法,即在进行运算符重载时, 必须写一个运算符函数,其名字为 operator后随一个要重载的运 算符。例如,要重载“+”号,应该写一个名字为 operator+的 函数。 表51运管答函数 函 数 功 能 operator +o 加法 operator-O 减法 operator * 乘法 operator/( 除法 operator <o 小于
7 运算符函数 为了表达上的方便,人们希望预定义的内部运算符(如“+” 、 “-” 、 “*” 、 “/”等)在特定类的对象上以新的含义进行解释,如希 望能够实现total=com1+com2,这就需要用重载运算符来解决。 C++为运算符重载提供了一种方法,即在进行运算符重载时, 必须写一个运算符函数,其名字为operator后随一个要重载的运 算符。例如,要重载“+”号,应该写一个名字为 operator+ 的 函数。 表5.1 运算符函数 函 数 功 能 operator + ( ) 加 法 operator - ( ) 减 法 operator * ( ) 乘 法 operator / ( ) 除 法 operator < ( ) 小 于 ……………… ………………
运算符函数 operator+() 在编译时遇到名为 perator@的运算符函数(@表示所要重教的运 算符),就检查传递给函数的参数的类型。如果编译器在一个运 算符的两边看到自定义的数据类型,就执行用户自己的函数,而 不是常规运算符。 若要将上述类 complex的两个对象相加,只要写一个运算符函数 operator+ complex operator+(complex om1, complex om2) complex temp temp. realom1 real+om2 real; temp. imag=om1 imag+. imag return temp; 我们就能方便的使用语句 total=com1+com2: 将类 complex的两个对象com和com2相加。 也可以使用以下的调用语句,将两个 complex类对象相加: total=operator+(com1, com2); 这两个调用语句是等价的,但显然后者不如前者简明和方便
8 运算符函数operator+() 在编译时遇到名为operator@的运算符函数(@表示所要重载的运 算符),就检查传递给函数的参数的类型。如果编译器在一个运 算符的两边看到自定义的数据类型,就执行用户自己的函数,而 不是常规运算符。 若要将上述类complex的两个对象相加,只要写一个运算符函数 operator+() complex operator+(complex om1,complex om2) { complex temp; temp.real=om1.real+om2.real; temp.imag=om1.imag+om2.imag; return temp; } 我们就能方便的使用语句 total=com1+com2; 将类complex的两个对象com1和com2相加。 也可以使用以下的调用语句,将两个complex类对象相加: total=operator+(com1,com2); 这两个调用语句是等价的,但显然后者不如前者简明和方便
例5.2运算符函数 operator+()将两个 complex类对象相加程序 include <iostream. h> class complex public double real: double imad' Void main( [complex com1(1.1, 2.2),com2 (3.3, 4.4), totall, total2 tota1= operator+(com1,com2);∥调用运算符函数 operator+() 的第一种方式 cout<< real1=<<totall, reak<< < imagi="<<totallimag<<endl; tota|2=com1+com2/调用运算符函数 operator+()的第二种方 cout<< real2=<<total2 reak<< < image="<<total2 imag<<endl; 程序运行结果为: real1=4.4 imagi=6.6 real2=4. 4 imag=6.6
9 例 5.2 运算符函数operator+()将两个complex类对象相加程序 #include <iostream.h> class complex{ public: double real; double imag; complex(double r=0,double i=0) {real=r;imag=i;} }; complex operator+(complex co1,complex co2) { complex temp; temp.real=co1.real+co2.real; temp.imag=co1.imag+co2.imag; return temp; } Void main() {complex com1(1.1,2.2),com2(3.3,4.4), total1, total2; total1=operator+(com1,com2); //调用运算符函数operator+() 的第一种方式 cout<<"real1="<<total1.real<<” "<<"imag1="<<total1.imag<<endl; total2=com1+com2;//调用运算符函数operator+()的第二种方 cout<<"real2="<<total2.real<<” "<<"imag2="<<total2.imag<<endl; } 程序运行结果为: real1=4.4 imag1=6.6 real2=4.4 imag2=6.6
说明: (1)重载运算符与预定义运算符的使用方法完全相同,它不能改 变原有运算符的参数个数(单目或双目),也不能改变原有的优 先级和结合性。 (2)在C++中,大多数系统预定义的运算符都能被重载,例如 〓 /=% = =|=≤>> >〓 <<==l= &&‖ I new delete 也有一些运算符是不能被重载的,如: 预处理符号#和##也不能重载
10 说明: (1)重载运算符与预定义运算符的使用方法完全相同,它不能改 变原有运算符的参数个数(单目或双目),也不能改变原有的优 先级和结合性。 (2)在C++中,大多数系统预定义的运算符都能被重载,例如 + - * / % ^ & | ~ ! = < > += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= && || ++ -- [] () new delete 也有一些运算符是不能被重载的,如: . .* :: ?: 预处理符号#和##也不能重载