习题6及其解答第二版.docx

上传人:b****8 文档编号:29204558 上传时间:2023-07-21 格式:DOCX 页数:17 大小:18.23KB
下载 相关 举报
习题6及其解答第二版.docx_第1页
第1页 / 共17页
习题6及其解答第二版.docx_第2页
第2页 / 共17页
习题6及其解答第二版.docx_第3页
第3页 / 共17页
习题6及其解答第二版.docx_第4页
第4页 / 共17页
习题6及其解答第二版.docx_第5页
第5页 / 共17页
点击查看更多>>
下载资源
资源描述

习题6及其解答第二版.docx

《习题6及其解答第二版.docx》由会员分享,可在线阅读,更多相关《习题6及其解答第二版.docx(17页珍藏版)》请在冰豆网上搜索。

习题6及其解答第二版.docx

习题6及其解答第二版

第6章运算符重载

习题6

6.1选择题

1.在下列运算符中,不能重载的是(b)。

(a)!

(b)sizeof(c)new(d)delete

2.下列关于运算符重载的描述中,(d)是正确的。

(a)可以改变参与运算的操作数个数(b)可以改变运算符原来的优先级

(c)可以改变运算符原来的结合性(d)不能改变原运算符的语义

3.下列函数中,不能重载运算符的函数是(b)。

(a)成员函数(b)构造函数(c)普通函数(d)友员函数

4.不能用友员函数重载的是(a)。

(a)=(b)==(c)<=(d)++

5.下面关于类型转换的描述中,错误的是(b)。

(a)构造函数可以把一种类类型对象转换成另一种类类型对象

(b)构造函数可以把一种类类型对象转换成基本类型对象

(c)类型转换函数可以把类类型对象转换为其他指定类型对象

(d)类型转换函数只能定义为一个类的成员函数,不能定义为类的友员函数

6.2阅读下列程序,写出执行结果

1.#include

classT

{public:

T(){a=0;b=0;c=0;}

T(inti,intj,intk)

{a=i;b=j;c=k;}

voidget(int&i,int&j,int&k)

{i=a;j=b;k=c;}

Toperator*(Tobj);

private:

inta,b,c;

};

TT:

:

operator*(Tobj)

{Ttempobj;

tempobj.a=a*obj.a;

tempobj.b=b*obj.b;

tempobj.c=c*obj.c;

returntempobj;

}

voidmain()

{Tobj1(1,2,3),obj2(5,5,5),obj3;

inta,b,c;

obj3=obj1*obj2;

obj3.get(a,b,c);

cout<<"(obj1*obj2):

\t"

<<"a="<

(obj2*obj3).get(a,b,c);

cout<<"(obj2*obj3):

\t"

<<"a="<

}

【答案】

(obj1*obj2):

a=5b=10c=15

(obj2*obj3):

a=25b=50c=75

2.#include

classVector

