C程序设计与应用基础第三章重载习题答案.docx
《C程序设计与应用基础第三章重载习题答案.docx》由会员分享,可在线阅读,更多相关《C程序设计与应用基础第三章重载习题答案.docx(16页珍藏版)》请在冰豆网上搜索。
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<>
classString
{
public:
String(intsize=80)
{
length=size;
buf=newchar[length+1];
*buf='\0';
}
String(char*s)
{
length=strlen(s);
buf=newchar[length+1];
strcpy(buf,s);
}
String(constString&str)
{
length=strlen;
buf=newchar[length+1];
strcpy(buf,;
}
~String(){delete[]buf;}
String&operator=(constString&str)
{
length=;
strcpy(buf,;
return*this;
}
voidPrint(){cout<friendStringoperator+(constString&str1,constString&str2)
{
Stringtemp(strlen+strlen+1);
strcpy,;
strcat,;
returntemp;
}
private:
char*buf;
intlength;
};
voidmain()
{
Stringstr1,str2("Hello,"),str3("everyone!
");
str1=str2+str3;
();
}
2)给th类:
classThree-d
{
public:
Three_d(intI,intj,intk){x=I;y=j;z=k;}.
Three_d()){x=0;y=0;z=0;}
VoidGet(int&I,int&j,int&k({I=x;j=y;k=z;}
private:
intx,y,z;
};
针对给出的类,重载"+"、"++"与"一"运算符(只重载前缀方式即可)。
答案:
#include<>
classThree_d
{
public:
Three_d(inti,intj,intk){x=i;y=j;z=k;}
Three_d(){x=0;y=0;z=0;}
voidGet(int&i,int&j,int&k){i=x;j=y;k=z;}
voidPrint(){cout<<'('<Three_d&operator++()
{
x++;y++;z++;
return*this;
}
Three_d&operator--()
{
x--;y--;z--;
return*this;
}
friendThree_doperator+(Three_d&t1,Three_d&t2);
private:
intx,y,z;
};
Three_doperator+(Three_d&t1,Three_d&t2)
{
returnThree_d+,+,+;
}
voidmain()
{
Three_dobj1(1,2,3),obj2(13,12,11),obj3;
++obj1;
();
--obj2;
();
obj3=obj1+obj2;
();
}
3)开发多项式类Polynomial,多项式的每一项用数组表示,每项包含一个系数和一个指数。
例如:
2x4的指数为4,系数为2。
试开发一个完整的Polynomial类,包括构造函数、析构函数、”get"函数和”set”函数,以及下述重载的运算符:
①重载加法运算符+,将两个多项式相加;
②重载减法运算符-,将两个多项式相减:
③重载乘法运算符*,将两个多项式相乘:
④重载加法赋值运算符+=,减法赋值运算符-=,以及乘法赋值运算符*=。
答案:
#include<>
#include<>
classPolynomial
{
public:
Polynomial();
Polynomialoperator+(constPolynomial&)const;
Polynomialoperator-(constPolynomial&)const;
Polynomialoperator*(constPolynomial&);
Polynomial&operator+=(constPolynomial&);
Polynomial&operator-=(constPolynomial&);
Polynomial&operator*=(constPolynomial&);
voidEnterTerms();
voidPrintPolynomial()const;
private:
intexponents[100];
intcoefficients[100];
voidpolynomialCombine(Polynomial&);
};
Polynomial:
:
Polynomial()
{
for(inti=0;i<100;i++)
{
coefficients[i]=0;
exponents[i]=0;
}
}
voidPolynomial:
:
PrintPolynomial()const
{
intstart;
boolzero=false;
if(coefficients[0])
{
cout<start=1;
zero=true;
}
else
{
if(coefficients[1])
{
cout<if((exponents[1]!
=0)&&(exponents[1]!
=1))
cout<<'^'<zero=true;
}
start=2;
}
for(intx=start;x<100;x++)//输出其他各项
if(coefficients[x]!
=0)
{
cout<:
showpos)<<:
showpos)<<'x';
if((exponents[x]!
=0)&&(exponents[x]!
=1))
cout<<'^'<zero=true;
}
if(!
zero)//多项式为空
cout<<'0';
cout<}
PolynomialPolynomial:
:
operator+(constPolynomial&r)const
{
Polynomialtemp;
boolexponentExists;
[0]=coefficients[0]+[0];//计算常量之和
for(ints=1;(s<100)&&[s]!
=0);s++)
{
[s]=[s];
[s]=[s];
}
for(intx=1;x<100;x++)//计算其他各项之和
{
exponentExists=false;
for(intt=1;(t<100)&&(!
exponentExists);t++)
if(exponents[x]==[t])
{
[t]+=coefficients[x];
exponentExists=true;
}
if(!
exponentExists)
{
[s]=exponents[x];
[s]+=coefficients[x];
s++;
}
}
returntemp;
}
Polynomial&Polynomial:
:
operator+=(constPolynomial&r)
{
*this=*this+r;
return*this;
}
PolynomialPolynomial:
:
operator-(constPolynomial&r)const
{
Polynomialtemp;
boolexponentExists;
[0]=coefficients[0][0];
for(ints=1;(s<100)&&(exponents[s]!
=0);s++)
{
[s]=coefficients[s];
[s]=exponents[s];
}
for(intx=1;x<100;x++)
{
exponentExists=false;
for(intt=1;(t<100)&&(!
exponentExists);t++)
if[x]==[t])
{
[t]-=coefficients[x];
exponentExists=true;
}
if(!
exponentExists)
{
[s]=[x];
[s]-=[x];
s++;
}
}
returntemp;
}
Polynomial&Polynomial:
:
operator-=(constPolynomial&r)
{
*this=*this-r;
return*this;
}
PolynomialPolynomial:
:
operator*(constPolynomial&r)
{
Polynomialtemp;
ints=1;
for(intx=0;(x<100)&&(x==0||coefficients[x]!
=0);x++)
for(inty=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);//合并同类项
returntemp;
}
voidPolynomial:
:
polynomialCombine(Polynomial&w)
{
Polynomialtemp=w;
intexp;
for(intx=0;x<100;x++)
{
[x]=0;
[x]=0;
}
for(x=1;x<100;x++)
{
exp=[x];
for(inty=x+1;y<100;y++)
if(exp==[y])
{
[x]+=[y];
[y]=0;
[y]=0;
}
}
w=temp;
}
Polynomial&Polynomial:
:
operator*=(constPolynomial&r)
{
*this=*this*r;
return*this;
}
voidPolynomial:
:
EnterTerms()
{
boolfound=false;
intnumberOfTerms,c,e;
cout<<"Enternumberofpolynomialterms:
";
cin>>numberOfTerms;
for(intn=1;n<=numberOfTerms;n++)
{
cout<<"Entercoefficient:
";
cin>>c;
cout<<"Enterexponent:
";
cin>>e;
if(c!
=0)
{
if(e==0)
{
coefficients[0]+=c;
continue;
}
for(intterm=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;
}
}
}
}
voidmain()
{
Polynomiala,b,c,t;
();
();
cout<";
();
cout<";
();
cout<";
c=a+b;
();
cout<";
t=a;
a+=b;
();
cout<";
a=t;
c=a-b;
();
cout<";
a-=b;
();
cout<";
a=t;
c=a*b;
();
cout<";
a*=b;
();
cout<}
4)C++在运行期间不会自动检查数组是否越界。
设计一个类用来检查数组是否越界。
答案:
#include<>
#include<>
classTest
{
public:
Test(char*s)
{
str=newchar[strlen(s)+1];
strcpy(str,s);
len=strlen(s);
}
charoperator[](intn)
{
if(n>len-1)
{
cout<<"数组下标越界"<return'';
}
else
return*(str+n);
}
voidPrint(){cout<private:
intlen;
char*str;
};
voidmain()
{
Testarray("GoodMorning");
();
cout<<"Location0:
"<cout<<"Location20:
"<}