版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡(jiǎn)介
Chapter10OperatorOverloading10.1Theneedforoperatoroverloading
(運(yùn)算符重載的必要性)OperatorOverloadingProgrammercanusesomeoperatorsymbolstodefinespecialmemberfunctionsofaclassProvidesconvenientnotationsforobjectbehaviorsTheC++built-inarithmetic(+,*,/etc.)andrelationaloperators(>,<,==,!=)workwithbuilt-indatatypes(int,float,etc.).notallthebuilt-inoperatorscanbeusedwitheverydatatype.Forexample,multiplicationisnotavalidoperationforstringsand%canonlybeusedwithintegerdatatypes.10.1Theneedforoperatoroverloading
(運(yùn)算符重載的必要性)The+operator,however,isappropriateforstringsandistakentomeanconcatenationThismeansthattheoperator+hasadifferentmeaningfornumericdatatypesthanforstringdatatypes.Whenyoucreateanewclassyoucanredefineoroverloadexistingoperatorssuchas+Existingoperatorsareoverloadedforspecificuseinaclassusebywritingoperatorfunctionsfortheclass.10.2Overloadingtheadditionoperator+
(重載加法運(yùn)算符+)ProgramExampleP10A1//ProgramExampleP10A2//Demonstrationofanoverloaded+operator.3#include<iostream>4usingnamespacestd;56classtime24//Asimple24-hourtimeclass.7{8public:9 time24(inth=0,intm=0,ints=0);10 voidset_time(inth,intm,ints);11 voidget_time(int&h,int&m,int&s)const;12 time24operator+(int
secs)const;13private:14 inthours;//0to2315 intminutes;//0to5916 Intseconds;//0to5917}Theprototypefortheoverloaded+operatorisonline12.(重載運(yùn)算符+的函數(shù)的函數(shù)原型)10.2Overloadingtheadditionoperator+18//Constructor.19time24::time24(inth,intm,ints):hours(h),minutes(m),seconds(s){}2021//Mutatorfunction.22voidtime24::set_time(inth,intm,ints)23{24 hours=h;minutes=m;seconds=s;25}2627//Inspectorfunction.28voidtime24::get_time(int&h,int&m,int&s)const29{30 h=hours;m=minutes;s=seconds;31}10.2Overloadingtheadditionoperator+32//Overloaded+operator.33time24time24::operator+(int
secs)const34{35 //Addsecstoclassmembersecondsandcalculatethenewtime.36 time24temp;37 temp.seconds=seconds+secs;38
temp.minutes=minutes+temp.seconds/60;39 temp.seconds%=60;40 temp.hours=hours+temp.minutes/60;41 temp.minutes%=60;42 temp.hours%=24;43 returntemp;//Returnthenewtime.44}definetheoverloaded+operatorforaddinganintegernumberofsecondstoatime24objectandreturningtheresult.10.2Overloadingtheadditionoperator+45main()46{47 inth,m,s;48 time24t1(23,59,57);//t1represents23:59:5749 time24t2;50 t2=t1+4;//t2shouldnowbe0:0:151 t2.get_time(h,m,s);52 cout<<"Timet2is"<<h<<":"<<m<<":"<<s<<endl;53}itcanbecalledlikeanyotherfunction;line50canalsobewrittenas:t2=t1.operator+(4);ProgramExampleP10A+B+C1//ProgramExampleP10C2//Demonstrationofanoverloaded+operator.3#include<iostream>4usingnamespacestd;5classtime24//Asimple24-hourtimeclass.6{7public:8 time24(inth=0,intm=0,ints=0);9 voidset_time(inth,intm,ints);10 voidget_time(int&h,int&m,int&s)const;11 time24operator+(int
secs)const;12 time24operator+(consttime24&t)const;13 time24operator+(int
secs,consttime24&t)14private:15 inthours;//0to2316 intminutes;//0to5917 Intseconds;//0to5918}10.2Overloadingtheadditionoperator+
(重載加法運(yùn)算符+)Theprototypefortheoverloaded+operatorisonline12.(重載運(yùn)算符+的函數(shù)的函數(shù)原型)10.2Overloadingtheadditionoperator+19//Constructor.20time24::time24(inth,intm,ints):hours(h),minutes(m),seconds(s){}21//Mutatorfunction.22voidtime24::set_time(inth,intm,ints)23{24 hours=h;minutes=m;seconds=s;25}2627//Inspectorfunction.28voidtime24::get_time(int&h,int&m,int&s)const29{30 h=hours;m=minutes;s=seconds;31}10.2Overloadingtheadditionoperator+32//Overloaded+operator.33time24time24::operator+(int
secs)const34{35 //Addsecstoclassmembersecondsandcalculatethenewtime.36 time24temp;37 temp.seconds=seconds+secs;38
temp.minutes=minutes+temp.seconds/60;39 temp.seconds%=60;40 temp.hours=hours+temp.minutes/60;41 temp.minutes%=60;42 temp.hours%=24;43 returntemp;//Returnthenewtime.44}definetheoverloaded+operatorforaddinganintegernumberofsecondstoatime24objectandreturningtheresult.45//Overloaded+operator.46time24time24::operator+(consttime24&t)const47{48 //Addtotalsecondsinttosecondsandcalculatethenewtime.49 time24temp;50 int
secs=t.hours*3600+t.minutes*60+t.seconds;51 temp.seconds=seconds+secs;52 temp.minutes=minutes+temp.seconds/60;53 temp.seconds%=60;54 temp.hours=hours+temp.minutes/60;55 temp.minutes%=60;56 temp.hours%=24;57 returntemp;//Returnthenewtime.58}10.2Overloadingtheadditionoperator+definetheoverloaded+operatorforaddinganatime24objectofsecondstoatime24objectandreturningtheresult.10.2Overloadingtheadditionoperator+//Non-memberoverloaded+operator.59time24operator+(int
secs,consttime24&t)60{61 //Addsecstottocalculatethenewtime.62 time24temp;63 temp=t+secs;//Usesthememberfunctionoperator+(int).64 returntemp;//Returnthenewtime.65}Theprogramcontainsanon-classstandalonefunctioncalledoperator+()onlines59to65.Thisfunctionisnotamemberofthetime24class.Likeanyotherfunctionitcanbecalledintheconventionalwaywithastatementsuchas:t2=operator+(4,t1);whichisequivalenttothestatementonline81.66main()67{68 inth,m,s;69 time24start_time(23,0,0);70 time24elapsed_time(1,2,3);71 time24finish_time;72 finish_time=start_time+elapsed_time;73 finish_time.get_time(h,m,s);74 cout<<"FinishTimeis"75 <<h<<":"<<m<<":"<<s<<endl;10.2Overloadingtheadditionoperator+itcanbecalledlikeanyotherfunction;line72canalsobewrittenas:start_time.operator+(elapsed_time);76 time24t1(23,59,57);//t1represents23:59:5777 time24t2;78 t2=t1+4;79 t2.get_time(h,m,s);80 cout<<"Timet2is"<<h<<":"<<m<<":"<<s<<endl;81 t2=4+t1;82 t2.get_time(h,m,s);83 cout<<"Timet2is"<<h<<":"<<m<<":"<<s<<endl;84}10.2Overloadingtheadditionoperator+itcanbecalledlikeanyotherfunction;line50canalsobewrittenas:t2=t1.operator+(4);whichisequivalenttothestatementonline81.t2=operator+(4,t1);10.3Rulesofoperatoroverloading
(運(yùn)算符重載的規(guī)則)AlltheC++operatorsinappendixBwiththeexceptionofthefollowingfiveoperatorscanbeoverloaded..*::?:sizeofThefollowingrulesapplytooperatoroverloading:Newoperatorscannotbeinvented.Forexample,itisnotpossibletooverload<>tomean‘notequalto’or**tomean‘tothepowerof’.
10.3Rulesofoperatoroverloading
(運(yùn)算符重載的規(guī)則)Thefollowingrulesapplytooperatoroverloading:Theoverloadedoperatormusthavethesamenumberofoperandsasthecorrespondingpredefinedoperator.Forexample,anoverloadedequivalentoperator==musthavetwooperandsandanoverloaded++operatormusthaveoneoperand.Thepriorityofanoverloadedoperatorremainsthesameasitscorrespondingpredefinedoperator.Forexample,regardlessofwhether*isoverloadedornot,itwillalwayshaveahigherprecedencethan+and-.10.3Rulesofoperatoroverloading
(運(yùn)算符重載的規(guī)則)Thefollowingrulesapplytooperatoroverloading:Overloadedoperatorscannothavedefaultarguments.Theoperatorsforthebuilt-indatatypes,e.g.int,floatandsoon,cannotberedefined.10.4Overloading++
(重載運(yùn)算符++)ProgramExampleP10D1//ProgramExampleP10D2//Programtodemonstrateoverloadingtheprefix++operator.3#include<iostream>4usingnamespacestd;56classtime24//Asimple24-hourtimeclass.7{8public:9time24(inth=0,intm=0,ints=0);10voidset_time(inth,intm,ints);11voidget_time(int&h,int&m,int&s)const;12time24operator+(int
secs)const;13time24operator+(consttime24&t)const;14time24operator++();15private:16inthours;//0to2317intminutes;//0to5918intseconds;//0to5919};Theprototypefortheoverloaded++operatorisonline14.10.4Overloading++
(重載運(yùn)算符++)67time24time24::operator++()68{69//Add1secondtothecallingobject'sseconds.70*this=*this+1;//Usesmemberfunctionoperator+(int).71return*this;//Returnupdatedtimeofcallingobject.72}83main()84{85inth,m,s;86time24t1(23,59,57);8788++t1;89t1.get_time(h,m,s);90cout<<"Timet1is"<<h<<":"<<m<<":"<<s<<endl;91}Thefunctionmakesuseofthespecialbuilt-inpointerthis.Thethispointerisavailableinallmemberfunctionsofaclassandisapointertotheobjectthatcalledthememberfunction.Thefunctiondefinitionforthe++operatorisonlines67to72.Whenoperator++()iscalledonline88,thiscontainstheaddressoft1.Line70thereforeadds1tot1.10.4.1Overloadingprefixandpostfixformsof++Theincrementoperator++needstobeoverloadedinbothitsprefixandpostfixforms.Forexample,ift1andt2aretime24objectsthenthefollowingtwostatementswillproducedifferentresultst2=++t1;//Useofprefix++.t2=t1++;//Useofpostfix++.Thefirststatementincrementst1beforeassigningitsvaluetot2.Thesecondstatementassignst1tot2andthenincrementst1.10.4.1Overloadingprefixandpostfixformsof++Todistinguishbetweenthetwooperator++functions,thepostfixversionusesa‘dummy’integerparameter.1//ProgramExampleP10E2//Programtodemonstrateoverloadingprefixandpostfix++.3#include<iostream>4usingnamespacestd;56classtime24//Asimple24-hourtimeclass.7{8public:9time24(inth=0,intm=0,ints=0);10voidset_time(inth,intm,ints);11voidget_time(int&h,int&m,int&s)const;12time24operator+(int
secs)const;13time24operator+(consttime24&t)const;14time24operator++();//prefix.15time24operator++(int);//postfix.16private:17inthours;//0to2318intminutes;//0to5919intseconds;//0to5920};10.4.1Overloadingprefixandpostfixformsof++76time24time24::operator++(int)77{78//Savecallingobjectbeforeincrementingseconds.79time24temp;80temp=*this;81*this=*this+1;//Usesoperator+(int),82//couldalsouse++(*this).83returntemp;//Returnthesavedcallingobject.84}Thepostfixversionofoperator++isdefinedinlines76to84.Theoriginalvalueofthetime24objectthatcalledthememberfunctionisstoredintemp.Onlyaftersavingtheoriginalvalueofthetime24objectisitsvalueincremented.Theoriginalvalueofthetime24object(storedintemp)isthenreturned.10.4.1Overloadingprefixandpostfixformsof++Lines101to107testanddisplaytheresultofusingthepostfixoperator++andlines111to117testanddisplaytheresultofusingtheprefixThevalueoft1isidenticalinbothtests,butt2isassignedtwodifferentvalues.95main()96{97inth,m,s;98time24t1(23,59,57);99time24t2;100101t2=t1++;//Testpostfix++102t1.get_time(h,m,s);103cout<<"Usingpostfix++:"<<"timet1is:"104<<h<<":"<<m<<":"<<s;105t2.get_time(h,m,s);106cout<<",timet2is:"107<<h<<":"<<m<<":"<<s<<endl;108109t1.set_time(23,59,57);//Resetthetime.110111t2=++t1;//Testprefix++112t1.get_time(h,m,s);113cout<<"Usingprefix++:"<<"timet1is:"114<<h<<":"<<m<<":"<<s;115t2.get_time(h,m,s);116cout<<",timet2is:"117<<h<<":"<<m<<":"<<s<<endl;118}Usingpostfix++:timet1is:23:59:58,timet2is:23:59:57Usingprefix++:timet1is:23:59:58,timet2is:23:59:5810.4.2Improvingtheprefix++operatormember
functionThereturnstatementonline72ofprogramP10Ereturnsacopyoftheobject.
Itwouldbemoreefficient(especiallyforlargeobjects)toreturnareferencetotheobjectinstead.Todothisrequiresonlytwosmallchangestotheclass.Theoperator++functionheaderonline68changesto:time24&time24::operator++()andthefunctionprototypeonline14changesto:time24&operator++();68time24time24::operator++()69{70//Add1secondtothecallingobject71*this=*this+1;72return*this;73}10.4.2Improvingtheprefix++operatormember
functionThesameimprovementcannotbemadefortheoverloadedpostfixversionof++.Thereasonforthisisthattheobjectreturnedonline83isthelocalobjecttemp,whichwillbeoutofscopeandthereforeundefinedwhenthefunctionends.Returningareferencetoanundefinedobjectwillcauseproblems,soacopyoftemphastobereturned.76time24time24::operator++(int)77{78//Savecallingobjectbeforeincrementingseconds.79time24temp;80temp=*this;81*this=*this+1;//Usesoperator+(int),82//couldalsouse++(*this).83returntemp;//Returnthesavedcallingobject.84}10.5Overloadingrelationaloperators
(重載關(guān)系運(yùn)算符)1//ProgramExampleP10F2//Programtodemonstratetheoverloadingofthe==operator.3#include<iostream>4usingnamespacestd;56classtime24//Asimple24-hourtimeclass.7{8public:9time24(inth=0,intm=0,ints=0);10voidset_time(inth,intm,ints);11voidget_time(int&h,int&m,int&s)const;12time24operator+(int
secs)const;13time24operator+(consttime24&t)const;14time24&operator++();//prefix.15time24operator++(int);//postfix.16booloperator==(consttime24&t)const;17private:18inthours;//0to2319intminutes;//0to5920intseconds;//0to5921};10.5Overloadingrelationaloperators
(重載關(guān)系運(yùn)算符)87//Overloadedequalityoperator==.88booltime24::operator==(consttime24&t)const89{90if(hours==t.hours&&91minutes==t.minutes&&92seconds==t.seconds)93returntrue;94else95returnfalse;96}Theoverloadedoperator==forthetime24classonlines88to96returnsaboolvalueoftrueifthedatamembersofthecallingobject(t1)andtheargument(t2)areequal,otherwisefalseisreturned.10.5Overloadingrelationaloperators
(重載關(guān)系運(yùn)算符)107main()108{109inth,m,s;110time24t1(23,59,57);111time24t2;112113t2=++t1;//t1andt2shouldbeequal.114t1.get_time(h,m,s);115cout<<"t1is"116<<h<<":"<<m<<":"<<s;117t2.get_time(h,m,s);118cout<<",t2is"119<<h<<":"<<m<<":"<<s<<endl;120121if(t1==t2)//Testequalityoperator==.122cout<<"t1andt2areequal.Prefix++isworking."123<<endl;124else125cout<<"t1andt2arenotequal.Prefix++isnotworking."126<<endl;127}10.6Overloading<<and>>(重載運(yùn)算符<<和>>)108//Non-memberoverloaded<<operator.109ostream&operator<<(ostream&
os,consttime24&t)110{111//Formattime24object,precedesingledigitswitha0.112inth,m,s;113t.get_time(h,m,s);114os<<setfill('0')115<<setw(2)<<h<<":"116<<setw(2)<<m<<":"117<<setw(2)<<s<<endl;118returnos;119}Both>>and<<areimplementedasnon-memberfunctions.coutisanobjectoftheclassostreamandcinisanobjectoftheclassistream.Theseobjectsandclassesaredefinedintheheaderfileiostream.Anyfunctionthatusesanoutputstreamwillmodifyit,whichmeansthattheoutputstreammustbepassedbyreference,ratherthanbyvalue,toafunction.Thesecondparametertisdeclaredasareferencetoaconsttime24objectforthepurposeofefficiency.10.6Overloading<<and>>(重載運(yùn)算符<<和>>)Thereturntypeonline109isareferencetotheoutputstreamwhichisreturnedonline118.Thefirstpartofthisstatement(cout<<t1)displaysthevalueoft1andalsoreturnsareferencetocoutenablingthenextpartofthestatement(<<t2)toalsousecout.109ostream&operator<<(ostream&os,consttime24&t)10.6Overloading<<and>>(重載運(yùn)算符<<和>>)121//Non-memberoverloaded>>operator.122istream&operator>>(istream&is,time24&t)123{124//Inputatime24objectdataintheformath:m:s.125inth,m,s;126do127{128is>>h;129}130while(h<0||h>23);131//Ignoretheseparator.132is.ignore(1);133do134{135is>>m;}137while(m<0||m>60);138//Ignoretheseparator.139is.ignore(1);140do141{142is>>s;143}144while(s<0||s>60);145t.set_time(h,m,s);146returnis;147}10.6Overloading<<and>>(重載運(yùn)算符<<和>>)149main()150{151time24t1(1,2,3);152time24t2(10,10,10);153154cout<<t1<<t2;155156time24t3;157cin>>t3;158cout<<t3;159}01:02:0310:10:104:5:604:05:0610.7Conversionoperators(轉(zhuǎn)換運(yùn)算符)Aconversionoperatorfunctionisusedtoconvertfromaclassobjecttoabuilt-indatatypeortoanotherclassobject.Theconversionoperatormemberfunctionhasthesamenameasthedatatypetowhichtheclassistobeconverted.Conversionoperatorsdifferfromotheroverloadedoperatorsintwoways.Firstly,aconversionoperatortakesnoarguments.Secondly,aconversionoperatorhasnoreturnvalue,notevenvoid.Thereturnvaluefromaconversionoperatorisinferredfromtheoperator’sname.Forexample,toconvertatime24objecttoanintaconversionoperatornamedoperatorintisdefinedintheclass.10.7Conversionoperators(轉(zhuǎn)換運(yùn)算符)1//ProgramExampleP10H2//Demonstrationofaclassconversionoperator.…7classtime24//Asimple24hourtimeclass.8{9public:10time24(inth=0,intm=0,ints=0);11voidset_time(inth,intm,ints);12voidget_time(int&h,int&m,int&s)const;13time24operator+(int
secs)const;14time24operator+(consttime24&t)const;15time24&operator++();//prefix.16time24operator++(int);//postfix.17booloperator==(consttime24&t)const;18operatorint();19private:20inthours;//0to2321intminutes;//0to5922intseconds;//0to5923};10.7Conversionoperators(轉(zhuǎn)換運(yùn)算符)101time24::operatorint()102{103int
no_of_seconds=hours*3600+minutes*60+seconds;104returnno_of_seconds;105}Time=01:02:03Equivalentnumberofseconds=3723151main()152{153time24t(1,2,3);154ints;155156s=t;//Conversionfromatime24datatypetoanintdatatype.157cout<<"Time="<<t158<<"Equivalentnumberofseconds="<<s<<endl;159}10.8Useoffriendfunctions(使用友元函數(shù))Thevaluesoftheprivatemembersofclassareaccessedusingtheclassmemberfunction.hours,minutesandsecondsofthetime24classareaccessedusingtheclassmemberfunctionget_time()andset_time().Toaccesstheprivatedatamembersofaclassdirectlyinanon-memberfunction,thefunctionmustbedeclaredtobeafriendoftheclass.10.8Useoffriendfunctions(使用友元函數(shù))Todeclarethenon-memberfunctionsasfriendsoftheclass,thefollowingdeclarationsareinsertedintheclass.friend返回類型operator運(yùn)算符(形參表)Thesedeclarationscanbeplacedanywhereintheclass,buttheyareusuallyplacedinthepublicsection
10.8Useoffriendfunctions(使用友元函數(shù))Friendfunctionsoverrideabasicprincipleofobject-orientedprogramming-thatofdatahiding.Friendsofaclasshaveaccesstoalltheprivatedataofaclassandtheiruseshouldbeminimisedwherepossible.
10.9Overloadingtheassignmentoperator=
(重載賦值運(yùn)算符=)10.9.1Aclasswithapointerdatamember10.9.2Assigningoneobjecttoanother10.9.1AclasswithapointerdatamemberConsideraclassforrecordingthetransactionsmadebyabankcustomer.Tosimplifymatters,theclassprivatedatamembersarethebankaccountnumber,thenumberoftransactionsmadeandtheamountofeachtransaction.Eachbankaccountwillhaveadifferentnumberoftransactions,butthemaximumnumberoftransactionsissettoonehundred.Eachtransactionsobjecthasstorageforahundredfloating-pointtransactionvalues.10.9.1Aclasswithapointerdatamemberclasstransactions{public:transactions();//Constructor....private:
int
account_number;
int
number_of_transactions;floattransaction_amounts[100];};Iftherearenotransactionsorahundredtransactions,thearraysizeisfixedat100.Thiswouldbeaveryinefficientforalargenumberofcustomersandwouldrestrictthenumberoftransactionsto100.Itwouldbebettertousedynamicmemoryallocationtoallocatetheexactstoragerequiredbyeachaccount.Thisrequiresaclasstohaveapointerdatamemberandisdemonstratedinthenextprogram.1//ProgramExampleP10I2//Demonstrationofaclasswithapointerdatamember.3#include<iostream>4usingnamespacestd;56classtransactions//Aclasscontainingapointerdatamember.7{8public:9transactions(int
ac_num,int
num_transactions,10constfloattransaction_values[]);11//Purpose:Constructor.12~transactions();13//Purpose:Destructor.14voiddisplay()const;15//Purpose:Displaytransactions.16private:17int
account_number;18int
number_of_transactions;19float*transaction_amounts;//Pointertoarrayoftransactions.20};10.9.1Aclasswithapointerdatamember22//Constructor.23transactions::transactions(int
ac_num,int
num_transactions,24constfloattransaction_values[])25{26account_number=ac_num;27number_of_transactions=num_transactions;28//Allocatestorageforthetransactions.29transaction_amounts=newfloat[number_of_transactions];30for(inti=0;i<number_of_transactions;i++)31transaction_amounts[i]=transaction_values[i];32}Theexactstoragerequiredforthetransactionamountsisallocatedusingnewintheconstructoronline29.10.9.1Aclasswithapointerdatamember34//Destructor.35transactions::~transactions()36{37delete[]transaction_amounts;38}39Lines35to38isaspecialclassmemberfunctioncalledadestructor.Adestructoralwayshasthesamenameastheclassitself,butisprecededwithatilde(~).line37freesthememoryallocatedbytheconstructoronline29.Justasaclassconstructoriscalledautomaticallywhenaclassobjectiscreated,aclassdestructoriscalledautomaticallyjustbeforetheclassobjectisdestroyed.Adestructorhasnoparametersandnoreturntype.10.9.1AclasswithapointerdatamemberLine53createsatransactionsobjectwithaccountnumber1andfivetransactions.40voidtransactions::display()const41{42cout<<"AccountNumber"<<account_number<<"Transactions:";43for(inti=0;i<number_of_transactions;i++)44cout<<transaction_amounts[i]<<'';45cout<<endl;46}4748main()49{50//Constructatransactionsobject51//withaccountnumber1and5transactions.52floattrans_amounts1[]={25.67,6.12,19.86,23.41,1.21};53transactionstrans_object1(1,5,trans_amounts1);54trans_object1.display();55}10.9.2AssigningoneobjecttoanotherWhenassigningoneobjecttoanotherobjectofthesametype,member-wiseassignmentisperformedbydefault.Forexample,ift1andt2aretime24objectsthenthestatementhastheeffectofcopyingeachmemberonebyoneasin:Thedefaultassignmentoperatorworkswellformostclasses,butfailstoworkproperlyforclassesthatallocatememoryforoneormoreofitsdatamembers.t1=t2;t1.hours=t2.hours;t1.minutes=t2.minutes;t1.seconds=t2.seconds;10.9.2Assigningoneobjecttoanother1//ProgramExampleP10J2//Programtodemonstratethedefaultassignmentoperatorproblem3//foraclasswithapointerdatamember.4#include<iostream>5usingnamespacestd;67classtransactions//Aclasscontainingapointerdatamember.8{9public:10transactions(int
ac_num,int
num_transactions,11constfloattransaction_values[]);12//Purpose:Constructor.13~transactions();14//Purpose:Destructor.15voidmodify_transaction(int
transaction_index,floatamount);16//Purpose:Modifyatransactionamount.17//Arguments:transaction_index-transactiontochange.18//amount-newtransactionamount.19voiddisplay()const;20//Purpose:Displaytransactions.10.9.2Assigningoneobjecttoanother21private:22int
account_number;23int
number_of_transactions;24float*transaction_amounts;//Pointertoarrayoftransactions.25};2627//Constructor.28transactions::transactions(int
ac_num,int
num_transactions,29constfloattransaction_values[])30{31account_number=ac_num;32number_of_transactions=num_transactions;33//Allocatestorageforthetransactions.34transaction_amounts=n
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 2026年監(jiān)理工程師之交通工程目標(biāo)控制考試題庫300道附完整答案(易錯(cuò)題)
- 申通地鐵副經(jīng)理面試題及答案解析
- 2026年山東管理學(xué)院輔導(dǎo)員招聘?jìng)淇碱}庫附答案
- 2026福建三明市建寧縣公開招聘緊缺急需專業(yè)教師19人考試筆試備考試題及答案解析
- 2025年山東?。?67所)輔導(dǎo)員招聘?jìng)淇碱}庫附答案
- 2026年消防設(shè)施操作員之消防設(shè)備高級(jí)技能考試題庫300道及參考答案
- 2026年中級(jí)注冊(cè)安全工程師之安全實(shí)務(wù)化工安全考試題庫300道及答案(有一套)
- 2026河南儲(chǔ)備糧管理集團(tuán)招聘面試題及答案
- 工程維修合同范本
- 2026年徽商職業(yè)學(xué)院?jiǎn)握新殬I(yè)適應(yīng)性考試必刷測(cè)試卷含答案
- 支原體抗體診斷培訓(xùn)
- 三通、大小頭面積計(jì)算公式
- 軟件無線電原理與應(yīng)用(第3版)-習(xí)題及答案匯總 第1-9章 虛擬人-軟件無線電的新發(fā)展 認(rèn)知無線電
- 中級(jí)會(huì)計(jì)實(shí)務(wù)-存貨
- 機(jī)械電氣設(shè)備管理制度
- 簡(jiǎn)單酒水購銷合同
- GB/T 41933-2022塑料拉-拉疲勞裂紋擴(kuò)展的測(cè)定線彈性斷裂力學(xué)(LEFM)法
- 高中語文 選修中冊(cè) 第四課時(shí) 展示強(qiáng)大思想力量 邏輯思維在著作中提升-《改造我們的學(xué)習(xí)》《人的正確思想是從哪里來的》
- 大學(xué)化學(xué)試題庫
- GCB發(fā)電機(jī)出口斷路器教育課件
- 柑桔周年管理工作歷第二版課件
評(píng)論
0/150
提交評(píng)論