ImageVerifierCode 换一换
格式:DOC , 页数:16 ,大小:95KB ,
资源ID:6660295      下载积分:10 金币
快捷注册下载
登录下载
邮箱/手机:
温馨提示:
快捷下载时,用户名和密码都是您填写的邮箱或者手机号,方便查询和重复下载(系统自动生成)。 如填写123,账号就是123,密码也是123。
特别说明:
请自助下载,系统不会自动发送文件的哦; 如果您已付费,想二次下载,请登录后访问:我的下载记录
支付方式: 支付宝    微信支付   
验证码:   换一换

开通VIP
 

温馨提示:由于个人手机设置不同,如果发现不能下载,请复制以下地址【https://www.zixin.com.cn/docdown/6660295.html】到电脑端继续下载(重复下载【60天内】不扣币)。

已注册用户请登录:
账号:
密码:
验证码:   换一换
  忘记密码?
三方登录: 微信登录   QQ登录  

开通VIP折扣优惠下载文档

            查看会员权益                  [ 下载后找不到文档?]

填表反馈(24小时):  下载求助     关注领币    退款申请

开具发票请登录PC端进行申请

   平台协调中心        【在线客服】        免费申请共赢上传

权利声明

1、咨信平台为文档C2C交易模式,即用户上传的文档直接被用户下载,收益归上传人(含作者)所有;本站仅是提供信息存储空间和展示预览,仅对用户上传内容的表现方式做保护处理,对上载内容不做任何修改或编辑。所展示的作品文档包括内容和图片全部来源于网络用户和作者上传投稿,我们不确定上传用户享有完全著作权,根据《信息网络传播权保护条例》,如果侵犯了您的版权、权益或隐私,请联系我们,核实后会尽快下架及时删除,并可随时和客服了解处理情况,尊重保护知识产权我们共同努力。
2、文档的总页数、文档格式和文档大小以系统显示为准(内容中显示的页数不一定正确),网站客服只以系统显示的页数、文件格式、文档大小作为仲裁依据,个别因单元格分列造成显示页码不一将协商解决,平台无法对文档的真实性、完整性、权威性、准确性、专业性及其观点立场做任何保证或承诺,下载前须认真查看,确认无误后再购买,务必慎重购买;若有违法违纪将进行移交司法处理,若涉侵权平台将进行基本处罚并下架。
3、本站所有内容均由用户上传,付费前请自行鉴别,如您付费,意味着您已接受本站规则且自行承担风险,本站不进行额外附加服务,虚拟产品一经售出概不退款(未进行购买下载可退充值款),文档一经付费(服务费)、不意味着购买了该文档的版权,仅供个人/单位学习、研究之用,不得用于商业用途,未经授权,严禁复制、发行、汇编、翻译或者网络传播等,侵权必究。
4、如你看到网页展示的文档有www.zixin.com.cn水印,是因预览和防盗链等技术需要对页面进行转换压缩成图而已,我们并不对上传的文档进行任何编辑或修改,文档下载后都不会有水印标识(原文档上传前个别存留的除外),下载后原文更清晰;试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓;PPT和DOC文档可被视为“模板”,允许上传人保留章节、目录结构的情况下删减部份的内容;PDF文档不管是原文档转换或图片扫描而得,本站不作要求视为允许,下载前可先查看【教您几个在下载文档中可以更好的避免被坑】。
5、本文档所展示的图片、画像、字体、音乐的版权可能需版权方额外授权,请谨慎使用;网站提供的党政主题相关内容(国旗、国徽、党徽--等)目的在于配合国家政策宣传,仅限个人学习分享使用,禁止用于任何广告和商用目的。
6、文档遇到问题,请及时联系平台进行协调解决,联系【微信客服】、【QQ客服】,若有其他问题请点击或扫码反馈【服务填表】;文档侵犯商业秘密、侵犯著作权、侵犯人身权等,请点击“【版权申诉】”,意见反馈和侵权处理邮箱:1219186828@qq.com;也可以拔打客服电话:0574-28810668;投诉电话:18658249818。

注意事项

本文(实验五 运算符重载完成.doc)为本站上传会员【pc****0】主动上传,咨信网仅是提供信息存储空间和展示预览,仅对用户上传内容的表现方式做保护处理,对上载内容不做任何修改或编辑。 若此文所含内容侵犯了您的版权或隐私,请立即通知咨信网(发送邮件至1219186828@qq.com、拔打电话4009-655-100或【 微信客服】、【 QQ客服】),核实后会尽快下架及时删除,并可随时和客服了解处理情况,尊重保护知识产权我们共同努力。
温馨提示:如果因为网速或其他原因下载失败请重新下载,重复下载【60天内】不扣币。 服务填表

实验五 运算符重载完成.doc

