欢迎来到天天文库
浏览记录
ID:22496978
大小:99.92 KB
页数:9页
时间:2018-10-29
《具有多态性的银行系统程序c》由会员上传分享,免费在线阅读,更多相关内容在学术论文-天天文库。
1、使用习题12.10屮创建的Account类层次结构开发一个具有多态性的银行系统程序。创建一个Account指针的vector对象,其中的指针指向SavingsAccount对象和CheckingAccount对象。对于该vector对象中每个Account,允许用户使用成员函数debit指定要从Account収出的货币金额,并允许川户使川成员函数credit指定要存入该Account的货币金额。处理每个Account时,应判定它的类型。如果Account是SavingsAccount,就使用成员两数,就使用成员函数calculatel
2、nterest计算该Account应得的利息,然后使用成员函数credit把利息加到帐广余额上。处理完一个Account后,通过调用基类成员函数getBalance打印吏新后的帐户余额。Account.h#ifndefACCOUNT一H#defineACCOUNT_HclassAccount{public:Account(double=0);virtualboolcredit(double);virtualbooldebit(double);virtualdoublegetBalance(){returnbalance;}privat
3、e:doublebalance;#endifAccount.cpp#include〈iostream〉usingstd::endl;usingstd::cout;#include’’Account.h’Account::Account(doubleYuE){if(YuE>=0)balance=YuE;elsebalance=0;cout«"Unvalidinput!Balanceissettedto0!"«endl;//不合要求输出提示信息}}boolAccount::credit(doubledeposit)//存钱{cout«"y
4、ouarecreditingorgetinterest"«deposit«"yuan’1«endl;if(deposit>=0){balance+=deposit;returntrue;}else{cout«"Wrongdeposit!Youcan’tdepositlessthan0yuan!M;//不合要求输出提示信息returnfalse;}}boolAccount::debit(doublewithdraw)//取饯{cout«"youaredebitingorpayingthefeechargedforthistransact
5、ion•’«withdraw«”yuan”《endl;if(withdraw>=0&&withdraw<=balance){balance-=withdraw;returntrue;}else{cout«nDebitamountexceededaccountbalance,oryouwronglywithdrawlessthan0yuan!";//不合要求输出提示信息returnfalse;}}SavingAccount.h#ifndefSAVINGACCOUNT.H#defineSAVINGACCOUNTH#include"Acco
6、unt.hclassSavingAccount:publicAccount{public:SavingAccount(double=0,double=0);doublecalculatelntere$t()//计算利息{returninterestrate*getBalance();}private:doubleinterestrate;};#endifSavingAccount.cpp^include〈iostream〉usingstd::endl;using$td::cout;#include"SavingAccount.h”Sa
7、vingAccount::SavingAccount(doubleYuE,doubleLiLv):Account(YuE)//为基类中的数掘成员初始化{if(LiLv>=0&&LiLv<=1)interestrate=LiLv;else{interestrate=0;cout«"Unvalidinput!Interestrateissettedto0!’’《endl;//不合要求输出提示信息}cout«MsavinginterestrateisH«interestrate«endl;CheckingAccount.h#ifndefCH
8、ECKINGACCOUNTH#defineCHECKINGACCOUNTH#include’’Account.h”classCheckingAccount:publicAccount{public:CheckingAcc
此文档下载收益归作者所有