免费预览已结束,剩余5页可下载查看
下载本文档
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
第三章 重载1、请完成下列填空题1)在C+中,只能重载为类的成员函数的运算符是_=_、_、_()_、_-_。2)利用成员函数对二元运算符重载,其左操作数为_This_,右操作数为_成员函数参数_。3)单目运算符作为类成员函数重载时_没有_的参数;双目运算符作为_类成员函数_重载时需声明其右操作数,作为_友元函数_重载时需声明全部操作数。4)设a和b是两个整型变量,a+b表示这两个变量的和:设c和d为浮点型变量,c+d也表示这两个变量的和。这里的运算符+具有不同的用途,这是_运算符重载_的例子。5)重载的运算符仍然保持其原来的优先级、_结合性_和_语法结构_。6)C+中不能重载的运算符有_._、_*_、_:_、_?:_和_sizof_。2、编程题1)字符串连接需要两个操作数,即两个要被连接的字符串。请按照以平方式实现operator +操作:string1=string2+string3答案:#include #include class Stringpublic:String(int size=80)length=size;buf=new charlength+1;*buf=0;String(char *s)length=strlen(s);buf=new charlength+1;strcpy(buf,s);String(const String& str)length=strlen(str.buf);buf=new charlength+1;strcpy(buf,str.buf);String()deletebuf;String& operator =(const String& str)length=str.length;strcpy(buf,str.buf);return *this;void Print()coutbufendl;friend String operator +(const String& str1,const String& str2)String temp(strlen(str1.buf)+strlen(str2.buf)+1);strcpy(temp.buf,str1.buf);strcat(temp.buf,str2.buf);return temp;private:char *buf;int length;void main()String str1,str2(Hello,),str3(everyone!);str1=str2+str3;str1.Print();2)给th类:class Three-dpublic:Three_d(int I,int j,int k)x=I;y=j;z=k;.Three_d()x=0;y=0;z=0;Void Get(int &I,int &j,int &k(I=x;j=y;k=z;private:int x,y,z;针对给出的类,重载+、+与一运算符(只重载前缀方式即可)。答案:#include class Three_dpublic:Three_d(int i,int j,int k)x=i;y=j;z=k;Three_d()x=0;y=0;z=0;void Get(int &i,int &j,int &k)i=x;j=y;k=z;void Print()cout(x,y,z)endl;Three_d& operator+()x+;y+;z+;return *this;Three_d& operator-()x-;y-;z-;return *this;friend Three_d operator + (Three_d& t1,Three_d& t2);private:int x,y,z;Three_d operator +(Three_d& t1,Three_d& t2)return Three_d(t1.x+t2.x,t1.y+t2.y,t1.z+t2.z);void main()Three_d obj1(1,2,3),obj2(13,12,11),obj3;+obj1;obj1.Print();-obj2;obj2.Print();obj3=obj1+obj2;obj3.Print();3)开发多项式类Polynomial,多项式的每一项用数组表示,每项包含一个系数和一个指数。例如:2x4的指数为4,系数为2。试开发一个完整的Polynomial类,包括构造函数、析构函数、”get函数和”set”函数,以及下述重载的运算符:重载加法运算符+,将两个多项式相加;重载减法运算符-,将两个多项式相减:重载乘法运算符*,将两个多项式相乘:重载加法赋值运算符+=,减法赋值运算符-=,以及乘法赋值运算符*=。答案:#include #include class Polynomialpublic:Polynomial();Polynomial operator+(const Polynomial&)const;Polynomial operator-(const Polynomial&)const;Polynomial operator*(const Polynomial&);Polynomial& operator+=(const Polynomial&);Polynomial& operator-=(const Polynomial&);Polynomial& operator*=(const Polynomial&);void EnterTerms();void PrintPolynomial( )const;private:int exponents100;int coefficients100;void polynomialCombine(Polynomial&);Polynomial:Polynomial()for(int i=0;i100;i+)coefficientsi=0;exponentsi=0;void Polynomial:PrintPolynomial() constint start;bool zero=false;if(coefficients0)coutcoefficients0;start=1;zero=true;elseif(coefficients1)coutcoefficients1x;/常量不存在,输出指数为1的项if(exponents1!=0)&(exponents1!=1)coutexponents1;zero=true;start=2;for(int x=start;x100;x+)/输出其他各项if(coefficientsx!=0)coutsetiosflags(ios:showpos)coefficientsxresetiosflags(ios:showpos)x;if(exponentsx!=0)&(exponentsx!=1)coutexponentsx;zero=true;if(!zero)/多项式为空cout0;coutendl;Polynomial Polynomial:operator+(const Polynomial& r) constPolynomial temp;bool exponentExists;temp.coefficients0=coefficients0+r.coefficients0;/计算常量之和for(int s=1;(s100)&(r.exponentss!=0);s+)temp.coefficientss=r.coefficientss;temp.exponentss=r.exponentss;for(int x=1;x100;x+)/计算其他各项之和exponentExists=false;for(int t=1;(t100)&(!exponentExists);t+)if(exponentsx=temp.exponentst)temp.coefficientst+=coefficientsx;exponentExists=true;if(!exponentExists)temp.exponentss=exponentsx;temp.coefficientss+=coefficientsx;s+;return temp;Polynomial &Polynomial:operator+=(const Polynomial &r)*this=*this+r;return *this;Polynomial Polynomial:operator-(const Polynomial &r)constPolynomial temp;bool exponentExists;temp.coefficients0=coefficients0-r.coefficients0;for(int s=1;(s100)&(exponentss!=0);s+)temp.coefficientss=coefficientss;temp.exponentss=exponentss;for(int x=1;x100;x+)exponentExists=false;for(int t=1;(t100)&(!exponentExists);t+)if(r.exponentsx=temp.exponentst)temp.coefficientst-=coefficientsx;exponentExists=true;if(!exponentExists)temp.exponentss=r.exponentsx;temp.coefficientss-=r.coefficientsx;s+;return temp;Polynomial &Polynomial:operator-=(const Polynomial& r)*this=*this-r;return *this;Polynomial Polynomial:operator*(const Polynomial& r)Polynomial temp;int s=1;for(int x=0;(x100)&(x=0|coefficientsx!=0);x+)for(int y=0;(y100)&(y=0|r.coefficientsy!=0);y+)if(coefficientsx*r.coefficientsy)if(exponentsx=0)&(r.exponentsy=0)temp.coefficients0+=coefficientsx*r.coefficientsy;elsetemp.coefficientss= coefficientsx*r.coefficientsy;temp.exponentss=exponentsx+r.exponentsy;s+;polynomialCombine(temp);/合并同类项return temp;void Polynomial:polynomialCombine(Polynomial& w)Polynomial temp=w;int exp;for(int x=0;x100;x+)w.coefficientsx=0;w.exponentsx=0;for(x=1;x100;x+)exp=temp.exponentsx;for(int y=x+1;y100;y+)if(exp=temp.exponentsy)temp.coefficientsx+=temp.coefficientsy;temp.exponentsy=0;temp.coefficientsy=0;w=temp;Polynomial &Polynomial:operator*=(const Polynomial& r)*this=*this*r;return *this;void Polynomial:EnterTerms()bool found=false;int numberOfTerms,c,e;coutnumberOfTerms;for(int n=1;n=numberOfTerms;n+)coutc;coute;if(c!=0)if(e=0)coefficients0+=c;continue;for(int term=1;(term100)&(coefficientsterm!=0);term+)if(e=exponentsterm)coefficientsterm+=c;exponentsterm=e;found=true;if(!found)coefficientsterm+=c;exponentsterm=e;void main()Polynomial a,b,c,t;a.EnterTerms();b.EnterTerms();coutendlFirst polynomial is:;a.PrintPolynomial();coutendlSecond polynomial is:;b.PrintPolynomial();coutendlAdding the polynomials yields:;c=a+b;c.PrintPolynomial();coutendl+=the polynomials yields:;t=a;a+=b;a.PrintPolynomial();coutendlSubtracting the
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 中国物流秋招面试题及答案
- 水库快艇租赁合同范本
- 交房前修改合同范本
- 水库租赁养鹅协议合同
- 校服定做加工合同范本
- 楼房加固相邻协议书
- 施工位合同解除协议
- 楼房独家出售协议书
- 乡村振兴示范协议书
- 柱子保洁服务合同范本
- 2025宁夏回族自治区大学生乡村医生专项计划招聘工作人员13人考试笔试模拟试题及答案解析
- 学校食堂满意度测评及管理方案
- 2025安徽清水街道招聘就业专干6人笔试考试参考试题附答案解析
- 2025云南楚雄州元谋县国有资产投资管理有限公司及所属子公司合同制员工招聘13人考试笔试备考试题及答案解析
- 小学语文教师素养大赛知识素养试题
- 2025年辐射安全与防护考试考核题库(附答案)
- 北京市海淀区2025-2026学年高三上学期期中地理试题 含解析
- 施工现场安全事故应急预案
- 2025版疾病控制护理护士培训大纲
- 2025年中级消防设施操作员《理论知识》题库必做200题(含答案)
- 特种设备重大事故隐患判定标准
评论
0/150
提交评论