欢迎来到天天文库
浏览记录
ID:34761529
大小:119.63 KB
页数:11页
时间:2019-03-10
《实验流运算符的重载及文件的使用》由会员上传分享,免费在线阅读,更多相关内容在工程资料-天天文库。
1、西华大学实验报告(2010/2011学年第二学期)课程名称面向对象程序设计实验名称流运算符的重载及文件的使用实验时间年月日指导单位指导教师学生姓名班级学号学院(系)专业实验报告实验名称流运算符的重载及文件的使用实验类型验证实验学时2×2一、实验目的和要求(1)掌握在自定义的类中重载输入流运算符>>和输出流运算符<<,并输入/输出本类对象。(2)掌握文件操作的步骤和方法,能利用程序建立数据文件、打开数据文件并进行相关操作。二、实验环境(实验设备)硬件:微型计算机软件:MicrosoftVisualC++6.0三、实验原理及内容实验题目(1):在
2、上一次实验的基础上,在复数类中增加输入流运算符>>和输出流运算符<<的重载,主函数通过“cin>>对象名”输入对象的值,通过“cout<<对象名”输出对象的值,输出复数值时将原来主函数中“对象名.print()”改成“cout<<对象名”形式。实验解答:①写出在复数类中增加的输入流运算符>>和输出流运算符<<的重载函数完整代码:ostream&operator<<(ostream&out,constComplex&a){out<0)11out<<"+";out<3、"i";}out<>(istream&in,constComplex&a){in>>a.real;if(a.imag!=0){if(a.imag>0)in>>"+";in>>a.imag>>"i";}returnin;}②写出主函数中调用流运算符函数的主要代码(对算术运算符重载函数的调用可以忽略)voidmain(){Complexa1,a2;cout<<"a1=";cin>>a1;cout<<"a2=";cin>>a2;cout<<"originala1is:"<4、endl;cout<<"originala2is:"<>、<<运算符重载的定义及调用,写出完整的程序代码。#includeusingnamespacestd;templateclassComplex{private:Treal;Timag;public:Complex(Tr=0,Ti=0){real=r;imag=i;}friend5、Complexoperator+(constComplex&a,constComplex&b);friendComplexoperator-(constComplex&a,constComplex&b);friendostream&operator<<(ostream&out,constComplex&a);friendistream&operator>>(istream&in,constComplex&a);Complexoperator*(constComplex&a);Complexoperator/(constComplex&a);}6、;templateostream&operator<<(ostream&out,constComplex&a){out<0)out<<"+";out<istream&operator>>(istream&in,constComplex&a){in>>a.real;if(a.imag!=0)11{if(a.imag>0)in>>"+";in>>a.imag>>"i"7、;}returnin;}templateComplexoperator+(constComplex&a,constComplex&b){Complextemp;temp.real=a.real+b.real;temp.imag=a.imag+b.imag;returntemp;}templateComplexoperator-(constComplex&a,constComplex&b){Complextemp;temp.real=a.real-b.real;te8、mp.imag=a.imag-b.imag;returntemp;}templateComplexComplex::operator*(
3、"i";}out<>(istream&in,constComplex&a){in>>a.real;if(a.imag!=0){if(a.imag>0)in>>"+";in>>a.imag>>"i";}returnin;}②写出主函数中调用流运算符函数的主要代码(对算术运算符重载函数的调用可以忽略)voidmain(){Complexa1,a2;cout<<"a1=";cin>>a1;cout<<"a2=";cin>>a2;cout<<"originala1is:"<4、endl;cout<<"originala2is:"<>、<<运算符重载的定义及调用,写出完整的程序代码。#includeusingnamespacestd;templateclassComplex{private:Treal;Timag;public:Complex(Tr=0,Ti=0){real=r;imag=i;}friend5、Complexoperator+(constComplex&a,constComplex&b);friendComplexoperator-(constComplex&a,constComplex&b);friendostream&operator<<(ostream&out,constComplex&a);friendistream&operator>>(istream&in,constComplex&a);Complexoperator*(constComplex&a);Complexoperator/(constComplex&a);}6、;templateostream&operator<<(ostream&out,constComplex&a){out<0)out<<"+";out<istream&operator>>(istream&in,constComplex&a){in>>a.real;if(a.imag!=0)11{if(a.imag>0)in>>"+";in>>a.imag>>"i"7、;}returnin;}templateComplexoperator+(constComplex&a,constComplex&b){Complextemp;temp.real=a.real+b.real;temp.imag=a.imag+b.imag;returntemp;}templateComplexoperator-(constComplex&a,constComplex&b){Complextemp;temp.real=a.real-b.real;te8、mp.imag=a.imag-b.imag;returntemp;}templateComplexComplex::operator*(
4、endl;cout<<"originala2is:"<>、<<运算符重载的定义及调用,写出完整的程序代码。#includeusingnamespacestd;templateclassComplex{private:Treal;Timag;public:Complex(Tr=0,Ti=0){real=r;imag=i;}friend
5、Complexoperator+(constComplex&a,constComplex&b);friendComplexoperator-(constComplex&a,constComplex&b);friendostream&operator<<(ostream&out,constComplex&a);friendistream&operator>>(istream&in,constComplex&a);Complexoperator*(constComplex&a);Complexoperator/(constComplex&a);}
6、;templateostream&operator<<(ostream&out,constComplex&a){out<0)out<<"+";out<istream&operator>>(istream&in,constComplex&a){in>>a.real;if(a.imag!=0)11{if(a.imag>0)in>>"+";in>>a.imag>>"i"
7、;}returnin;}templateComplexoperator+(constComplex&a,constComplex&b){Complextemp;temp.real=a.real+b.real;temp.imag=a.imag+b.imag;returntemp;}templateComplexoperator-(constComplex&a,constComplex&b){Complextemp;temp.real=a.real-b.real;te
8、mp.imag=a.imag-b.imag;returntemp;}templateComplexComplex::operator*(
此文档下载收益归作者所有