資源描述:
《C++程序代碼示例3》由會員上傳分享,免費(fèi)在線閱讀,更多相關(guān)內(nèi)容在教育資源-天天文庫。
1、LabExercise—PolymorphicBankingILabObjectivesInthislab,youwillpractice:1)CreatinganAccountbaseclassthatcontainsvirtualfunctionsandderivedclassesSavingsAccountandCheckingAccount.2)Definingvirtualfunctions.3)Callingvirtualfunctions.4)Downcastingwithapointerw
2、iththedynamic_castoperator.IIDescriptionoftheProblemDevelopapolymorphicbankingprogramusingtheAccounthierarchycreatedinExercise12.10.CreateavectorofAccountpointerstoSavingsAccountandCheckingAccountobjects.ForeachAccountinthevector,allowtheusertospecifyanam
3、ountofmoneytowithdrawfromtheAccountusingmemberfunctiondebitandanamountofmoneytodepositintotheAccountusingmemberfunctioncredit.AsyouprocesseachAccount,determineitstype.IfanAccountisaSavingsAccount,calculatetheamountofinterestowedtotheAccountusingmemberfunc
4、tioncalculateInterest,thenaddtheinteresttotheaccountbalanceusingmemberfunctioncredit.AfterprocessinganAccount,printtheupdatedaccountbalanceobtainedbyinvokingbaseclassmemberfunctiongetBalance.IIISampleOutputIVProblem-SolvingTips1.Toachievepolymorphism,decl
5、arethefunctionsthatshouldbecalledpolymorphicallyasvirtual.Toindicateavirtualfunctionwithinaclassdefinition,add“virtual”beforethefunctionprototype.Whenthevirtualfunctionsareredefinedinaderivedclass,thosememberfunctionprototypesshouldalsobeprecededbythekeyw
6、ordvirtualasagoodprogrammingpractice.2.TodetermineifapointertoanAccountobjectisactuallypointingtoaSavingsAccountobject,downcastittoaSavingsAccount*usingthedynamic_castoperator.Ifthepointerreturnedbythisoperationisnotthenullpointer(i.e.,0)thentheobjectisaS
7、avingsAccountobjectandthatpointercanbeusedtoaccessmembersuniquetoclassSavingsAccount.3.Rememberthatyourcompilermayrequireyoutoenablerun-timetypeinformation(RTTI)forthisparticularprojectbeforethisprogramwillruncorrectly.VYourSolution#ifndefCHECKING_H#defin
8、eCHECKING_H#include"Account.h"classCheckingAccount:publicAccount{public:CheckingAccount(double,double);private:doubletransactionFee;voidchargeFee();};#ifndefACCOUNT_H#defineACCOUNT_HclassAccount{public:Account(doubl