欢迎来到天天文库
浏览记录
ID:40525561
大小:144.00 KB
页数:5页
时间:2019-08-04
《C++程序代码示例3》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
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
此文档下载收益归作者所有