课件C程序设计zw10_第1页
课件C程序设计zw10_第2页
课件C程序设计zw10_第3页
课件C程序设计zw10_第4页
课件C程序设计zw10_第5页
已阅读5页,还剩7页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

C+程序设计(第3版)第10章运算符重载10.1什么是运算符重载编写程序:#includeusing 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 Complexcomplex_add(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; /定义3个复数对象 c3=plex_add(c2); /调用复数相加函数 coutc1=;c1.display(); /输出c1的值coutc2=;c2.display(); /输出c2的值 coutc1+c2=;c3.display(); /输出c3的值 return 0;运行结果:(3,4i)c1=(3,4i)c2=(5,-10i)c1+c2=(8,-6i)10.2运算符重载的方法编写程序: #includeusing 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 Complexoperator+(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(); /输出c1coutc2=;c2.display(); /输出c2 coutc1+c2=;c3.display(); /输出c1+c2return 0;运行结果: c1=(3,4i)c2=(5,-10i)c1+c2=(8,-6i)10.3重载运算符的规则10.4运算符重载函数作为类成员函数和友元函数编写程序: #includeusing 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 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();10.5重载双目运算符编写程序:(1) 先建立一个String类: #includeusing namespace std;class Stringpublic: String()p=NULL;/定义默认构造函数 String(char str); /声明构造函数 void display(); private: char p; /字符型指针,用于指向字符串 ;StringString(char str) /定义构造函数p=str; /使p指向实参字符串void Stringdisplay() /输出p所指向的字符串coutp;int main() String string1(Hello),string2(Book); /定义对象 string1.display(); /调用公用成员函数 coutendl; /换行string2.display(); /调用公用成员函数 return 0;编写程序:#includeusing 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; ; StringString(char str)p=str;void Stringdisplay()/输出p所指向的字符串cout(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)(string1,string2)=1)string1.display();cout;string2.display();elseif(operator(string1,string2)=1)string1.display();cout;string2.display();elseif(operator=(string1,string2)=1)string1.display();cout=;string2.display();coutBookBookComputerHello=Hello10.6重载单目运算符编写程序: #includeusing namespace std;class Time public:Time()minute=0;sec=0; /默认构造函数Time(int m,int s):minute(m),sec(s) /构造函数重载Time operator+(); /声明运算符重载成员函数void display()coutminute:sec=60) sec-=60; /满60秒进1分钟 +minute; return this; /返回当前对象值 int main()Time time1(34,0);for(int i=0;i61;i+) +time1;time1.display();return 0;运行结果: 34:134:2(中间的输出从略)34:5935:035:1(共输出61行)编写程序: #includeusing 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()coutminute:sec=60) sec-=60; +minute; return this; /返回自加后的当前对象Time Timeoperator+(int) /定义后置自增运算符+重载函数 Time temp(this); /建立临时对象tempsec+;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 couttime1+: ; time1.display(); cout time2: ; time2.display(); /输出time2对象的值运行结果: time1:34:59(time1原值)+time1: 35:0 (执行+time1后time1的值)time1+: 35:1 (再执行time1+后time1的值)time2: 35:0 (time2保存的是执行time1+前time1的值)10.7重载流插入运算符“”10.7.1重载流插入运算符“”编写程序:#includeusing 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 Complexoperator+(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; cout”编写程序:#includeusing 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;S运行结果: input real part and imaginary part of complex number:3 6input real part and imaginary part of complex number:4 10c1=(3+6i)c2=(4+10i)10.8有关运算符重载的归纳 10.9不同类型数据间的转换10.9.1标准类型数据间的转换10.9.2用转换构造函数进行不同类型数据的转换10.9.3类型转换函数编写程序: #includeusing namespace std;class Complexpublic: Complex()real=0;imag=0; Complex(double r,double i)real=r;imag=i; operator double()return real; /定义类型转换函数 private: double real; double imag; ;int main() Complex c1(3,4),c2(5,-10),c3; /建立3个Complex类对象double d;d=2.5+c1; /要求将一个double数据与Complex类数据相加coutdendl;return 0;编写程序: (在这个程序中只包含转换构造函数和运算符重载函数)#includeusing namespace std;class Complexpublic: Complex()real=0;imag=0;/默认构造函数,无形参 Complex(double r)real=r;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 Complexdisplay() /定义输出函数cout(real,imagi)endl;int main() Complex c1(3,4),c2(5,-10),c3; /建立3个对象c3=c1+2.5; /复数与double数据相加c3.display();return 0;习题1. 定义一个复数类Complex,重载运算符“+”,使之能用于复数的加法运算。将运算符函数重载为

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论