




已阅读5页,还剩11页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
实验五 运算符重载的应用班级:B135A2学号: 201322688 姓名: 杨弘 成绩: 一 实验目的1、理解运算符重载的作用;2、掌握运算符重载的两种方法;3、掌握单目、双目运算符的重载;二 使用的设备和仪器计算机+Windows XP +Visual C+6.0三 实验内容及要求1、定义一个复数类Complex,重载运算符“-”,使之能用于复数与实数的减法运算。参加运算的两个操作数一个是类的对象,一个是实数,顺序任意。例如:i-c,c-i均合法(其中,c为复数类的对象,i为实数)。减法规则:复数实部与实数相减,复数虚部不变。2、定义点类Point,重载运算符“”、“”、“”、“!=”、“+”、“-”、“”、“”,实现两个点的相加、相减、相等、不等、自增、自减、输入和输出运算。3、定义一个矩阵类Matrix,均为M行N列,通过重载运算符“+”、“-”,“”,“+”,“-”,“=”,“!=”来实现矩阵的相加、相减、输出、输入、自增、自减以及相等、不等的判断。4、定义时间类Time,时间的表示采用24小时制。重载运算符“”实现时间的输出和输入;重载运算符“+”和“-”实现时间推后和提前若干分钟;重载运算符“+”和“-”实现当前时间推后和提前1小时;重载“”、“”、“=”来判断两个时间之间大于、小于、等于以及不等于的关系。注意:输入时需对数据合法性进行判断。5、编写一个程序,处理一个复数与一个double型数相加的运算,结果存放在一个double型的变量d1中,输出d1的值,再以复数形式输出此值。定义复数(Complex)类,在成员函数中包含重载类型转换运算符:Operator double() return real;6、定义一个教师类和一个学生类,二者有一部分数据成员是相同的,例如num,name,sex。请编程,将一个学生类对象转换为教师类,只将以上3个相同的数据成员移植过去。可以设想为:一位学生大学毕业了,留校担任教师,他原有的部分数据对现在的教师身份来说仍然是有用的,应当保留并成为其教师的数据的一部分。四 实验步骤1.程序代码:/*1、定义一个复数类Complex,重载运算符-,使之能用于复数与实数的减法运算。参加运算的两个操作数一个是类的对象,一个是实数,顺序任意。例如:i-c,c-i均合法(其中,c为复数类的对象,i为实数)。减法规则:复数实部与实数相减,复数虚部不变。*/#include /#includeusing std:cin;using std:cout;using std:endl;/using namespace std;class Complex /定义复数类private:double real,imag;public:Complex()real=0;imag=0;Complex(double a,double b)real=a;imag=b;void Display();void Input();friend Complex operator-(Complex &a,double b);friend Complex operator-(double b,Complex &a);Complex operator-(Complex &a,double b) /定义“-”重载函数return Complex(a.real-=b,a.imag);Complex operator-(double b,Complex &a)return Complex(a.real-=b,a.imag);void Complex:Input()cout*输入*endl;coutrealimag;void Complex:Display()cout*显示*endl; coutreal+imagiendl;int main() /主函数Complex c(1,1),c1;double m;c.Display();coutm; c=c-m;c.Display();return 0;运行结果:2.程序代码:/*2、定义点类Point,重载运算符、!=、+、-、,实现两个点的相加、相减、相等、不等、自增、自减、输入和输出运算。*/#include #include/using std:cin; /用#include加上Using std:cin;using / std:cout;using std:endl;对重载流插入、流提取运算符错误/using std:cout;/using std:endl;/using namespace std;class Point /定义point类private:double x;double y;static int count;public:Point()x=0;y=0;void Input();void Display(); Point operator+(Point &b); Point operator-(Point &b);friend int operator=(Point &a,Point &b);friend int operator!=(Point &a,Point &b);friend Point operator+(Point &a);friend Point operator+(Point &a,int);friend Point operator-(Point &a); friend Point operator-(Point &a,int);friend istream & operator(istream & input,Point & a); friend ostream & operator(ostream & output,Point & a);int Point:count=0; /注意赋值时没有static, int static Point:count=0; wrong!void Point:Input()cout*输入*endl;coutxy;count+;void Point:Display()cout*显示*endl;cout第count个坐标为:(x,y)(istream & input,Point & a) /重载流插入运算符inputa.xa.y;return input;ostream & operator(ostream & output,Point & a) /重载流插入运算符output(a.x,a.y);return output;int main() /主函数Point a,b;a.Input();a.Display();b.Input();b.Display();a=a+b;coutOverload+-a=a+b:aendl;a=a-b;coutOverload-a=a-b:aendl;cout重新用输入输出:endl;couta;cout显示a:endl;coutaendl;coutb;cout显示b:endl;coutbendl;cout*Overload = and !=endl;if(a=b)couta=bendl;else if(a!=b)couta!=bendl;return 0;运行结果:程序代码:/*3、定义一个矩阵类Matrix,均为M行N列,通过重载运算符+、-,,+,-,=,!=来实现矩阵的相加、相减、输出、输入、自增、自减以及相等、不等的判断。*/#includeusing namespace std;const int M=3;const int N=3;class Matrixprivate:int XMN;public:friend Matrix operator+(Matrix aN,Matrix bN);friend Matrix operator-(Matrix &a,Matrix &b);friend istream & operator(istream & input,Matrix &c);friend ostream & operator(istream & output,Matrix &c);friend Matrix operator+(Matrix &c);friend Matrix operator-(Matrix &c);friend int operator=(Matrix &a,Matrix &b);friend int operator!=(Matrix &a,Matrix &b);Matrix operator+(Matrix aN,Matrix bN) int i,j;Matrix cMN;for(i=0;iM;i+)for(j=0;jN;j+) cij=aij+bij;return cMN;Matrix operator-(Matrix *a,Matrix *b) int i,j;Matrix cMN;for(i=0;iM;i+)for(j=0;j(istream & input,Matrix *c)int i,j;for(i=0;iM;i+)for(j=0;jcij;ostream & operator(istream & output,Matrix *c)int i,j; for(i=0;iM;i+)for(j=0;jN;j+)outputcij ;Matrix operator+(Matrix *c)int i,j;for(i=0;iM;i+)for(j=0;jN;j+)+cij;return cMN;Matrix operator-(Matrix *c)int i,j;for(i=0;iM;i+)for(j=0;jN;j+)-cij;return cMN;int operator=(Matrix *a,Matrix *b)int i,j;for(i=0;iM;i+)for(j=0;jN;j+)if(aij!=bij)return 0;return 1;int operator!=(Matrix *a,Matrix *b)int i,j;for(i=0;iM;i+)for(j=0;jN;j+)if(aij!=bij)return 1;return 0;int main()Matrix aMN,bMN,cMN;couta;cinb;coutDisplay(a):a;coutDisplay:(b)b;coutOverload + and -:endl;cMN=aMN+MN;coutDisplay(a):c;cMN=aMN-MN;coutDisplay:(b)c;coutOverload = and !=:endl;if(aMN=bMN)coutaMN=bMNendl;else coutaMN!=bMNendl;if(aMN!=bMN)coutaMN!=bMNendl;else coutaMN=bMNendl;return 0;4. 程序代码:/*4、定义时间类Time,时间的表示采用24小时制。重载运算符实现时间的输出和输入;重载运算符+和-实现时间推后和提前若干分钟;重载运算符+和-实现当前时间推后和提前1小时;重载、=来判断两个时间之间大于、小于、等于以及不等于的关系。注意:输入时需对数据合法性进行判断。*/#include/using namespace std;class Time /Define class Timeprivate:int hour,minute,second;public:Time operator-();Time operator+();Time operator+(int n);Time operator-(int n);friend istream & operator(istream & Input,Time & t);friend ostream & operator(ostream & Output,Time & t);friend int operator(Time &a,Time &b);friend int operator=(Time &a,Time &b);istream & operator(istream & Input,Time & t) /Overload /Make sure hour,minute,second right.Inputt.hourt.minutet.second;while(1) if(t.hour24|t.hour60|t.minute60|t.second0)coutWrong! Please enter again!t.hourt.minutet.second;if(t.hour0&t.minute0&t.second0)break;return Input; ostream & operator(ostream & Output,Time & t) /Overload Outputt.hour:t.minute:t.second;return Output;Time Time:operator+(int n) /Overload + Time t;t.hour=hour; t.minute=minute+n;t.second=second;return t;Time Time:operator-(int n) /Overload - Time t; t.minute=minute-n;t.hour=hour;t.second=second;return t;Time Time:operator+() /Overload + Time t; t.minute=minute;t.second=second;+hour;t.hour=hour;return t;Time Time:operator-() /Overload - Time t;t.minute=minute;t.second=second;-hour;t.hour=hour;return t;int operator(Time &a,Time &b) /Overload if(a.hourb.hour)return 1;else if(a.hour=b.hour&a.minuteb.minute)return 1;else if(a.hour=b.hour&a.minute=b.minute&a.second(Time &a,Time &b) /Overload if(a.hourb.hour)return 1;else if(a.hour=b.hour&a.minuteb.minute)return 1;else if(a.hour=b.hour&a.minute=b.minute&a.secondb.second)return 1;else return 0;int operator=(Time &a,Time &b) /Overload = if(a.hour!=b.hour)return 0;else if(a.minute!=b.minute)return 0;elseif(a.second!=b.second)return 0;else return 1; int main()Time t,t0;coutt;cout*Display*endl;coutTime: tendl;cout*Operator*endl;coutAdd 20 minutes: ;t=t+20;couttendl; coutMinus 10 minutes: ;t=t-10;couttendl;coutAdd 1 hour: ; +t;couttendl;coutMinus 1 hour: ;-t;couttendl;coutt0;cout*Display*endl;coutTime: t0endl;cout*Compare*endl;if(t=t0)coutt=t0t0)coutt0endl;elsecouttt0endl;return 0;运行结果:程序代码:/*5、编写一个程序,处理一个复数与一个double型数相加的运算,结果存放在一个double型的变量d1中,输出d1的值,再以复数形式输出此值。定义复数(Complex)类,在成员函数中包含重载类型转换运算符:Operator double() return real;*/#include/using namespace std;class Complex /Define class Complexprivate:double real,imag;public:double Complex:operator+(double a);friend ostream & operator(istream & input,Complex & a);Complex() /构造函数real=10;imag=10;Complex(double a) /定义转换构造函数real=a;imag=0;double Complex:operator+(double a) / 重载类型转换符“+”double b;b=a+real;return b;ostream & operator(ostream & output,Complex &a) /重载“ ”outputa.real+a.imagi(istream & input,Complex &a) /重载“ ”inputa.reala.imag;return input;int main() / 主函数Complex s;double a,d1;coutComplex: sendl;couta):;cina;d1=s+a;coutd1= d1endl;s=d1;coutAfter s=d1 : sendl;return 0;运行结果:程序代码:/*6、定义一个教师类和一个学生类,二者有一部分数据成员是相同的,例如num,name,sex。请编程,将一个学生类对象转换为教师类,只将以上3个相同的数据成员移植过去。可以设想为:一位学生大学毕业了,留校担任教师,他原有的部分数据对现在的
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 菏泽市重点中学2025年高三第二次调研测试物理试题理试题含解析
- 河南焦作市沁阳市2025届初三年级第一次质量调研生物试题试卷含解析
- 浙江机电职业技术学院《特效化妆工艺》2023-2024学年第一学期期末试卷
- 红色简约商务风季度绩效考核报告
- 电机在医疗放射设备中的应用考核试卷
- 森林公园生态旅游市场品牌建设与竞争力提升考核试卷
- 煤气化中的智能化制造技术发展前景考核试卷
- 工程质量事故分析总复习考核试卷
- 液压技术在物料搬运设备中的重要性考核试卷
- 2025届上海市长宁区高三二模考试数学试卷
- 施工风险技术风险
- (高清版)JGT 225-2020 预应力混凝土用金属波纹管
- 酸枣仁汤的临床应用研究
- 河北省廊坊市安次区2023-2024学年八年级下学期4月期中物理试题
- 前庭神经炎病人的护理
- 招标代理服务 投标方案(技术方案)
- 寺院宣传法治知识讲座
- 《多源图像融合技术及其遥感应用-图像融合技术》课件
- 直播带岗方案
- 网络安全前沿技术与未来趋势研究
- 辽宁省沈阳市铁西区2024届英语三年级第二学期期中调研试题含答案
评论
0/150
提交评论