实验五-运算符重载--完成_第1页
实验五-运算符重载--完成_第2页
实验五-运算符重载--完成_第3页
实验五-运算符重载--完成_第4页
实验五-运算符重载--完成_第5页
已阅读5页,还剩11页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

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

2、=”、“+”、“-”、“>>”、“<<”,实现两个点的相加、相减、相等、不等、自增、自减、输入和输出运算。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<iostream> /#include<iostream.h>using std:cin;using std:cout;using std:endl;/using namespace

5、 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) /定义“-”重载函

6、数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;cout<<"请输入复数的虚部和实部:"cin>>real>>imag;void Complex:Display()cout<<"*显示*"<<endl; co

7、ut<<real<<"+"<<imag<<"i"<<endl;int main() /主函数Complex c(1,1),c1;double m;c.Display();cout<<"请输入需要与复数相减的实数:"cin>>m; c=c-m;c.Display();return 0;运行结果:2.程序代码:/*2、定义点类Point,重载运算符""、""、""、"!="、&

8、quot;+"、"-"、">>"、"<<",实现两个点的相加、相减、相等、不等、自增、自减、输入和输出运算。*/#include<iostream> #include<iostream.h>/using std:cin; /用#include<iostream>加上Using std:cin;using / std:cout;using std:endl;对重载流插入、流提取运算符错误/using std:cout;/using std:endl;/using na

9、mespace 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 op

10、erator+(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:

11、count=0; /注意赋值时没有static," int static Point:count=0; " wrong!void Point:Input()cout<<"*输入*"<<endl;cout<<"请输入点坐标(x,y)"cin>>x>>y;count+;void Point:Display()cout<<"*显示*"<<endl;cout<<"第"<<count<<

12、"个坐标为:"<<"("<<x<<","<<y<<")"<<endl;Point Point:operator+(Point &b) /重载“+”为成员函数Point p;p.x=x+b.x;p.y=y+b.y;return p;Point Point:operator-(Point &b) /重载“-”为成员函数Point p;p.x=x-b.x;p.y=y-b.y;return p;int operator=(Point &

13、amp;a,Point &b) /重载“=”为友员函数if(a.x!=b.x)return 0;else if(a.y!=b.y)return 0;elsereturn 1;int operator!=(Point &a,Point &b) /重载“!=”为友员函数if(a.x=b.x&&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

14、,int) /重载“+”为友员函数Point c;c.x=a.x;c.y=a.y;a.x+;a.y+;return c; 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; istream & operator>>(istream & input,Point & a) /重载流插入运算

15、符input>>a.x>>a.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;co

16、ut<<"Overload'+'-a=a+b:"<<a<<endl;a=a-b;cout<<"Overload'-'-a=a-b:"<<a<<endl;cout<<"重新用'<<','>>'输入输出:"<<endl;cout<<"输入a的坐标:"cin>>a;cout<<"显示<&l

17、t;a:"<<endl;cout<<a<<endl;cout<<"输入b的坐标:"cin>>b;cout<<"显示<<b:"<<endl;cout<<b<<endl;cout<<"*Overload '=' and '!='"<<endl;if(a=b)cout<<"a=b"<<endl;else if(a!

18、=b)cout<<"a!=b"<<endl;return 0;运行结果:程序代码:/*3、定义一个矩阵类Matrix,均为M行N列,通过重载运算符"+"、"-","<<",">>","+","-","=","!="来实现矩阵的相加、相减、输出、输入、自增、自减以及相等、不等的判断。*/#include<iostream>using namespace std

19、;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

20、 &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;i<M;i+)for(j=0;j<N;j+) cij=aij+bij;return

21、 cMN;Matrix operator-(Matrix *a,Matrix *b) int i,j;Matrix cMN;for(i=0;i<M;i+)for(j=0;j<N;j+) cij=aij-bij;return cMN;istream & operator>>(istream & input,Matrix *c)int i,j;for(i=0;i<M;i+)for(j=0;j<N;j+)input>>cij;ostream & operator<<(istream & output,Matr

22、ix *c)int i,j; for(i=0;i<M;i+)for(j=0;j<N;j+)output<<cij<<" "Matrix operator+(Matrix *c)int i,j;for(i=0;i<M;i+)for(j=0;j<N;j+)+cij;return cMN;Matrix operator-(Matrix *c)int i,j;for(i=0;i<M;i+)for(j=0;j<N;j+)-cij;return cMN;int operator=(Matrix *a,Matrix *b)int

