《c程序设计快速进阶大学教程》教材习题答案-第4章.doc

上传人:marr****208 文档编号:132128291 上传时间:2020-05-12 格式:DOC 页数:23 大小:131KB
返回 下载 相关 举报
《c程序设计快速进阶大学教程》教材习题答案-第4章.doc_第1页
第1页 / 共23页
《c程序设计快速进阶大学教程》教材习题答案-第4章.doc_第2页
第2页 / 共23页
《c程序设计快速进阶大学教程》教材习题答案-第4章.doc_第3页
第3页 / 共23页
《c程序设计快速进阶大学教程》教材习题答案-第4章.doc_第4页
第4页 / 共23页
《c程序设计快速进阶大学教程》教材习题答案-第4章.doc_第5页
第5页 / 共23页
点击查看更多>>
资源描述

《《c程序设计快速进阶大学教程》教材习题答案-第4章.doc》由会员分享,可在线阅读,更多相关《《c程序设计快速进阶大学教程》教材习题答案-第4章.doc(23页珍藏版)》请在金锄头文库上搜索。

1、第四章 运算符号重载习题参考答案1列出C+的所有可重载的运算符,并对每个可重载的运算符,列出它们在用于几个不同的类时的一种或者几种可能的含义,可以尝试在数组类、字符串类以及堆栈类中解释运算符的意义。说明哪些运算符的含义可适用于大量的类,哪些运算符重载的价值极小,哪些运算符具有歧义性。参考答案:C+允许重载大部分运算符,见表4.1。有一些运算符不允许被重载: 如#和#具有特定意义的符号、 成员访问运算符. 、 成员指针访问运算符.* 、 作用域解析符,长度运算符sizeof 和 条件运算符? : 。重载运算符数组类字符串类矩阵类+数组的合并字符串连接两个矩阵相加比较数组,如比较元素个数判断字符串

2、大小比较矩阵数组赋值操作字符串赋值操作矩阵赋值操作输出数组输出字符串输出矩阵 访问数组元素访问字符串中字符对于大部分与数学相关的类,重载算术运算符与关系运算符。对于大部分类可以重载运算符,对于设计内存空间申请与释放的类需要重载赋值以及复合赋值运算符。2定义描述平面点类Point,用减法计算两个点的距离,分别用成员函数与友元函数实现,对比两种实现方式的区别。参考程序:#include #include using namespace std;class Pointpublic:Point( int a= 0, int b = 0 ):x(a),y(b) void setPoint( int a,

3、 int b ) x = a ; y = b ; double operator-( const Point& p); /成员函数包含this指针,只需1个参数friend double operator-(Point& p1, Point& p2); / 友元函数,需要传递2个参数friend ostream& operator( ostream& , Point& );private:int x, y;/ Point类的数据成员;double Point:operator-(const Point& p)double d = (x-p.x)*(x-p.x)+(y-p.y)*(y-p.y);

4、 return sqrt(d);double operator-(Point& p1, Point& p2)double d = (p1.x-p2.x)*(p1.x-p2.x)+(p1.y-p2.y)*(p1.y-p2.y); return sqrt(d);ostream& operator( ostream& out, Point& p )out p.x , p.y ”、“ =”、“、“=”和“!”,使之能够比较两个有理数。(3)重载运算符“”,使其能以规范的方式输出分数,如1/2,-1/3,分母不能为0。参考程序:#include #include /using namespace std

5、;/ 有理数类class Rationalpublic:Rational(int nn=1,int mm=1); void print() const; Rational operator+(Rational & A);/Rational add(Rational & A);Rational operator-();Rational operator*(Rational & A); Rational operator/(Rational & A);bool operator(Rational &);friend Rational operator-(Rational & a,Rational

6、& b);friend ostream& operator(ostream& output, Rational& a);friend bool operator=( Rational& a,Rational& b);friend bool operator=(Rational& ,Rational& );private:void simple(); / 化简int m; / 分母int n; / 分子;Rational: Rational(int nn,int mm )if(m=0)couterror!endl;return;m = mm;n=nn;simple();void Rational

7、:print() constif(m0)cout -n /-mendl; elsecout n /mendl;void Rational:simple()int a = m ,b = n, r = 0; if(mn)a = n;b = m;r = a % b ;while ( r != 0 )a = b ;b = r ;r = a % b ;n /= b;m /= b;/ R = A + BRational Rational:operator+(Rational & A)Rational C;C.n = n*A.m+m*A.n;C.m = m*A.m;C.simple();return C;/

8、 R = A-BRational operator-(Rational & A,Rational & B)Rational C;C.n = A.n*B.m-A.m*B.n;C.m = A.m*B.m;C.simple();return C;Rational Rational:operator*(Rational & A)Rational C;C.n = n*A.n;C.m = m*A.m;C.simple();return C;Rational Rational:operator/(Rational & A)Rational C;C.n = n*A.m;C.m = m*A.n;C.simple

9、();return C;/coutA;ostream& operator(ostream& output, Rational& a)if(a.m0)output -a.n /-a.mendl;else if(a.m=1)output a.n endl;elseoutput a.n /a.mr2bool Rational:operator(Rational& a)return double(n)/m (double)a.n/a.m ? true : false;bool operator(double)B.n/B.m ? true : false;bool operator=(Rational

10、& A,Rational & B)return double(A.n)/A.m=double(B.n)/B.m?true:false;int main()Rational A(3,6),B(1,-2),C,D;coutABC;C = A + B ;D = A * B ;coutC = A+B =Cendl; / cout C+A+B endl; / ?coutD = A*B =D B)coutBendl;elsecoutA=Bendl;C = -A;coutC = -A = Cendl;if(A=C)coutA=Cendl;elsecoutA!=Cendl;return 0;4定义描述时间的t

11、ime类,包括数据成员小时hour、分钟minute和秒second:(1)重载运算符“+”与“-”,能够实现时间对象与整数秒的加减操作。(2)重载运算符“”输出时间对象,能够按照“小时:分钟:秒”的方式显示时间。(3)重载运算符“+”与“-”,要求能够实现时间的合理自增自减功能(秒数的增减)。参考程序:# include class Timepublic:Time(int h,int m,int s):hour(s),minute(m),second(s) Time(Time &t);Time operator+(int num);Time operator-(int num);friend

12、 ostream& operator(ostream& output, Time& t);Time & operator + ();Time operator +(int);Time & operator - ();Time operator -(int);void adjust();private:int hour,minute,second;Time:Time(Time &t)hour=t.hour;minute=t.minute;second=t.second;Time Time:operator+(int num)Time temp(*this);temp.second+=num;temp.ad

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

当前位置:首页 > 高等教育 > 其它相关文档

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