C++运算符重载例程详解.docx

上传人:hs****ma 文档编号:561657936 上传时间:2023-04-07 格式:DOCX 页数:19 大小:34.03KB
返回 下载 相关 举报
C++运算符重载例程详解.docx_第1页
第1页 / 共19页
C++运算符重载例程详解.docx_第2页
第2页 / 共19页
C++运算符重载例程详解.docx_第3页
第3页 / 共19页
C++运算符重载例程详解.docx_第4页
第4页 / 共19页
C++运算符重载例程详解.docx_第5页
第5页 / 共19页
点击查看更多>>
资源描述

《C++运算符重载例程详解.docx》由会员分享,可在线阅读,更多相关《C++运算符重载例程详解.docx(19页珍藏版)》请在金锄头文库上搜索。

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=NULL;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;

展开阅读全文
相关资源
正为您匹配相似的精品文档
相关搜索

最新文档


当前位置:首页 > 生活休闲 > 科普知识

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号