1、 实验五 运算符重载的应用 班级:B135A2学号: 201322688 姓名: 杨弘 成绩: 一. 实验目的 1、理解运算符重载的作用; 2、掌握运算符重载的两种方法; 3、掌握单目、双目运算符的重载; 二. 使用的设备和仪器 计算机+Windows XP +Visual C++6.0 三. 实验内容及要求 1、定义一个复数类Complex,重载运算符“-”,使之能用于复数与实数的减法运算。参加运算的两个操作数一个是类的对象,一个是实数,顺序任意。例如:i-c,c-i均合法(其中,c为复数类的对象,i为实数)。 减法规则:复数实部与实数相减,复数虚部不

2、变。 2、定义点类Point,重载运算符“+”、“-”、“==”、“!=”、“++”、“--”、“>>”、“<<”,实现两个点的相加、相减、相等、不等、自增、自减、输入和输出运算。 3、定义一个矩阵类Matrix,均为M行N列,通过重载运算符“+”、“-”,“<<”,“>>”,“++”,“--”,“==”,“!=”来实现矩阵的相加、相减、输出、输入、自增、自减以及相等、不等的判断。 4、定义时间类Time,时间的表示采用24小时制。重载运算符“<<”和“>>”实现时间的输出和输入;重载运算符“+”和“-”实现时间推后和提前若干分钟;重载运算符“++”和“--”实现当前时间推后和提前1小时

3、重载“>”、“<”、“==”来判断两个时间之间大于、小于、等于以及不等于的关系。 注意:输入时需对数据合法性进行判断。 5、编写一个程序,处理一个复数与一个double型数相加的运算,结果存放在一个double型的变量d1中,输出d1的值,再以复数形式输出此值。定义复数(Complex)类,在成员函数中包含重载类型转换运算符: Operator double(){ return real;} 6、定义一个教师类和一个学生类,二者有一部分数据成员是相同的,例如num,name,sex。请编程,将一个学生类对象转换为教师类,只将以上3个相同的数据成员移植过去。可以设想为:一位学生大学毕业

4、了,留校担任教师,他原有的部分数据对现在的教师身份来说仍然是有用的,应当保留并成为其教师的数据的一部分。 四. 实验步骤 1.程序代码:/*1、定义一个复数类Complex,重载运算符"-",使之能用于复数与实数的减法运算。参加运算的两个操作数一个是类的对象,一个是实数,顺序任意。 例如:i-c,c-i均合法(其中,c为复数类的对象,i为实数)。 减法规则:复数实部与实数相减,复数虚部不变。*/ #include //#include using std::cin; using std::

5、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 operat

6、or-(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<<"******

7、输入**********"<>real>>imag; } void Complex::Display() { cout<<"*********显示**********"<

8、实数:"; cin>>m; c=c-m; c.Display(); return 0; } 运行结果: 2.程序代码: /*2、定义点类Point,重载运算符"+"、"-"、"=="、"!="、"++"、"--"、">>"、"<<", 实现两个点的相加、相减、相等、不等、自增、自减、输入和输出运算。*/ //#include #include //using std::cin; //用#include加上Using std::cin;usin

9、g // 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 D

10、isplay(); 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 opera

11、tor--(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<<"******输入******"<

12、out<<"请输入点坐标(x,y)"; cin>>x>>y; count++; } void Point::Display() { cout<<"******显示******"<

13、int &b) //重载“-”为成员函数 { Point p; p.x=x-b.x; p.y=y-b.y; return p; } int operator==(Point &a,Point &b) //重载“==”为友员函数 { if(a.x!=b.x) return 0; else if(a.y!=b.y) return 0; else return 1; } int operator!=(Point &a,Point &b) //重载“!=”为友员函数 { if(a.x==b.x&

14、a.y==b.y) return 0; else return 1; } Point operator++(Point &a) //重载“++”为友员函数 { Point p; p.x=++a.x; p.y=++a.y; return p; } Point operator++(Point &a,int) //重载“++”为友员函数 { Point c; c.x=a.x; c.y=a.y; a.x++; a.y++; return c; } Point operator--(P

15、oint &a) //重载“--”为友员函数 { Point p; p.x=-a.x; p.y=--a.y; return p; } Point operator--(Point &a,int) //重载“--”为友员函数 { Point c; c.x=a.x; c.y=a.y; a.x--; a.y--; return c; } istream & operator>>(istream & input,Point & a) //重载流插入运算符 { input>>a

16、x>>a.y; return input; } ostream & operator<<(ostream & output,Point & a) //重载流插入运算符 { output<<"("<

17、 cout<<"Overload'+'----a=a+b:"<>'输入输出:"<>a; cout<<"显示<>b; cout<<"显示<

18、 and '!='"<>","++","--", "==","!="来实现矩阵的相加、相减、输出、输入、自增、自减以及相等、不等的判断。*/ #include using namespace std; const int M=3; const int

