资源描述:
《【例4.8】定义复数complex类,使用友元函数》由会员上传分享,免费在线阅读,更多相关内容在工程资料-天天文库。
1、【例4.8】定义复数Complex类,使用友元函数,完成复数的加法、减法、乘法运算,以及对复数的输出。#includeclassComplex{doublereal;doubleimage;public:Complex(doubler=0,doublei=0){real=r;image=i;}friendvoidinputcomplex(Complex&comp);friendComplexaddcomplex(Complexc1,Complexc2);friendComplexsubcomplex(Complexc1,Complexc2);fr
2、iendComplexmulcomplex(Complexc1,Complexc2);friendvoidoutputcomplex(Complexcomp);};voidinputcomplex(Complex&comp){cin>>comp.real>>comp.image;otherstaffoftheCentre.Duringthewar,ZhuwastransferredbacktoJiangxi,andDirectorofthenewOfficeinJingdezhen,JiangxiCommitteeSecretary.Startingin1939serv
3、edasrecorderoftheWestNorthOrganization,SecretaryoftheSpecialCommitteeAfterthevictoryofthelongMarch,hehasbeentheNorthwestOfficeoftheFederationofStateenterprisesMinister,ShenmufuguSARmissions,DirectorofNingxiaCountypartyCommitteeSecretaryandrecorderoftheCountypartyCommitteeSecretary,Minist
4、ersand}Complexaddcomplex(Complexc1,Complexc2){Complexc;c.real=c1.real+c2.real;c.image=c1.image+c2.image;returnc;}Complexsubcomplex(Complexc1,Complexc2){Complexc;c.real=c1.real-c2.real;c.image=c1.image-c2.image;returnc;}Complexmulcomplex(Complexc1,Complexc2){Complexc;c.real=c1.real*c2.rea
5、l-c1.image*c2.image;otherstaffoftheCentre.Duringthewar,ZhuwastransferredbacktoJiangxi,andDirectorofthenewOfficeinJingdezhen,JiangxiCommitteeSecretary.Startingin1939servedasrecorderoftheWestNorthOrganization,SecretaryoftheSpecialCommitteeAfterthevictoryofthelongMarch,hehasbeentheNorthwest
6、OfficeoftheFederationofStateenterprisesMinister,ShenmufuguSARmissions,DirectorofNingxiaCountypartyCommitteeSecretaryandrecorderoftheCountypartyCommitteeSecretary,Ministersandc.image=c1.real*c2.image+c1.image*c2.real;returnc;}voidoutputcomplex(Complexcomp){cout<<"("<7、image<<")";}voidmain(){Complexc1,c2,result;cout<<"请输入第一个复数的实部和虚部:"<