1、运算符重载就是对一个已有的运算符赋予新的含义,使之实现新功能。下面将通过简单的几个例程阐叙其用法。例10.1 通过函数来实现复数相加。#include using namespace std;class Complex /定义Complex类public:Complex( )real=0;imag=0; /定义构造函数Complex(double r,double i)real=r;imag=i; /构造函数重载Complex complex_add(Complex &c2); /声明复数相加函数void display( ); /声明输出函数 private:double real; /实部
2、double imag; /虚部;Complex Complex:complex_add(Complex &c2)Complex c;c.real=real+c2.real;c.imag=imag+c2.imag;return c; void Complex:display( ) /定义输出函数cout(real,imagi)endl;int main( )Complex c1(3,4),c2(5,-10),c3; /定义3个复数对象c3=plex_add(c2); /调用复数相加函数coutc1=; c1.display( ); /输出c1的值coutc2=; c2.display( );
3、/输出c2的值coutc1+c2=; c3.display( ); /输出c3的值return 0;例10.2 改写例10.1,重载运算符“+”,使之能用于两个复数相加。#include using namespace std;class Complexpublic:Complex( )real=0;imag=0;Complex(double r,double i)real=r;imag=i;Complex operator+(Complex &c2); /声明重载运算符的函数void display( ); private:double real;double imag;Complex Co
4、mplex:operator+(Complex &c2) /定义重载运算符的函数 Complex c;c.real=real+c2.real;c.imag=imag+c2.imag;return c;void Complexdisplay( ) cout(real,imagi)endl;int main( ) Complex c1(3,4),c2(5,-10),c3;c3=c1+c2; /运算符+用于复数运算coutc1=;c1.display( );coutc2=;c2.display( );coutc1+c2=;c3.display( );return 0;例10.3 将运算符“+”重载为
5、适用于复数加法,重载函数不作为成员函数,而放在类外,作为Complex类的友元函数。#include using namespace std;class Complexpublic:Complex( )real=0;imag=0;Complex(double r,double i)real=r;imag=i;friend Complex operator + (Complex &c1,Complex &c2);/重载函数作为友元函数void display( ); private:double real;double imag;Complex operator + (Complex &c1,C
6、omplex &c2) /定义作为友元函数的重载函数return Complex(c1.real+c2.real, c1.imag+c2.imag);/简洁的写法void Comple:display( )cout(real,imagi)endl;int main( )Complex c1(3,4),c2(5,-10),c3;c3=c1+c2;coutc1=; c1.display( );coutc2=; c2.display( );coutc1+c2 =; c3.display( );例10.4 定义一个字符串类String,用来存放不定长的字符串,重载运算符“=”,“”,用于两个字符串的等
7、于、小于和大于的比较运算。为了使读者便于理解程序,同时也使读者了解建立程序的步骤,下面分几步来介绍编程过程。(1) 先建立一个String类: #include using namespace std;class Stringpublic:String( )p=NULL; /默认构造函数String(char *str); /构造函数void display( );private:char *p; /字符型指针,用于指向字符串;StringString(char *str) /定义构造函数p=str; /使p指向实参字符串void Stringdisplay( ) /输出p所指向的字符串cou
8、tp;int main( )String string1(Hello),string2(Book);string1.display( );cout”。程序如下: #include #include using namespace std;class Stringpublic:String( )p=NULL;String(char *str);friend bool operator(String &string1,String &string2);/声明运算符函数为友元函数void display( );private:char *p; /字符型指针,用于指向字符串;StringString(
9、char *str)p=str;void Stringdisplay( ) /输出p所指向的字符串cout(String &string1,String &string2) /定义运算符重载函数if(strcmp(string1.p,string2.p)0)return true;else return false;int main( )String string1(Hello),string2(Book);coutstring2) (String &string1, String &string2);friend bool operator(String &string1,String &s
10、tring2) /对运算符“”重载if(strcmp(string1.p,string2.p)0)return true;elsereturn false;bool operator(String &string1,String &string2) /对运算符“”重载if(strcmp(string1.p,string2.p)0)return true;elsereturn false;bool operator=(String &string1,String &string2) /对运算符“=”重载if(strcmp(string1.p,string2.p)=0)return true;el
11、sereturn false;再修改主函数: int main( )String string1(Hello),string2(Book),string3(Computer);coutstring2)endl; /比较结果应该为truecout(string1string3)endl; /比较结果应该为false cout(string1=string2)endl; /比较结果应该为false return 0;(4) 再进一步修饰完善,使输出结果更直观。下面给出最后的程序。#include using namespace std;class Stringpublic:String( )p=N
12、ULL;String(char *str);friend bool operator(String &string1,String &string2);friend bool operator(String &string1,String &string2);friend bool operator=(String &string1,String &string2);void display( );private:char *p; StringString(char *str)p=str;void Stringdisplay( ) /输出p所指向的字符串cout(String &string1
13、,String &string2)if(strcmp(string1.p,string2.p)0)return true;elsereturn false;bool operator(String &string1,String &string2)if(strcmp(string1.p,string2.p)(string1,string2)=1)string1.display( );cout;string2.display( );elseif(operator(string1,string2)=1)string1.display( );cout;string2.display( );elsei
14、f(operator=(string1,string2)=1)string1.display( );cout=;string2.display( );coutendl;int main( )String string1(Hello),string2(Book),string3(Computer),string4(Hello);compare(string1,string2);compare(string2,string3);compare(string1,string4);return 0;例10.5 有一个Time类,包含数据成员minute(分)和sec(秒),模拟秒表,每次走一秒,满60
15、秒进一分钟,此时秒又从0开始算。要求输出分和秒的值。#include using namespace std;class Timepublic:Time()minute=0;sec=0; /默认构造函数Time(int m,int s):minute(m),sec(s) /构造函数重载Time operator+( ); /声明运算符重载函数void display()coutminute:sec=60)sec=0; /满60秒进1分钟+minute;return *this; /返回当前对象值 int main( )Time time1(34,0);for (int i=0;i61;i+)+
16、time1;time1.display( );return 0;例10.6 在例10.5程序的基础上增加对后置自增运算符的重载。修改后的程序如下:#include using namespace std;class Timepublic:Time( )minute=0;sec=0;Time(int m,int s):minute(m),sec(s)Time operator+( ); /声明前置自增运算符“+”重载函数Time operator+(int); /声明后置自增运算符“+”重载函数void display( )coutminute:sec=60)sec-=60;+minute;re
17、turn *this; /返回自加后的当前对象Time Timeoperator+(int) /定义后置自增运算符“+”重载函数Time temp(*this);sec+;if(sec=60)sec-=60;+minute;return temp; /返回的是自加前的对象int main( )Time time1(34,59),time2;cout time1 : ;time1.display( );+time1;cout+time1: ;time1.display( );time2=time1+; /将自加前的对象的值赋给time2couttime1+: ;time1.display( );
18、cout time2 :;time2.display( ); /输出time2对象的值例10.7 在例10.2的基础上,用重载的“”输出复数。 #include using namespace std; class Complexpublic:Complex( )real=0;imag=0;Complex(double r,double i)real=r;imag=i;Complex operator + (Complex &c2); /运算符“+”重载为成员函数friend ostream& operator (ostream&,Complex&); /运算符“”重载为友元函数private
19、:double real;double imag; Complex Complex:operator + (Complex &c2) /定义运算符“+”重载函数return Complex(real+c2.real,imag+c2.imag);ostream& operator (ostream& output,Complex& c) /定义运算符“”重载函数output(c.real+c.imagi)endl;return output;int main( )Complex c1(2,4),c2(6,10),c3;c3=c1+c2;coutc3;return 0;(在Visual C+ 6.
20、0环境下运行时,需将第一行改为#include ,并删去第2行。)例10.8 在例10.7的基础上,增加重载流提取运算符“”,用“cin”输入复数,用“cout”输出复数。#include using namespace std;class Complexpublic:friend ostream& operator (ostream&,Complex&); /声明重载运算符“ (istream&,Complex&); /声明重载运算符“”private:double real;double imag;ostream& operator (ostream& output,Complex& c) /定义重载运算符“”output(c.real+c.imag (istream& input,Complex& c) /定义重载运算符“”coutc.realc.imag;return input;int main( )Complex c1,c2;cinc1c2; coutc1=c1endl; coutc2=c2endl;return 0;