{public:

Vector(){}

Vector(inti,intj)

{x=i;y=j;}

friendVectoroperator+(Vectorv1,Vectorv2)

{VectortempVector;

tempVector.x=v1.x+v2.x;

tempVector.y=v1.y+v2.y;

returntempVector;

}

voiddisplay()

{cout<<"("<

private:

intx,y;

};

voidmain()

{Vectorv1(1,2),v2(3,4),v3;

cout<<"v1=";

v1.display();

cout<<"v2=";

v2.display();

v3=v1+v2;

cout<<"v3=v1+v2=";

v3.display();

}

【答案】

v1=(1,2)

v2=(3,4)

v3=v1+v2=(4,6)

6.3思考题

1.一个运算符重载函数被定义为成员函数或友员函数,从定义方式、解释方式和调用方式上有何区别?

可能会出现什么问题?

请用一个实例说明之。

2.类类型对象之间,类类型和基本类型对象之间用什么函数进行类型转换?

归纳进行类型转换的构造函数和类型转换函数的定义形式、调用形式和调用时机。

6.4编程题

1.分别使用成员函数和友员函数编程序重载运算符“+”,使该运算符能实现两个字符串的连接。

【解答】

(1)使用成员函数

#include

classstring

{public:

string(){*str='\0';}

string(char*pstr)

{strcpy(str,pstr);}

char*gets()

{returnstr;}

stringoperator+(stringobj);

private:

charstr[100];

};

stringstring:

:

operator+(stringobj)

{strcat(str,obj.str);

returnstr;//或return*this

}

voidmain()

{stringobj1("Visual"),obj2("C++"),obj3;

obj3=obj1+obj2;

cout<

}

(2)使用友员函数

#include

#include

classstring

{public:

string(){*str='\0';}

string(char*pstr)

{strcpy(str,pstr);}

char*gets()

{returnstr;}

friendstringoperator+(stringobj1,stringobj2);

private:

charstr[100];

};

stringoperator+(stringobj1,stringobj2)

{stringtempobj;

strcat(tempobj.str,obj1.str);

strcat(tempobj.str,obj2.str);

returntempobj;

}

voidmain()

{stringobj1("Visual"),obj2("C++"),obj3;

obj3=obj1+obj2;

cout<

}

2.定义一个整数计算类Integer,实现短整数+,-,*,/基本算术运算。

要求可以进行数据范围检查(-32768~32767,或自行设定),数据溢出时显示错误信息并中断程序运行。

【解答】

#include

#include

classInteger

{private:

shorta;

public:

Integer(shortn=0){a=n;}

Integeroperator+(Integer);

Integeroperator-(Integer);

Integeroperator*(Integer);

Integeroperator/(Integer);

Integeroperator=(Integer);

voiddisplay()

{cout<

};

IntegerInteger:

:

operator+(Integerx)

{Integertemp;

if(a+x.a<-32768||a+x.a>32767){cout<<"Dataoverflow!

"<

temp.a=a+x.a;

returntemp;

}

IntegerInteger:

:

operator-(Integerx)

{Integertemp;

if(a-x.a<-32768||a-x.a>32767){cout<<"Dataoverflow!

"<

temp.a=a-x.a;

returntemp;

}

IntegerInteger:

:

operator*(Integerx)

{Integertemp;

if(a*x.a<-32768||a*x.a>32767){cout<<"Dataoverflow!

"<

temp.a=a*x.a;

returntemp;

}

IntegerInteger:

:

operator/(Integerx)

{Integertemp;

if(a/x.a<-32768||a/x.a>32767){cout<<"Dataoverflow!

"<

temp.a=a/x.a;

returntemp;

}

IntegerInteger:

:

operator=(Integerx)

{a=x.a;

return*this;}

voidmain()

{IntegerA(90),B(30),C;

cout<<"A=";A.display();

cout<<"B=";B.display();

C=A+B;

cout<<"C=A+B=";C.display();

C=A-B;

cout<<"C=A-B=";C.display();

C=A*B;

cout<<"C=A*B=";C.display();

C=A/B;

cout<<"C=A/B=";C.display();

}

3.定义一个实数计算类Real,实现单精度浮点数+,-,*,/基本算术运算。

要求可以进行数据范围(-3.4×1038~3.4×1038,或自行设定)检查,数据溢出时显示错误信息并中断程序运行。

【解答】

#include

#include

classReal

{private:

doublea;

public:

Real(doubler=0){a=r;}

Realoperator+(Real);

Realoperator-(Real);

Realoperator*(Real);

Realoperator/(Real);

Realoperator=(Real);

voiddisplay()

{cout<

};

RealReal:

:

operator+(Realx)

{Realtemp;

if(a+x.a<-1.7e308||a+x.a>1.7e308){cout<<"Dataoverflow!

"<

temp.a=a+x.a;

returntemp;

}

RealReal:

:

operator-(Realx)

{Realtemp;

if(a-x.a<-1.7e308||a-x.a>1.7e308){cout<<"Dataoverflow!

"<

temp.a=a-x.a;

returntemp;

}

RealReal:

:

operator*(Realx)

{Realtemp;

if(a*x.a<-1.7e308||a*x.a>1.7e308){cout<<"Dataoverflow!

"<

temp.a=a*x.a;

returntemp;

}

RealReal:

:

operator/(Realx)

{Realtemp;

if(a/x.a<-1.7e308||a/x.a>1.7e308){cout<<"Dataoverflow!

"<

temp.a=a/x.a;

returntemp;

}

RealReal:

:

operator=(Realx)

{a=x.a;

return*this;}

voidmain()

{

RealA(1.1),B(1.2),C;

cout<<"A=";A.display();

cout<<"B=";B.display();

C=A+B;

cout<<"C=A+B=";C.display();

C=A-B;

cout<<"C=A-B=";C.display();

C=A*B;

cout<<"C=A*B=";C.display();

C=A/B;

cout<<"C=A/B=";C.display();

}

4.设向量X=(x1,x2,x3)和Y=(y1,y2,y3),则它们之间的加、减和积分别定义为:

X+Y=(x1+y1,x2+y2,x3+y3)

X-Y=(x1-y1,x2-y2,x3-y3)

X*Y=x1*y1+x2*y2+x3*y3

编程序定义向量类vector,重载运算符“+”、“-”、“*”和“=”,实现向量之间的加、减、乘和赋值运算。

用重载运算符“>>”、“<<”做向量的输入/输出操作。

注意检测运算的合法性。

【解答】

#include

#include

classVector

{private:

double*v;

intlen;

public:

Vector(intsize);

Vector(double*,int);

~Vector();

double&operator[](inti);

Vector&operator=(Vector&);

friendVectoroperator+(Vector&,Vector&);

friendVectoroperator-(Vector&,Vector&);

frienddoubleoperator*(Vector&,Vector&);

friendostream&operator<<(ostream&output,Vector&);

friendistream&operator>>(istream&input,Vector&);

};

Vector:

:

Vector(intsize)

{if(size<=0||size>=2147483647)

{cout<<"Thesizeof"<

\n";

abort();}

v=newdouble[size];

for(inti=0;i

len=size;

}

Vector:

:

Vector(double*C,intsize)

{if(size<=0||size>=2147483647)

{cout<<"Thesizeof"<

\n"<

abort();}

v=newdouble[size];

len=size;

for(inti=0;i

v[i]=C[i];

}

Vector:

:

~Vector()

{delete[]v;

v=NULL;len=0;}

double&Vector:

:

operator[](inti)

{if(i>=0&&i

else

{cout<<"Thesizeof"<

\n";

abort();}

}

Vector&Vector:

:

operator=(Vector&C)

{if(len==C.len)

{for(inti=0;i

v[i]=C[i];

return*this;

}

else

{cout<<"Operator=fail!

\n";

abort();}

}

Vectoroperator+(Vector&A,Vector&B)//向量相加

{intsize=A.len;

double*T=newdouble[size];

if(size==B.len)

{for(inti=0;i

T[i]=A[i]+B[i];

returnVector(T,size);

}

else

{cout<<"Operator+fail!

\n";

abort();}

}

Vectoroperator-(Vector&A,Vector&B)//向量相减

{intsize=A.len;

double*T=newdouble[size];

if(size==B.len)

{for(inti=0;i

T[i]=A[i]-B[i];

returnVector(T,size);

}

else

{cout<<"Operator-fail!

\n";

abort();}

}

doubleoperator*(Vector&A,Vector&B)//向量相乘

{intsize=A.len;

doubles=0;

if(size==B.len)

{for(inti=0;i

s+=A[i]*B[i];

returns;

}

else

{cout<<"Operator*fail!

\n";

abort();}

}

ostream&operator<<(ostream&output,Vector&A)//输出

{output<<'(';

for(inti=0;i

output<

output<

returnoutput;

}

istream&operator>>(istream&input,Vector&A)//输入

{for(inti=0;i

input>>A[i];

returninput;

}

voidmain()

{intk1,k2,k3;doublet;

cout<<"InputthelengthofVectorA:

\n";

cin>>k1;

VectorA(k1);

cout<<"InputtheelementsofVectorA:

\n";

cin>>A;

cout<<"InputthelengthofVectorB:

\n";

cin>>k2;

VectorB(k2);

cout<<"InputtheelementsofVectorB:

\n";

cin>>B;

cout<<"InputthelengthofVectorC:

\n";

cin>>k3;

VectorC(k3);

cout<<"A="<

cout<<"B="<

C=A+B;

cout<<"A+B="<

C=A-B;

cout<<"A-B="<

t=A*B;

cout<<"A*B="<

}

5.定义一个类nauticalmile_kilometer,它包含两个数据成员kilometer(千米)和meter(米)。

还包含一个构造函数对数据成员初始化;成员函数print用于输出数据成员kilometer和meter的值;类型转换函数double()实现把千米和米转换为海里(1海里=1.852千米)。

编写main函数,测试类nauticalmile_kilometer。

【解答】

#include

constdoublen=1.852;//定义海里与千米和米的转换系数(1海里=1.852千米)

classnauticalmile_kilometer

{public:

nauticalmile_kilometer(intkm,doublem)

{kilometer=km;meter=m;}

voidprint()

{cout<<"kilometer="<

operatordouble();

private:

intkilometer;

doublemeter;

};

nauticalmile_kilometer:

:

operatordouble()

{return(meter/1000+double(kilometer))/n;}

voidmain()

{nauticalmile_kilometerobj(100,50);

obj.print();

cout<<"nauticalmile="<

}

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 农林牧渔 > 水产渔业

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1