您好,欢迎访问三七文档
(Account继承层次)创建一个银行账户的继承层次,表示银行的所有客户账户。所有的客户都能在他们的银行账户存钱,取钱,但是账户也可以分成更具体的类型。例如,一方面存款账户SavingAccount依靠存款生利,另一方面,支票账户CheckingAccount对每笔交易(即存款或取款)收取费用。创建一个类层次,以Account作为基类,SavingAccount和CheckingAccount作为派生类。基类Account应该包括一个double类型的数据成员balance,表示账户的余额。该类应提供一个构造函数,接受一个初始余额值并用它初始化数据成员balance。而且构造函数确认初始余额的有效性,保证它大于等于0。如果小于0,则需将其置为0,并显示出错信息,表明该初始化余额是一个无效的值。该类应当提供三个成员函数。成员函数credit可以向当前余额加钱;成员函数debit负责从账户中取钱,并且保证账户不会被透支。如果提取金额大于账户金额,函数将保持balance不变,并打印信息“Debitamountexceededaccountbalance”;成员函数getBalance则返回当前balance的值派生类SavingAccount不仅继承了基类Account的功能,而且还应提供一个附加的double类型数据成员interestrate表示这个账户的比率(百分比)。SavingAccount的构造函数应接受初始余额值和初始利率值,还应提供一个public成员函数caclculateInterest,返回代表账户的利息的一个double值,这个值是balance和interestrate的乘积。注意:类SavingAccount应继承成员函数credit和debit,不需要重新定义。派生类CheckingAccount不仅继承了基类Account的功能,还应提供一个附加的double类型数据成员表示每笔交易的费用。CheckingAccount的构造函数应接受初始余额值和交易费用值。类CheckingAccount需要重新定义成员函数credit和debit,当每笔交易完成时,从balance中减去每笔交易的费用。重新定义这些函数时应用(即调用)基类Account的这两个函数来执行账户余额的更新。CheckingAccount的debit函数只有当钱被成功提取时(即提取金额不超过账户余额时)才应收取交易费。提示:定义Account的debit函数使它返回一个bool类型值,表示钱是否被成功提取。然后利用该值决定是否需要扣除交易费。当这个层次中的类定义完毕后,编写一个程序,要求创建每个类的对象并测试他们的成员函数。将利息加到SavingAccount对象的方法是:先调用它的成员函数caclculateInterest,然后将返回的利息数传递给该对象的credit值。Account.h#ifndefACCOUNT_H#defineACCOUNT_HclassAccount{public:Account(double=0);boolcredit(double);booldebit(double);doublegetBalance(){returnbalance;}private:doublebalance;};#endifAccount.cpp#includeiostreamusingstd::endl;usingstd::cout;#includeAccount.hAccount::Account(doubleYuE){if(YuE=0)balance=YuE;else{balance=0;coutUnvalidinput!Balanceissettedto0!endl;//不合要求输出提示信息}}boolAccount::credit(doubledeposit)//存钱{coutyouarecreditingorgetinterestdeposityuanendl;if(deposit=0){balance+=deposit;returntrue;}else{coutWrongdeposit!Youcan'tdepositlessthan0yuan!;//不合要求输出提示信息returnfalse;}}boolAccount::debit(doublewithdraw)//取钱{coutyouaredebitingorpayingthefeechargedforthistransactionwithdrawyuanendl;if(withdraw=0&&withdraw=balance){balance-=withdraw;returntrue;}else{coutDebitamountexceededaccountbalance,oryouwronglywithdrawlessthan0yuan!;//不合要求输出提示信息returnfalse;}}SavingAccount.h#ifndefSAVINGACCOUNT_H#defineSAVINGACCOUNT_H#includeAccount.hclassSavingAccount:publicAccount{public:SavingAccount(double=0,double=0);doublecalculateInterest()//计算利息{returninterestrate*getBalance();}private:doubleinterestrate;};#endifSavingAccount.cpp#includeiostreamusingstd::endl;usingstd::cout;#includeSavingAccount.hSavingAccount::SavingAccount(doubleYuE,doubleLiLv):Account(YuE)//为基类中的数据成员初始化{if(LiLv=0&&LiLv=1)interestrate=LiLv;else{interestrate=0;coutUnvalidinput!Interestrateissettedto0!endl;//不合要求输出提示信息}coutsavinginterestrateisinterestrateendl;}CheckingAccount.h#includeiostreamusingstd::endl;usingstd::cout;#includeCheckingAccount.hCheckingAccount::CheckingAccount(doubleYuE,doubleJiaoYiFei)//构造函数初始化余额和交易额:Account(YuE){if(JiaoYiFei=0)feechargedpertransaction=JiaoYiFei;else{feechargedpertransaction=0;coutUnvalidinput!Feechargedpertransactionissettedto0!endl;//不合要求输出提示信息}coutfeechargedforpertransactionisfeechargedpertransactionendl;}voidCheckingAccount::credit(doubledeposit)//存钱交易{if(Account::credit(deposit)==true)//收取交易费相当于取钱Account::debit(feechargedpertransaction);elsecoutTradeFaided!endl;}voidCheckingAccount::debit(doublewithdraw)//取钱交易{if(Account::debit(withdraw)==true)//同理收取交易费相当于取钱Account::debit(feechargedpertransaction);elsecoutTradeFaided!endl;}test_Account.cpp#includeiostreamusingstd::endl;usingstd::cout;#includeAccount.h#includeSavingAccount.h#includeCheckingAccount.hintmain(){Accountaccount(2000.0);SavingAccountsaving(1500,0.05);CheckingAccountchecking(5000,2);//测试基类对象cout\nAccountbeforoperateisaccount.getBalance()endl;account.credit(222);coutAccountaftercreditisaccount.getBalance()endl;account.debit(2500);coutAccountafterdebitisaccount.getBalance()endl;//测试存款账户对象cout\nSavingAccountbeforoperateissaving.getBalance()endl;saving.credit(saving.calculateInterest());//将利息加到余额上利用存款coutSavingAccountaftercreditissaving.getBalance()endl;saving.debit(200);coutSavingAccountafterdebitissaving.getBalance()endl;//测试支票账户cout\nCheckingAccountbeforoperateischecking.getBalance()endl;checking.credit(600);coutCheckingAccountaftercreditischecking.getBalance()endl;checking.debit(6000);coutCheckingAccountafterdebitischecking.getBalance()endl;return0;}
本文标题:C++作业8
链接地址:https://www.777doc.com/doc-4702340 .html