面向对象上机实验5(参考代码)

整理文档很辛苦,赏杯茶钱您下走!

免费阅读已结束,点击下载阅读编辑剩下 ...

阅读已结束,您可以下载文档离线阅读编辑

资源描述

实验5-1编一个程序,用成员函数重载运算符“+”和“-”将两个二维数组相加和相减,要求第1个二维数组的值由构造函数设置,另一个二维数组的值由键盘输入。#includeiostreamusingnamespacestd;#includeiomanipconstintrow=2;constintcol=3;classarray{public:array();//构造函数array(inta,intb,intc,intd,inte,intf);voidget_array();//由键盘输入数组的值voiddisplay();//显示数组的值arrayoperator+(array&X);//将两个数组相加arrayoperator-(array&X);//将两个数组相减private:intvar[row][col];};array::array(){for(inti=0;irow;i++)for(intj=0;jcol;j++)var[i][j]=0;}array::array(inta,intb,intc,intd,inte,intf)//由构造函数设置数组的值{var[0][0]=a;var[0][1]=b;var[0][2]=c;var[1][0]=d;var[1][1]=e;var[1][2]=f;}voidarray::get_array()//由键盘输入数组的值{coutPleaseinput2*3dimensiondata:endl;for(inti=0;irow;i++)for(intj=0;jcol;j++)cinvar[i][j];}voidarray::display()//显示数组的值{for(inti=0;irow;i++){for(intj=0;jcol;j++)coutsetw(5)var[i][j];coutendl;}}arrayarray::operator+(array&X)//将两个数组相加{arraytemp;for(inti=0;irow;i++)for(intj=0;jcol;j++)temp.var[i][j]=var[i][j]+X.var[i][j];returntemp;}arrayarray::operator-(array&X)//将两个数组相减{arraytemp;for(inti=0;irow;i++)for(intj=0;jcol;j++)temp.var[i][j]=var[i][j]-X.var[i][j];returntemp;}intmain(){arrayX(11,22,33,44,55,66);arrayY,Z;Y.get_array();coutDisplayobjectXendl;X.display();coutDisplayobjectYendl;Y.display();Z=X+Y;coutDisplayobjectZ=X+Yendl;Z.display();Z=X-Y;coutDisplayobjectZ=X-Yendl;Z.display();return0;}实验5-2将实验5-1用友元函数重载运算符“+”和“-”将两个二维数组相加和相减。#includeiostream.h#includeiomanip.h//usingnamespacestd;constintrow=2;constintcol=3;classarray{public:array();//构造函数array(inta,intb,intc,intd,inte,intf);voidget_array();//由键盘输入数组的值voiddisplay();//显示数组的值friendarrayoperator+(array&X,array&Y);//将两个数组相加friendarrayoperator-(array&X,array&Y);//将两个数组相减private:intvar[row][col];};array::array(){for(inti=0;irow;i++)for(intj=0;jcol;j++)var[i][j]=0;}array::array(inta,intb,intc,intd,inte,intf)//由构造函数设置数组的值{var[0][0]=a;var[0][1]=b;var[0][2]=c;var[1][0]=d;var[1][1]=e;var[1][2]=f;}voidarray::get_array()//由键盘输入数组的值{coutPleaseinput2*3dimensiondata:endl;for(inti=0;irow;i++)for(intj=0;jcol;j++)cinvar[i][j];}voidarray::display()//显示数组的值{for(inti=0;irow;i++){for(intj=0;jcol;j++)coutsetw(5)var[i][j];coutendl;}}arrayoperator+(array&X,array&Y)//将两个数组相加{arraytemp;for(inti=0;irow;i++)for(intj=0;jcol;j++)temp.var[i][j]=Y.var[i][j]+X.var[i][j];returntemp;}arrayoperator-(array&X,array&Y)//将两个数组相减{arraytemp;for(inti=0;irow;i++)for(intj=0;jcol;j++)temp.var[i][j]=X.var[i][j]-Y.var[i][j];returntemp;}intmain(){arrayX(11,22,33,44,55,66);arrayY,Z;Y.get_array();coutDisplayobjectXendl;X.display();coutDisplayobjectYendl;Y.display();Z=X+Y;coutDisplayobjectZ=X+Yendl;Z.display();Z=X-Y;coutDisplayobjectZ=X-Yendl;Z.display();return0;}实验5-3编写一个程序,要求:(1)声明一个类complex,定义类complex的两个对象c1和c2,对象c1通过构造函数直接指定复数的实部和虚部(类私有数据成员为double类型的real和imag)为2.5及3.7,对象c2通过构造函数直接指定复数的实部和虚部为4.2及6.5;(2)定义友元运算符重载函数,它以c1、c2对象为参数,调用该函数时能返回两个复数对象相加操作;(3)定义成员函数print,调用该函数时,以格式“(real,imag)”输出当前对象的实部和虚部,例如:对象的实部和虚部分别是4.2和6.5,则调用print函数输出格式为:(4.2,6.5);(4)编写主程序,计算出复数对象c1和c2相加结果,并将其结果输出。#includeiostream.hclasscomplex{public:complex(doubler=0,doublei=0);friendcomplexoperator+(constcomplexc1,constcomplexc2);voidprint();private:doublereal,imag;};complex::complex(doubler,doublei){real=r;imag=i;}complexoperator+(constcomplexc1,constcomplexc2){complextemp;temp.real=c1.real+c2.real;temp.imag=c1.imag+c2.imag;returntemp;}voidcomplex::print(){cout(real,imag)endl;}intmain(){complexc1(2.5,3.7),c2(4.2,6.5);complexc;c=c1+c2;c.print();return0;}实验5-4定义复数类的加法、减法与乘法运算。#includeiostream.hclassComplex{public:Complex(doubler=0,doublev=0):real(r),imag(v){}//构造函数friendComplexoperator+(Complexa,Complexb);//在类内对友元函数进行声明friendComplexoperator-(Complexa,Complexb);friendComplexoperator*(Complexa,Complexb);friendostream&operator(ostream&out,Complex&a);private:doublereal;doubleimag;};//输出运算符重载的实现ostream&operator(ostream&out,Complex&a){returnouta.real+a.imagi\n;}//+运算符重载的实现Complexoperator+(Complexa,Complexb){returnComplex(a.real+b.real,a.imag+b.imag);}//-运算符重载的实现Complexoperator-(Complexa,Complexb){returnComplex(a.real-b.real,a.imag-b.imag);}//*运算符重载的实现Complexoperator*(Complexa,Complexb){returnComplex(a.real*b.real-a.imag*b.imag,a.real*b.imag+a.imag*b.real);}voidmain(){Complexa(2,5),b(7,8),c(0,0);c=a+b;cout两个复数的和是:c;c=a-b;cout两个复数的差是:c;c=a*b;cout两个复数的积是:c;}

1 / 6
下载文档,编辑使用

©2015-2020 m.777doc.com 三七文档.

备案号:鲁ICP备2024069028号-1 客服联系 QQ:2149211541

×
保存成功