实验4运算符重载参考答案.doc

上传人:人*** 文档编号:543521167 上传时间:2024-01-12 格式:DOC 页数:21 大小:169.51KB
返回 下载 相关 举报
实验4运算符重载参考答案.doc_第1页
第1页 / 共21页
实验4运算符重载参考答案.doc_第2页
第2页 / 共21页
实验4运算符重载参考答案.doc_第3页
第3页 / 共21页
实验4运算符重载参考答案.doc_第4页
第4页 / 共21页
实验4运算符重载参考答案.doc_第5页
第5页 / 共21页
点击查看更多>>
资源描述

《实验4运算符重载参考答案.doc》由会员分享,可在线阅读,更多相关《实验4运算符重载参考答案.doc(21页珍藏版)》请在金锄头文库上搜索。

1、运算符重载1、 实验目的和要求:掌握运算符重载的语法要点,学会运算符重载的编程方法。2、 实验内容(1) 先读程序,写出程序的输出结果,再运行程序验证程序的输出。用友元重载方式重新编写程序。#include using namespace std;class Vector public:Vector(int i = 0,int j = 0) x=i;y=j;Vector operator+=(Vector v) Vector temp; temp.x = x+v.x; temp.y = y+v.y; return temp;Vector operator-=(Vector v) Vector

2、temp( x-v.x, y-v.y); return temp;void display() cout(x,y)endl;private:int x,y;void main() Vector v1(1,2),v2(3,4),v3,v4; v3=v1+=v2; v4=v1-=v2; coutv1=; v1.display();/1,2 coutv2=; v2.display();/3,4 coutv3=; v3.display();/4,6 coutv4=; v4.display();/-2,-2用友元方式实现:#include using namespace std;class Vector

3、public:Vector(int i = 0,int j = 0)x=i;y=j;friend Vector operator+=(Vector &v1,Vector &v2) v1.x+=v2.x;v1.y+=v2.y;return v1;friend Vector operator-=(Vector &v1,Vector &v2) v1.x=v1.x-v2.x;v1.y=v1.y-v2.y;return Vector( v1.x, v1.y);void display() cout(x,y)endl;private:int x,y;void main()Vector v1(1,2),v2

4、(3,4),v3,v4;v3 = v1+=v2;v4 = v1-=v2; coutv1=; v1.display();/1,2coutv2=; v2.display();/3,4coutv3=; v3.display();/4,6coutv4=; v4.display();/1,2(2) 定义一个有理数类Rational,重载算术运算符。写一个完整的程序,测试各种运算符的使用,要求输出化简后的计算结果,注意分母不能为零!测试代码样例: Rational A(2,6),B(1,-2),C;C = -A; C.print();/输出1/3 C = A + B ; C.print(); /输出-1/

5、6 C = C*A/B; C.print(); C = +A ; A.print; C.print(); C = B- ; B.print; C.print(); 源程序:#includeclass Rationalpublic: Rational(int nn=1,int mm=1); void print();void simple(); Rational operator+(Rational & a);/加法friend Rational operator-(Rational & a,Rational & b);/减法friend Rational operator*(Rational

6、& a,Rational & b);/乘法friend Rational operator/(Rational & a,Rational & b);/除法Rational operator-();/取反 Rational & operator + ();/r=+r1Rational operator + (int);/r = r1+Rational & operator - ();/r=-r1Rational operator - (int);/r = r1- operator double();friend ostream& operator(ostream& output, Rationa

7、l& a);bool operatorr2bool Rational:operator(double)a.n/a.m ? true : false;/r1=r2bool operator=(Rational& a,Rational&b )return double(a.n)/a.m = (double)b.n/b.m ? true : false;/coutr;ostream& operator(ostream& output, Rational& a)/output(double)a.n/a.mendl;if(a.m0) outputThe value is -a.n /-a.mendl;e

8、lseoutputThe value is a.n /a.mendl;return output;void Rational:print()simple();if(m0)coutThe value is -n /-mendl; elsecoutThe value is 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;void main()Rational A(

9、2,6),B(1,-2),C;C = -A;C.print();/ -1/3 C = A + B;C.print();/ -1/6C = C*A/B; C.print();/ 1/9C = +A ;A.print();/ 4/3C.print();/ 4/3C = B- ; B.print();/ -3/2C.print();/ -1/2运行结果: (3) 设计集合类(Set), 用运算符重载实现并(+),差(),交()等操作,并重载=和 -= ,实现Set S1,S2; s1 = s2; 和 S1-=S2;集合S1中去掉S2中的元素的操作。源程序代码#include#includestruct Nodeint val;Node *pNext;class IntSet Node * first;/指向链表的头结点public:IntSet()first=

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

当前位置:首页 > 生活休闲 > 社会民生

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