19、N=3; class Matrix { private: int X[M][N]; public: friend Matrix operator+(Matrix a[][N],Matrix b[][N]); friend Matrix operator-(Matrix &a,Matrix &b); friend istream & operator>>(istream & input,Matrix &c); friend ostream & operator<<(istream & output,Matrix &c); friend Matrix operato

20、r++(Matrix &c); friend Matrix operator--(Matrix &c); friend int operator==(Matrix &a,Matrix &b); friend int operator!=(Matrix &a,Matrix &b); }; Matrix operator+(Matrix a[][N],Matrix b[][N]) { int i,j; Matrix c[M][N]; for(i=0;i

21、i][j]+b[i][j]; } return c[M][N]; } Matrix operator-(Matrix *a,Matrix *b) { int i,j; Matrix c[M][N]; for(i=0;i>(istream & input,Matrix *c) { int i,j; for(i=0;i<

22、M;i++) for(j=0;j>c[i][j]; } ostream & operator<<(istream & output,Matrix *c) { int i,j; for(i=0;i

23、 } Matrix operator--(Matrix *c) { int i,j; for(i=0;i

24、rix *a,Matrix *b) { int i,j; for(i=0;i>"<>a; cin>>b; cout<<"Display(a):"<

25、 and -:"<

26、b[M][N]"<>"实现时间的输出和输入; 重载运算符"+"和"-"实现时间推后和提前若干分钟;重载运算符"++"和"--"实现当前时间推后和提前1小时; 重载">"、"<"、"=="来判断两个时间之间大于、小于、等于以及不等于的关系。 注意:输入时需对数据合法性进行判断。*/ #include //using namespace st

27、d; class Time //Define class Time { private: 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

28、 Output,Time & t); friend int operator<(Time &a,Time &b); 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,

29、second right. Input>>t.hour>>t.minute>>t.second; while(1) { if(t.hour>24||t.hour<0||t.minute>60||t.minute<0||t.second>60||t.second<0) { cout<<"Wrong!! Please enter again!!"<>t.hour>>t.minute>>t.second; } if(t.hour<24&&t.hour>0&&t.minute<60&&t.minute>0&&t.second

30、<60||t.second>0) break; } return Input; } ostream & operator<<(ostream & Output,Time & t) //Overload " << " { Output<

31、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 " ++ "

32、{ 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)

33、 //Overload " < " { if(a.hour(Time &a,Time &b) //Overload " >

34、 " { if(a.hour>b.hour) return 1; else if(a.hour==b.hour&&a.minute>b.minute) return 1; else if(a.hour==b.hour&&a.minute==b.minute&&a.second>b.second) return 1; else return 0; } int operator==(Time &a,Time &b) //Overload " == " { if(a.hour!=b.hour)

35、 return 0; else if(a.minute!=b.minute) return 0; else if(a.second!=b.second) return 0; else return 1; } int main() { Time t,t0; cout<<"Please enter Time t(hour,minute,second):"; cin>>t; cout<<"*******Display********"<

36、Operator*******"<

37、"; cin>>t0; cout<<"*******Display********"<t0) cout<<"t>t0"<

38、结果存放在一个double型的变量d1中, 输出d1的值,再以复数形式输出此值。定义复数(Complex)类,在成员函数中包含重载类型转换运算符: Operator double(){ return real;}*/ #include //using namespace std; class Complex //Define class Complex { private: double real,imag; public: double Complex::operator+(double a); frien

39、d ostream & operator<<(ostream & output,Complex & a); friend istream & operator>>(istream & input,Complex & a); Complex() //构造函数 { real=10; imag=10; } Complex(double a) //定义转换构造函数 { real=a;imag=0; } }; double Complex::operator+(double a) // 重载类型转换

40、符“+” { double b; b=a+real; return b; } ostream & operator<<(ostream & output,Complex &a) //重载“ <<” { output<>(istream & input,Complex &a) //重载“ >>” { input>>a.real>>a.imag; return input; } int main()

41、 // 主函数 { Complex s; double a,d1; cout<<"Complex: "<>a):"; cin>>a; d1=s+a; cout<<"d1= "<

42、ame,sex。 请编程,将一个学生类对象转换为教师类,只将以上3个相同的数据成员移植过去。可以设想为: 一位学生大学毕业了,留校担任教师,他原有的部分数据对现在的教师身份来说仍然是有用的, 应当保留并成为其教师的数据的一部分。*/ #include #include #include using namespace std; class Teacher; class Student //定义学生类 { private: string num,name; string sex

43、 public: void Input(); void Display(); friend class Teacher; friend Teacher Teacher::operator=(Student & s); }; class Teacher //定义教师类 { private: string number,na; string s; public: void Input(); void Display(); Teacher operator=(Student & s); }; void Stud

44、ent::Input() { cout<<"对学生信息进行输入(学号、姓名、性别(f 或 m)):"; cin>>num>>name>>sex; } void Student::Display() { cout<<"对学生信息进行输出:"<

45、信息进行输入(学号、姓名、性别(f 或 m)):"; cin>>number>>na>>s; } void Teacher::Display() { cout<<"对教师信息进行输出:"<

移动网页_全站_页脚广告1

关于我们      便捷服务       自信AI       AI导航        抽奖活动

©2010-2026 宁波自信网络信息技术有限公司  版权所有

客服电话:0574-28810668  投诉电话:18658249818

gongan.png浙公网安备33021202000488号   

icp.png浙ICP备2021020529号-1  |  浙B2-20240490  

关注我们 :微信公众号    抖音    微博    LOFTER 

客服