C++程序设计与应用基础第三章重载习题答案

上传人:大米 文档编号:508349337 上传时间:2023-09-08 格式:DOC 页数:10 大小:57.50KB
返回 下载 相关 举报
C++程序设计与应用基础第三章重载习题答案_第1页
第1页 / 共10页
C++程序设计与应用基础第三章重载习题答案_第2页
第2页 / 共10页
C++程序设计与应用基础第三章重载习题答案_第3页
第3页 / 共10页
C++程序设计与应用基础第三章重载习题答案_第4页
第4页 / 共10页
C++程序设计与应用基础第三章重载习题答案_第5页
第5页 / 共10页
点击查看更多>>
资源描述

《C++程序设计与应用基础第三章重载习题答案》由会员分享,可在线阅读,更多相关《C++程序设计与应用基础第三章重载习题答案(10页珍藏版)》请在金锄头文库上搜索。

1、第三章 重载1、请完成下列填空题1)在C+中,只能重载为类的成员函数的运算符是_=_、_、_()_、_-_。2)利用成员函数对二元运算符重载,其左操作数为_This_,右操作数为_成员函数参数_。3)单目运算符作为类成员函数重载时_没有_的参数;双目运算符作为_类成员函数_重载时需声明其右操作数,作为_友元函数_重载时需声明全部操作数。4)设a和b是两个整型变量,a+b表示这两个变量的和:设c和d为浮点型变量,c+d也表示这两个变量的和。这里的运算符+具有不同的用途,这是_运算符重载_的例子。5)重载的运算符仍然保持其原来的优先级、_结合性_和_语法结构_。6)C+中不能重载的运算符有_._、

2、_*_、_:_、_?:_和_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=strl

3、en(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);strcp

4、y(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;pri

5、vate: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 Thre

6、e_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,多项式的每一项用数组表示,每项包

7、含一个系数和一个指数。例如:2x4的指数为4,系数为2。试开发一个完整的Polynomial类,包括构造函数、析构函数、”get函数和”set”函数,以及下述重载的运算符:重载加法运算符+,将两个多项式相加;重载减法运算符-,将两个多项式相减:重载乘法运算符*,将两个多项式相乘:重载加法赋值运算符+=,减法赋值运算符-=,以及乘法赋值运算符*=。答案:#include #include class Polynomialpublic:Polynomial();Polynomial operator+(const Polynomial&)const;Polynomial operator-(con

8、st 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 polynomialCombin

9、e(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)coutexp

10、onents1;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) constPolynomi

11、al 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.exp

12、onentst)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+)

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 商业/管理/HR > 营销创新

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