1、习题6及其解答第二版第6章 运算符重载习题66.1 选择题1.在下列运算符中,不能重载的是( b )。(a) ! (b) sizeof (c) new (d) delete2.下列关于运算符重载的描述中,( d )是正确的。(a) 可以改变参与运算的操作数个数 (b) 可以改变运算符原来的优先级(c) 可以改变运算符原来的结合性 (d) 不能改变原运算符的语义3.下列函数中,不能重载运算符的函数是( b )。(a) 成员函数 (b) 构造函数 (c) 普通函数 (d) 友员函数4.不能用友员函数重载的是( a )。(a) = (b) = (c) = (d) +5. 下面关于类型转换的描述中,错
2、误的是( b )。(a) 构造函数可以把一种类类型对象转换成另一种类类型对象(b) 构造函数可以把一种类类型对象转换成基本类型对象(c) 类型转换函数可以把类类型对象转换为其他指定类型对象(d) 类型转换函数只能定义为一个类的成员函数,不能定义为类的友员函数6.2 阅读下列程序,写出执行结果 1.#include class T public : T() a = 0; b = 0; c = 0; T( int i , int j , int k ) a = i; b =j ; c = k; void get( int &i , int &j , int &k ) i = a; j = b; k
3、 = c; T operator * ( T obj );private: int a , b , c;T T:operator * ( T obj ) T tempobj; tempobj.a = a * obj.a; tempobj.b = b * obj.b; tempobj.c = c * obj.c; return tempobj;void main() T obj1( 1,2,3 ), obj2( 5,5,5 ), obj3; int a , b , c; obj3 = obj1 * obj2; obj3.get( a, b, c ); cout ( obj1 * obj2 ):
4、t a = a t b = b t c = c t endl; ( obj2 * obj3 ).get( a, b, c ); cout ( obj2 * obj3 ): t a = a t b = b t c = c t endl;【答案】( obj1 * obj2 ): a = 5 b = 10 c = 15 ( obj2 * obj3 ): a = 25 b = 50 c = 752.#include class Vector public: Vector() Vector(int i,int j) x = i ; y = j ; friend Vector operator + ( V
5、ector v1, Vector v2 ) Vector tempVector ; tempVector.x = v1.x + v2.x ; tempVector.y = v1.y + v2.y ; return tempVector ; void display() cout ( x , y ) endl ; private: int x , y ;void main() Vector v1( 1, 2 ), v2( 3, 4 ), v3 ; cout v1 = ; v1.display() ; cout v2 = ; v2.display() ; v3 = v1 + v2 ; cout v
6、3 = v1 + v2 = ; v3.display() ; 【答案】v1 = ( 1, 2 ) v2 = ( 3, 4 ) v3 = v1 + v2 = ( 4, 6 )6.3 思考题 1 一个运算符重载函数被定义为成员函数或友员函数,从定义方式、解释方式和调用方式上有何区别?可能会出现什么问题?请用一个实例说明之。 2 类类型对象之间,类类型和基本类型对象之间用什么函数进行类型转换?归纳进行类型转换的构造函数和类型转换函数的定义形式、调用形式和调用时机。6.4 编程题1分别使用成员函数和友员函数编程序重载运算符“+”,使该运算符能实现两个字符串的连接。【解答】(1)使用成员函数#inclu
7、de class string public: string() *str = 0; string( char *pstr ) strcpy( str,pstr ); char *gets() return str; string operator+( string obj ); private: char str100;string string:operator+( string obj ) strcat( str,obj.str ); return str; /或return *this void main() string obj1( Visual ),obj2( C+ ),obj3;
8、 obj3 = obj1 + obj2; cout obj3.gets() endl;(2)使用友员函数#include #include class string public: string() *str= 0; string( char *pstr ) strcpy( str,pstr ); char *gets() return str; friend string operator+( string obj1,string obj2 ); private: char str100; string operator+( string obj1,string obj2 ) string
9、tempobj; strcat( tempobj.str,obj1.str ); strcat( tempobj.str,obj2.str ); return tempobj;void main() string obj1( Visual ),obj2( C+ ),obj3; obj3 = obj1 + obj2; cout obj3.gets() endl;2定义一个整数计算类Integer,实现短整数 +,-,*,/ 基本算术运算。要求可以进行数据范围检查(-3276832767,或自行设定),数据溢出时显示错误信息并中断程序运行。【解答】#include#includeclass Int
10、eger private: short a; public: Integer (short n=0) a=n; Integer operator +(Integer); Integer operator -(Integer); Integer operator *(Integer); Integer operator /(Integer); Integer operator =(Integer); void display() coutaendl;Integer Integer:operator+(Integer x) Integer temp; if(a+x.a32767) coutData
11、 overflow!endl; abort(); temp.a=a+x.a; return temp;Integer Integer:operator-(Integer x)Integer temp;if(a-x.a32767) coutData overflow!endl; abort();temp.a=a-x.a;return temp;Integer Integer:operator*(Integer x)Integer temp;if(a*x.a32767) coutData overflow!endl; abort();temp.a=a*x.a;return temp;Integer
12、 Integer:operator/(Integer x)Integer temp; if(a/x.a32767) coutData overflow!endl; abort(); temp.a=a/x.a; return temp; Integer Integer:operator=(Integer x) a=x.a; return *this; void main() Integer A(90),B(30),C; coutA=;A.display(); coutB=;B.display(); C=A+B; coutC=A+B=; C.display(); C=A-B; coutC=A-B=
13、; C.display(); C=A*B; coutC=A*B=; C.display(); C=A/B; coutC=A/B=; C.display();3定义一个实数计算类Real,实现单精度浮点数 +,-,*,/ 基本算术运算。要求可以进行数据范围(-3.410383.41038,或自行设定)检查,数据溢出时显示错误信息并中断程序运行。【解答】#include#includeclass Real private: double a; public: Real (double r=0)a=r; Real operator +(Real); Real operator -(Real); Re
14、al operator *(Real); Real operator /(Real); Real operator =(Real); void display() coutaendl;Real Real:operator+(Real x) Real temp; if(a+x.a1.7e308) coutData overflow!endl; abort(); temp.a=a+x.a; return temp;Real Real:operator-(Real x)Real temp;if(a-x.a1.7e308) coutData overflow!endl; abort();temp.a=
15、a-x.a;return temp;Real Real:operator*(Real x)Real temp;if(a*x.a1.7e308) coutData overflow!endl; abort();temp.a=a*x.a;return temp;Real Real:operator/(Real x)Real temp; if(a/x.a1.7e308) coutData overflow!endl; abort(); temp.a=a/x.a; return temp; Real Real:operator=(Real x) a=x.a; return *this; void ma
16、in() Real A(1.1),B(1.2),C; coutA=;A.display(); coutB=;B.display(); C=A+B; coutC=A+B=; C.display(); C=A-B; coutC=A-B=; C.display(); C=A*B; coutC=A*B=; C.display(); C=A/B; cout”、“”做向量的输入/输出操作。注意检测运算的合法性。【解答】#include#includeclass Vectorprivate: double *v; int len;public: Vector(int size); Vector(double
17、 *,int); Vector(); double &operator(int i); Vector & operator =(Vector &); friend Vector operator +(Vector &,Vector &); friend Vector operator -(Vector &,Vector &); friend double operator *(Vector &,Vector &); friend ostream & operator (istream &input,Vector &);Vector:Vector (int size) if(size=21474
18、83647)coutThe size of sizeis overflow!n;abort();v=new double size;for(int i=0;isize;i+) vi=0;len=size;Vector:Vector(double *C,int size) if(size=2147483647)coutThe size ofsizeis overflow!nendl; abort();v=new doublesize;len=size;for(int i=0;i=0 & ilen) return vi; else coutThe size ofiis overflow!n; ab
19、ort();Vector &Vector:operator =(Vector &C) if(len=C.len) for(int i=0;ilen;i+) vi=Ci; return *this; else coutOperator = fail!n; abort();Vector operator +(Vector &A,Vector &B) / 向量相加 int size=A.len ; double *T=new doublesize; if(size=B.len) for(int i=0;isize;i+) Ti=Ai+Bi; return Vector (T,size); else
20、coutOperator + fail!n; abort(); Vector operator -(Vector &A,Vector &B) /向量相减 int size=A.len ; double *T=new doublesize; if(size=B.len) for(int i=0;isize;i+) Ti=Ai-Bi; return Vector (T,size); else coutOperator - fail!n; abort(); double operator *(Vector &A,Vector &B) /向量相乘 int size=A.len ; double s=0
21、; if(size=B.len) for(int i=0;isize;i+) s+=Ai*Bi; return s; else coutOperator * fail!n; abort(); ostream & operator (ostream &output,Vector &A) /输出 output(; for(int i=0;iA.len-1;i+) outputAi,; outputAi(istream &input,Vector &A) /输入 for(int i=0;iAi;return input;void main() int k1,k2,k3;double t;coutk1
22、;Vector A(k1);coutA;coutk2;Vector B(k2);coutB;coutk3;Vector C(k3);coutA=Aendl;coutB=Bendl;C=A+B;coutA+B=A+B=Cendl;C=A-B;coutA-B=A-B=Cendl;t=A*B;coutA*B=A*B=tendl;5定义一个类nauticalmile_kilometer,它包含两个数据成员kilometer(千米)和meter(米)。还包含一个构造函数对数据成员初始化;成员函数print用于输出数据成员kilometer和meter的值;类型转换函数double()实现把千米和米转换为
23、海里(1海里=1.852千米)。编写main函数,测试类nauticalmile_kilometer。【解答】#include const double n = 1.852; / 定义海里与千米和米的转换系数(1海里=1.852千米)class nauticalmile_kilometer public: nauticalmile_kilometer( int km,double m ) kilometer = km; meter = m; void print() coutkilometer=kilometertmeter=meterendl; operator double();private: int kilometer;double meter;nauticalmile_kilometer:operator double() return ( meter / 1000 + double( kilometer ) / n; void main() nauticalmile_kilometer obj( 100,50 ); obj.print(); cout nauticalmile= double( obj ) endl;
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1