收藏 分销(赏)

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

上传人:pc****0 文档编号:5961278 上传时间:2024-11-24 格式:DOCX 页数:19 大小:34.02KB
下载 相关 举报
C++运算符重载例程详解.docx_第1页
第1页 / 共19页
C++运算符重载例程详解.docx_第2页
第2页 / 共19页
点击查看更多>>
资源描述
运算符重载就是对一个已有的运算符赋予新的含义,使之实现新功能。 下面将通过简单的几个例程阐叙其用法。 例10.1 通过函数来实现复数相加。 #include <iostream> 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; //实部 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<<″,″<<imag<<″i)″<<endl;} int main( ) {Complex c1(3,4),c2(5,-10),c3; //定义3个复数对象 c3=plex_add(c2); //调用复数相加函数 cout<<″c1=″; c1.display( ); //输出c1的值 cout<<″c2=″; c2.display( ); //输出c2的值 cout<<″c1+c2=″; c3.display( ); //输出c3的值 return 0; } 例10.2 改写例10.1,重载运算符“+”,使之能用于两个复数相加。 #include <iostream> using namespace std; class Complex {public: 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 Complex::operator+(Complex &c2) //定义重载运算符的函数 { Complex c; c.real=real+c2.real; c.imag=imag+c2.imag; return c;} void Complex∷display( ) { cout<<″(″<<real<<″,″<<imag<<″i)″<<endl;} int main( ) { Complex c1(3,4),c2(5,-10),c3; c3=c1+c2; //运算符+用于复数运算 cout<<″c1=″;c1.display( ); cout<<″c2=″;c2.display( ); cout<<″c1+c2=″;c3.display( ); return 0; } 例10.3 将运算符“+”重载为适用于复数加法,重载函数不作为成员函数,而放在类外,作为Complex类的友元函数。 #include <iostream> using namespace std; class Complex {public: 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,Complex &c2) //定义作为友元函数的重载函数 {return Complex(c1.real+c2.real, c1.imag+c2.imag);}//简洁的写法 void Comple::display( ) {cout<<″(″<<real<<″,″<<imag<<″i)″<<endl;} int main( ) {Complex c1(3,4),c2(5,-10),c3; c3=c1+c2; cout<<″c1=″; c1.display( ); cout<<″c2=″; c2.display( ); cout<<″c1+c2 =″; c3.display( ); } 例10.4 定义一个字符串类String,用来存放不定长的字符串,重载运算符“==”,“<”和“>”,用于两个字符串的等于、小于和大于的比较运算。 为了使读者便于理解程序,同时也使读者了解建立程序的步骤,下面分几步来介绍编程过程。 (1) 先建立一个String类: #include <iostream> using namespace std; class String {public: String( ){p=NULL;} //默认构造函数 String(char *str); //构造函数 void display( ); private: char *p; //字符型指针,用于指向字符串 }; String∷String(char *str) //定义构造函数 {p=str;} //使p指向实参字符串 void String∷display( ) //输出p所指向的字符串 {cout<<p;} int main( ) {String string1(″Hello″),string2(″Book″); string1.display( ); cout<<endl; string2.display( ); return 0; } (2) 有了这个基础后,再增加其他必要的内容。现在增加对运算符重载的部分。 为便于编写和调试,先重载一个运算符“>”。程序如下: #include <iostream> #include <string> using namespace std; class String {public: String( ){p=NULL;} String(char *str); friend bool operator>(String &string1,String &string2); //声明运算符函数为友元函数 void display( ); private: char *p; //字符型指针,用于指向字符串 }; String∷String(char *str) {p=str;} void String∷display( ) //输出p所指向的字符串 {cout<<p;} bool operator>(String &string1,String &string2) //定义运算符重载函数 {if(strcmp(string1.p,string2.p)>0) return true; else return false; } int main( ) {String string1(″Hello″),string2(″Book″); cout<<(string1>string2)<<endl; } (3) 扩展到对3个运算符重载。 在String类体中声明3个成员函数: friend bool operator> (String &string1, String &string2); friend bool operator< (String &string1, String &string2); friend bool operator==(String &string1, String& string2); 在类外分别定义3个运算符重载函数: bool operator>(String &string1,String &string2) //对运算符“>”重载 {if(strcmp(string1.p,string2.p)>0) return true; else return false; } bool operator<(String &string1,String &string2) //对运算符“<”重载 {if(strcmp(string1.p,string2.p)<0) return true; else return false; } bool operator==(String &string1,String &string2) //对运算符“==”重载 {if(strcmp(string1.p,string2.p)==0) return true; else return false; } 再修改主函数: int main( ) {String string1(″Hello″),string2(″Book″),string3(″Computer″); cout<<(string1>string2)<<endl; //比较结果应该为true cout<<(string1<string3)<<endl; //比较结果应该为false cout<<(string1==string2)<<endl; //比较结果应该为false return 0; } (4) 再进一步修饰完善,使输出结果更直观。下面给出最后的程序。 #include <iostream> using namespace std; class String {public: 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; }; String∷String(char *str) {p=str;} void String∷display( ) //输出p所指向的字符串 {cout<<p;} bool operator>(String &string1,String &string2) {if(strcmp(string1.p,string2.p)>0) return true; else return false; } bool operator<(String &string1,String &string2) {if(strcmp(string1.p,string2.p)<0) return true; else return false; } bool operator==(String &string1,String &string2) {if(strcmp(string1.p,string2.p)==0) return true; else return false; } void compare(String &string1,String &string2) {if(operator>(string1,string2)==1) {string1.display( );cout<<″>″;string2.display( );} else if(operator<(string1,string2)==1) {string1.display( );cout<<″<″;string2.display( );} else if(operator==(string1,string2)==1) {string1.display( );cout<<″=″;string2.display( );} cout<<endl; } 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秒进一分钟,此时秒又从0开始算。要求输出分和秒的值。 #include <iostream> using namespace std; class Time { public: Time(){minute=0;sec=0;} //默认构造函数 Time(int m,int s):minute(m),sec(s){ } //构造函数重载 Time operator++( ); //声明运算符重载函数 void display(){cout<<minute<<″:″<<sec<<endl;} //定义输出时间函数 private: int minute; int sec; }; Time Time::operator++( ) //定义运算符重载函数 { if(++sec>=60) { sec=0; //满60秒进1分钟 ++minute;} return *this; //返回当前对象值 } int main( ) { Time time1(34,0); for (int i=0;i<61;i++) {++time1; time1.display( );} return 0; } 例10.6 在例10.5程序的基础上增加对后置自增运算符的重载。修改后的程序如下: #include <iostream> using namespace std; class Time {public: Time( ){minute=0;sec=0;} Time(int m,int s):minute(m),sec(s){} Time operator++( ); //声明前置自增运算符“++”重载函数 Time operator++(int); //声明后置自增运算符“++”重载函数 void display( ){cout<<minute<<″:″<<sec<<endl;} private: int minute; int sec; }; Time Time∷operator++( ) //定义前置自增运算符“++”重载函数 {if(++sec>=60) {sec-=60; ++minute;} return *this; //返回自加后的当前对象 } Time Time∷operator++(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++; //将自加前的对象的值赋给time2 cout<<″time1++: ″; time1.display( ); cout<<″ time2 :″; time2.display( ); //输出time2对象的值 } 例10.7 在例10.2的基础上,用重载的“<<”输出复数。 #include <iostream> using namespace std; class Complex {public: Complex( ){real=0;imag=0;} Complex(double r,double i){real=r;imag=i;} Complex operator + (Complex &c2); //运算符“+”重载为成员函数 friend ostream& operator << (ostream&,Complex&); //运算符“<<”重载为友元函数 private: 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.imag<<″i)″<<endl; return output; } int main( ) {Complex c1(2,4),c2(6,10),c3; c3=c1+c2; cout<<c3; return 0; } (在Visual C++ 6.0环境下运行时,需将第一行改为#include <iostream.h>,并删去第2行。) 例10.8 在例10.7的基础上,增加重载流提取运算符“>>”,用“cin>>”输入复数,用“cout<<”输出复数。 #include <iostream> using namespace std; class Complex {public: friend ostream& operator << (ostream&,Complex&); //声明重载运算符“<<” friend istream& operator >> (istream&,Complex&); //声明重载运算符“>>” private: double real; double imag; }; ostream& operator << (ostream& output,Complex& c) //定义重载运算符“<<” {output<<″(″<<c.real<<″+″<<c.imag<<″i)″; return output; } istream& operator >> (istream& input,Complex& c) //定义重载运算符“>>” {cout<<″input real part and imaginary part of complex number:″; input>>c.real>>c.imag; return input; } int main( ) {Complex c1,c2; cin>>c1>>c2; cout<<″c1=″<<c1<<endl; cout<<″c2=″<<c2<<endl; return 0; }
展开阅读全文

开通  VIP会员、SVIP会员  优惠大
下载10份以上建议开通VIP会员
下载20份以上建议开通SVIP会员


开通VIP      成为共赢上传
相似文档                                   自信AI助手自信AI助手

当前位置:首页 > 百科休闲 > 其他

移动网页_全站_页脚广告1

关于我们      便捷服务       自信AI       AI导航        抽奖活动

©2010-2025 宁波自信网络信息技术有限公司  版权所有

客服电话:4009-655-100  投诉/维权电话:18658249818

gongan.png浙公网安备33021202000488号   

icp.png浙ICP备2021020529号-1  |  浙B2-20240490  

关注我们 :微信公众号    抖音    微博    LOFTER 

客服