实验五 运算符重载 完成

上传人:mg****85 文档编号:34128902 上传时间:2018-02-21 格式:DOC 页数:16 大小:95KB
返回 下载 相关 举报
实验五 运算符重载  完成_第1页
第1页 / 共16页
实验五 运算符重载  完成_第2页
第2页 / 共16页
实验五 运算符重载  完成_第3页
第3页 / 共16页
实验五 运算符重载  完成_第4页
第4页 / 共16页
实验五 运算符重载  完成_第5页
第5页 / 共16页
点击查看更多>>
资源描述

《实验五 运算符重载 完成》由会员分享,可在线阅读,更多相关《实验五 运算符重载 完成(16页珍藏版)》请在金锄头文库上搜索。

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

2、” 、 “” 、 “!=” 、 “+”、 “-”、 “”、“”,“+”,“-”, “=”, “!=”来实现矩阵的相加、相减、输出、输入、自增、自减以及相等、不等的判断。4、定义时间类 Time,时间的表示采用 24 小时制。重载运算符“”实现时间的输出和输入;重载运算符“+”和“-”实现时间推后和提前若干分钟;重载运算符“+”和“-”实现当前时间推后和提前 1 小时;重载“” 、 “ /#includeusing std:cin;using std:cout;using std:endl;/using namespace std;class Complex /定义复数类private:doub

3、le 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 friend Complex operator-(double b,Complex ;Complex operator-(Complex &a,double b) /定义“-” 重载函数return Complex(a.real-=b,a.imag);Complex operator-(double b,Com

4、plex &a)return Complex(a.real-=b,a.imag);void Complex:Input()coutrealimag;void Complex:Display()coutm;c=c-m;c.Display();return 0;运行结果:2.程序代码:/*2、定义点类 Point,重载运算符、 、 、 !=、+ 、-、 、 #include/using std:cin; /用#include加上 Using std:cin;using / std:cout;using std:endl;对重载流插入、流提取运算符错误/using std:cout;/using s

5、td: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 Point operator-(Point friend int operator=(Point friend int operator!=(Point friend Point operator+(Point friend Point operator+(Poin

6、t friend Point operator-(Point friend Point operator-(Point friend istream friend ostream ;int Point:count=0; /注意赋值时没有 static, int static Point:count=0; wrong!void Point:Input()coutxy;count+;void Point:Display()cout(istream & input,Point & a) /重载流插入运算符inputa.xa.y;return input;ostream & operator输入输出:

7、a;coutb;cout,+,-,=,!=来实现矩阵的相加、相减、输出、输入、自增、自减以及相等、不等的判断。*/#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 friend istream friend ostream friend Matrix operator+(Matrix friend Matrix o

8、perator-(Matrix friend int operator=(Matrix friend int operator!=(Matrix ;Matrix operator+(Matrix aN,Matrix bN)int i,j;Matrix cMN;for(i=0;i(istream & input,Matrix *c)int i,j;for(i=0;icij;ostream & operatora;cinb;cout实现时间的输出和输入;重载运算符+和-实现时间推后和提前若干分钟;重载运算符+和-实现当前时间推后和提前 1 小时;重载、/using namespace std;cl

9、ass Time /Define class Timeprivate:int hour,minute,second;public:Time operator-();Time operator+();Time operator+(int n);Time operator-(int n);friend istream friend ostream friend int operator(Time friend int operator=(Time ;istream & operator(istream & Input,Time & t) /Overload /Make sure hour,minu

10、te,second right.Inputt.hourt.minutet.second;while(1)if(t.hour24|t.hour60|t.minute60|t.secondt.hourt.minutet.second;if(t.hour0&t.minute0&t.second0)break;return Input; ostream & 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=

11、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;coutt0;coutt0)coutt0/using namespace std;class Comple

12、x /Define class Complexprivate:double real,imag;public:double Complex:operator+(double a);friend ostream friend istream 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(istream & input

13、,Complex &a) /重载“ ”inputa.reala.imag;return input;int main() / 主函数Complex s;double a,d1;couta):;cina;d1=s+a;cout#include#includeusing namespace std;class Teacher;class Student /定义学生类private:string num,name;string sex;public:void Input();void Display();friend class Teacher;friend Teacher Teacher:oper

14、ator=(Student ;class Teacher /定义教师类private:string number,na;string s;public:void Input();void Display();Teacher operator=(Student ;void Student:Input()coutnumnamesex;void Student:Display()coutnumbernas;void Teacher:Display()cout对教师信息进行输出:endl;coutsetw(10)工号setw(10)姓名setw(10)性别endl;coutsetw(10)numbersetw(10)nasetw(10)sendl;Teacher Teacher:operator=(Student &s)number=s.num;na=s.nam

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

当前位置:首页 > 生活休闲 > 科普知识

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