23、 i,j;for(i=0;i<M;i+)for(j=0;j<N;j+)if(aij!=bij)return 0;return 1;int operator!=(Matrix *a,Matrix *b)int i,j;for(i=0;i<M;i+)for(j=0;j<N;j+)if(aij!=bij)return 1;return 0;int main()Matrix aMN,bMN,cMN;cout<<"<<,>>"<<endl;cin>>a;cin>>b;cout<<

24、;"Display(a):"<<a;cout<<"Display:(b)"<<b;cout<<"Overload + and -:"<<endl;cMN=aMN+MN;cout<<"Display(a):"<<c;cMN=aMN-MN;cout<<"Display:(b)"<<c;cout<<"Overload = and !=:"<<endl;

25、if(aMN=bMN)cout<<"aMN=bMN"<<endl;else cout<<"aMN!=bMN"<<endl;if(aMN!=bMN)cout<<"aMN!=bMN"<<endl;else cout<<"aMN=bMN"<<endl;return 0;4. 程序代码:/*4、定义时间类Time,时间的表示采用24小时制。重载运算符"<<"和">>"

26、;实现时间的输出和输入;重载运算符"+"和"-"实现时间推后和提前若干分钟;重载运算符"+"和"-"实现当前时间推后和提前1小时;重载">"、"<"、"="来判断两个时间之间大于、小于、等于以及不等于的关系。注意:输入时需对数据合法性进行判断。*/#include<iostream.h>/using namespace std;class Time /Define class Timeprivate:int hour,minute,

27、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 ope

28、rator>(Time &a,Time &b);friend int operator=(Time &a,Time &b);istream & operator>>(istream & Input,Time & t) /Overload " >> " /Make sure hour,minute,second right.Input>>t.hour>>t.minute>>t.second;while(1) if(t.hour>24|t.hour&l

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

30、break;return Input; ostream & operator<<(ostream & Output,Time & t) /Overload " << " Output<<t.hour<<":"<<t.minute<<":"<<t.second;return Output;Time Time:operator+(int n) /Overload " + " Time t;t.hour=hour;

31、 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-() /Over

32、load " - "Time t;t.minute=minute;t.second=second;-hour;t.hour=hour;return t;int operator<(Time &a,Time &b) /Overload " < "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&&

33、amp;a.second<b.second)return 1;else return 0;int operator>(Time &a,Time &b) /Overload " > "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

34、 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;cout<<"Please enter Time t(hour,minute,second):"cin>>t;cout<<&quo

35、t;*Display*"<<endl;cout<<"Time: "<<t<<endl;cout<<"*Operator*"<<endl;cout<<"Add 20 minutes: "t=t+20;cout<<t<<endl; cout<<"Minus 10 minutes: "t=t-10;cout<<t<<endl;cout<<"Add

36、1 hour: " +t;cout<<t<<endl;cout<<"Minus 1 hour: "-t;cout<<t<<endl;cout<<"Please enter Time t0(hour,minute,second):"cin>>t0;cout<<"*Display*"<<endl;cout<<"Time: "<<t0<<endl;cout<<

37、;"*Compare*"<<endl;if(t=t0)cout<<"t=t0"<<endl;elseif(t>t0)cout<<"t>t0"<<endl;elsecout<<"t<t0"<<endl;return 0;运行结果:程序代码:/*5、编写一个程序,处理一个复数与一个double型数相加的运算,结果存放在一个double型的变量d1中,输出d1的值,再以复数形式输出此值。定义复数(Complex)类,在成

38、员函数中包含重载类型转换运算符:Operator double() return real;*/#include<iostream.h>/using namespace std;class Complex /Define class Complexprivate:double real,imag;public:double Complex:operator+(double a);friend ostream & operator<<(ostream & output,Complex & a);friend istream & operat

39、or>>(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) /重载“ <<”output<<a.real<

40、<"+"<<a.imag<<"i"<<" "return output;istream & operator>>(istream & input,Complex &a) /重载“ >>”input>>a.real>>a.imag;return input;int main() / 主函数Complex s;double a,d1;cout<<"Complex: "<<s<&

41、lt;endl;cout<<"Please enter a double number(>>a):"cin>>a;d1=s+a;cout<<"d1= "<<d1<<endl;s=d1;cout<<"After s=d1 : "<<s<<endl;return 0;运行结果:程序代码:/*6、定义一个教师类和一个学生类,二者有一部分数据成员是相同的,例如num,name,sex。请编程,将一个学生类对象转换为教师类,只将以上3个相同的数据成员移植过去。可以设想为:一位学生大学毕业了,留校担任教师,他原有的部分数据对现在的教师身份来说仍然是有用的,应当保留并成为其教师的数据的

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论