收藏 分销(赏)

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

上传人:pc****0 文档编号:6660295 上传时间:2024-12-19 格式:DOC 页数:16 大小:95KB 下载积分:10 金币
下载 相关 举报
实验五 运算符重载完成.doc_第1页
第1页 / 共16页
实验五 运算符重载完成.doc_第2页
第2页 / 共16页


点击查看更多>>
资源描述
实验五 运算符重载的应用 班级: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<iostream> //#include<iostream.h> using 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; cout<<"请输入复数的虚部和实部:"; cin>>real>>imag; } void Complex::Display() { cout<<"*********显示**********"<<endl; cout<<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,重载运算符"+"、"-"、"=="、"!="、"++"、"--"、">>"、"<<", 实现两个点的相加、相减、相等、不等、自增、自减、输入和输出运算。*/ //#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 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; cout<<"请输入点坐标(x,y)"; cin>>x>>y; count++; } void Point::Display() { cout<<"******显示******"<<endl; cout<<"第"<<count<<"个坐标为:"<<"("<<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 &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&&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--(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) //重载流插入运算符 { 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; cout<<"Overload'+'----a=a+b:"<<a<<endl; a=a-b; cout<<"Overload'-'----a=a-b:"<<a<<endl; cout<<"重新用'<<','>>'输入输出:"<<endl; cout<<"输入a的坐标:"; cin>>a; cout<<"显示<<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!=b) cout<<"a!=b"<<endl; return 0; } 运行结果: 程序代码:/*3、定义一个矩阵类Matrix,均为M行N列,通过重载运算符"+"、"-","<<",">>","++","--", "==","!="来实现矩阵的相加、相减、输出、输入、自增、自减以及相等、不等的判断。*/ #include<iostream> using namespace std; const int M=3; const int 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 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 a[][N],Matrix b[][N]) { int i,j; Matrix c[M][N]; for(i=0;i<M;i++) for(j=0;j<N;j++) { c[i][j]=a[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<M;i++) for(j=0;j<N;j++) { c[i][j]=a[i][j]-b[i][j]; } return c[M][N]; } istream & operator>>(istream & input,Matrix *c) { int i,j; for(i=0;i<M;i++) for(j=0;j<N;j++) input>>c[i][j]; } ostream & operator<<(istream & output,Matrix *c) { int i,j; for(i=0;i<M;i++) for(j=0;j<N;j++) output<<c[i][j]<<" "; } Matrix operator++(Matrix *c) { int i,j; for(i=0;i<M;i++) for(j=0;j<N;j++) ++c[i][j]; return c[M][N]; } Matrix operator--(Matrix *c) { int i,j; for(i=0;i<M;i++) for(j=0;j<N;j++) --c[i][j]; return c[M][N]; } int operator==(Matrix *a,Matrix *b) { int i,j; for(i=0;i<M;i++) for(j=0;j<N;j++) { if(a[i][j]!=b[i][j]) 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(a[i][j]!=b[i][j]) return 1; } return 0; } int main() { Matrix a[M][N],b[M][N],c[M][N]; cout<<"<<,>>"<<endl; cin>>a; cin>>b; cout<<"Display(a):"<<a; cout<<"Display:(b)"<<b; cout<<"Overload + and -:"<<endl; c[M][N]=a[M][N]+[M][N]; cout<<"Display(a):"<<c; c[M][N]=a[M][N]-[M][N]; cout<<"Display:(b)"<<c; cout<<"Overload == and !=:"<<endl; if(a[M][N]==b[M][N]) cout<<"a[M][N]==b[M][N]"<<endl; else cout<<"a[M][N]!=b[M][N]"<<endl; if(a[M][N]!=b[M][N]) cout<<"a[M][N]!=b[M][N]"<<endl; else cout<<"a[M][N]==b[M][N]"<<endl; return 0; } 4. 程序代码:/*4、定义时间类Time,时间的表示采用24小时制。重载运算符"<<"和">>"实现时间的输出和输入; 重载运算符"+"和"-"实现时间推后和提前若干分钟;重载运算符"++"和"--"实现当前时间推后和提前1小时; 重载">"、"<"、"=="来判断两个时间之间大于、小于、等于以及不等于的关系。 注意:输入时需对数据合法性进行判断。*/ #include<iostream.h> //using namespace std; 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 & 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,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!!"<<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) 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; 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.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) 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) 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********"<<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 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<<"*******Compare********"<<endl; if(t==t0) cout<<"t==t0"<<endl; else if(t>t0) cout<<"t>t0"<<endl; else cout<<"t<t0"<<endl; return 0; } 运行结果: 程序代码:/*5、编写一个程序,处理一个复数与一个double型数相加的运算,结果存放在一个double型的变量d1中, 输出d1的值,再以复数形式输出此值。定义复数(Complex)类,在成员函数中包含重载类型转换运算符: Operator double(){ return real;}*/ #include<iostream.h> //using namespace std; class Complex //Define class Complex { private: double real,imag; public: double Complex::operator+(double a); friend 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) // 重载类型转换符“+” { double b; b=a+real; return b; } ostream & operator<<(ostream & output,Complex &a) //重载“ <<” { output<<a.real<<"+"<<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<<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个相同的数据成员移植过去。可以设想为: 一位学生大学毕业了,留校担任教师,他原有的部分数据对现在的教师身份来说仍然是有用的, 应当保留并成为其教师的数据的一部分。*/ #include<iostream> #include<string> #include<iomanip> using namespace std; class Teacher; class Student //定义学生类 { private: string num,name; string sex; 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 Student::Input() { cout<<"对学生信息进行输入(学号、姓名、性别(f 或 m)):"; cin>>num>>name>>sex; } void Student::Display() { cout<<"对学生信息进行输出:"<<endl; cout<<setw(10)<<"学号"<<setw(10)<<"姓名"<<setw(10)<<"性别"<<endl; cout<<setw(10)<<num<<setw(10)<<name<<setw(10)<<sex<<endl; } void Teacher::Input() { cout<<"对教师信息进行输入(学号、姓名、性别(f 或 m)):"; cin>>number>>na>>s; } void Teacher::Display() { cout<<"对教师信息进行输出:"<<endl; cout<<setw(10)<<"工号"<<setw(10)<<"姓名"<<setw(10)<<"性别"<<endl; cout<<setw(10)<<number<<setw(10)<<na<<setw(10)<<s<<endl; } Teacher Teacher::operator=(Student &s) { number=s.num; na=s.name; s=s.sex; } int main() { Student s; Teacher t; s.Input(); s.Display(); t=s; t.Display(); return 0; } 五. 实验总结
展开阅读全文

开通  VIP会员、SVIP会员  优惠大
下载10份以上建议开通VIP会员
下载20份以上建议开通SVIP会员


开通VIP      成为共赢上传

当前位置:首页 > 包罗万象 > 大杂烩

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

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

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

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

gongan.png浙公网安备33021202000488号   

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

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

客服