当前位置:文档之家› C程序设计与应用基础第三章重载习题答案

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

第三章重载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 String{public:String(int size=80){length=size;buf=new char[length+1];*buf='\0';}String(char *s){length=strlen(s);buf=new char[length+1];strcpy(buf,s);}String(const String& str){length=strlen;buf=new char[length+1];strcpy(buf,;}~String(){delete[]buf;}String& operator =(const String& str){length=;strcpy(buf,;return *this;}void Print(){cout<<buf<<endl;}friend String operator +(const String& str1,const String& str2){String temp(strlen+strlen+1);strcpy,;strcat,;return temp;}private:char *buf;int length;};void main(){String str1,str2("Hello,"),str3("everyone!");str1=str2+str3;();}2)给th类:class Three-d{public:Three_d(int I,int j,int k){x=I;y=j;z=k;}.Three_d()){x=0;y=0;z=0;}V oid Get(int &I,int &j,int &k({I=x;j=y;k=z;}private:int x,y,z;};针对给出的类,重载"+"、"++"与"一"运算符(只重载前缀方式即可)。

答案:#include <>class Three_d{public: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+,+,+;}void main(){Three_d obj1(1,2,3),obj2(13,12,11),obj3;++obj1;();--obj2;();obj3=obj1+obj2;();}3)开发多项式类Polynomial,多项式的每一项用数组表示,每项包含一个系数和一个指数。

例如:2x4的指数为4,系数为2。

试开发一个完整的Polynomial类,包括构造函数、析构函数、”get"函数和”set”函数,以及下述重载的运算符:①重载加法运算符+,将两个多项式相加;②重载减法运算符-,将两个多项式相减:③重载乘法运算符*,将两个多项式相乘:④重载加法赋值运算符+=,减法赋值运算符-=,以及乘法赋值运算符*=。

答案:#include <>#include <>class Polynomial{public: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 exponents[100];int coefficients[100];void polynomialCombine(Polynomial&);};Polynomial::Polynomial(){for(int i=0;i<100;i++){coefficients[i]=0;exponents[i]=0;}}void Polynomial::PrintPolynomial() const{int start;bool zero=false;if(coefficients[0]){cout<<coefficients[0];start=1;zero=true;}else{if(coefficients[1]){cout<<coefficients[1]<<'x'; //常量不存在,输出指数为1的项if((exponents[1]!=0)&&(exponents[1]!=1))cout<<'^'<<exponents[1];zero=true;}start=2;}for(int x=start;x<100;x++) //输出其他各项if(coefficients[x]!=0){cout<<setiosflags(ios::showpos)<<coefficients[x]<<resetiosflags(ios::showpos)<<'x';if((exponents[x]!=0)&&(exponents[x]!=1))cout<<'^'<<exponents[x];zero=true;}if(!zero) //多项式为空cout<<'0';cout<<endl;}Polynomial Polynomial::operator+(const Polynomial& r) const{Polynomial temp;bool exponentExists;[0]=coefficients[0]+[0]; //计算常量之和for(int s=1;(s<100)&&[s]!=0);s++){[s]=[s];[s]=[s];}for(int x=1;x<100;x++) //计算其他各项之和{exponentExists=false;for(int t=1;(t<100)&&(!exponentExists);t++)if(exponents[x]==[t]){[t]+=coefficients[x];exponentExists=true;}if(!exponentExists){[s]=exponents[x];[s]+=coefficients[x];s++;}}return temp;}Polynomial &Polynomial::operator+=(const Polynomial &r){*this=*this+r;return *this;}Polynomial Polynomial::operator-(const Polynomial &r)const {Polynomial temp;bool exponentExists;[0]=coefficients[0][0];for(int s=1;(s<100)&&(exponents[s]!=0);s++){[s]=coefficients[s];[s]=exponents[s];}for(int x=1;x<100;x++){exponentExists=false;for(int t=1;(t<100)&&(!exponentExists);t++)if[x]==[t]){[t]-=coefficients[x];exponentExists=true;}if(!exponentExists){[s]=[x];[s]-=[x];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;(x<100)&&(x==0||coefficients[x]!=0);x++) for(int y=0;(y<100)&&(y==0||[y]!=0);y++)if(coefficients[x]*[y])if((exponents[x]==0)&&[y]==0))[0]+=coefficients[x]*[y];else{[s]= coefficients[x]*[y];[s]=exponents[x]+[y];s++;}polynomialCombine(temp); //合并同类项return temp;}void Polynomial::polynomialCombine(Polynomial& w) {Polynomial temp=w;int exp;for(int x=0;x<100;x++){[x]=0;[x]=0;}for(x=1;x<100;x++){exp=[x];for(int y=x+1;y<100;y++)if(exp==[y]){[x]+=[y];[y]=0;[y]=0;}}w=temp;}Polynomial &Polynomial::operator*=(const Polynomial& r) {*this=*this*r;return *this;}void Polynomial::EnterTerms(){bool found=false;int numberOfTerms,c,e;cout<<"Enter number of polynomial terms:";cin>>numberOfTerms;for(int n=1;n<=numberOfTerms;n++){cout<<"Enter coefficient:";cin>>c;cout<<"Enter exponent:";cin>>e;if(c!=0){if(e==0){coefficients[0]+=c;continue;}for(int term=1;(term<100)&&(coefficients[term]!=0);term++)if(e==exponents[term]){coefficients[term]+=c;exponents[term]=e;found=true;}if(!found){coefficients[term]+=c;exponents[term]=e;}}}}void main(){Polynomial a,b,c,t;();();cout<<endl<<"First polynomial is:";();cout<<endl<<"Second polynomial is:";();cout<<endl<<"Adding the polynomials yields:";c=a+b;();cout<<endl<<"+=the polynomials yields:";t=a;a+=b;();cout<<endl<<"Subtracting the polynomials yields:";a=t;();cout<<endl<<"-=the polynomials yields:";a-=b;();cout<<endl<<"Multiplying the polynomials yields:";a=t;c=a*b;();cout<<endl<<"*=the polynomials yields:";a*=b;();cout<<endl;}4)C++在运行期间不会自动检查数组是否越界。

相关主题