C++作业.doc_第1页
C++作业.doc_第2页
C++作业.doc_第3页
C++作业.doc_第4页
C++作业.doc_第5页
免费预览已结束,剩余1页可下载查看

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

.(Account继承层次)创建一个银行账户的继承层次,表示银行的所有客户账户。所有的客户都能在他们的银行账户存钱,取钱,但是账户也可以分成更具体的类型。例如,一方面存款账户SavingAccount依靠存款生利,另一方面,支票账户CheckingAccount对每笔交易(即存款或取款)收取费用。创建一个类层次,以Account作为基类, SavingAccount和CheckingAccount作为派生类。基类Account应该包括一个double类型的数据成员balance,表示账户的余额。该类应提供一个构造函数,接受一个初始余额值并用它初始化数据成员balance。而且构造函数确认初始余额的有效性,保证它大于等于0。如果小于0,则需将其置为0,并显示出错信息,表明该初始化余额是一个无效的值。该类应当提供三个成员函数。成员函数credit可以向当前余额加钱;成员函数debit负责从账户中取钱,并且保证账户不会被透支。如果提取金额大于账户金额,函数将保持balance不变,并打印信息“Debit amount exceeded account balance”;成员函数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#ifndef ACCOUNT_H#define ACCOUNT_Hclass Accountpublic:Account( double = 0 );bool credit( double );bool debit( double );double getBalance()return balance;private:double balance;#endifAccount.cpp#include using std:endl;using std:cout;#include Account.hAccount:Account( double YuE )if( YuE = 0 )balance = YuE;elsebalance = 0;cout Unvalid input! Balance is setted to 0! endl; / 不合要求输出提示信息bool Account:credit( double deposit ) / 存钱cout you are crediting or get interest deposit yuan = 0 )balance += deposit;return true;elsecout Wrong deposit! You cant deposit less than 0 yuan! ; / 不合要求输出提示信息return false;bool Account:debit( double withdraw ) / 取钱cout you are debiting or paying the fee charged for this transaction withdraw yuan = 0 & withdraw = balance )balance -= withdraw;return true;elsecout Debit amount exceeded account balance, or you wrongly withdraw less than 0 yuan!; / 不合要求输出提示信息return false;SavingAccount.h#ifndef SAVINGACCOUNT_H#define SAVINGACCOUNT_H#include Account.hclass SavingAccount:public Accountpublic:SavingAccount( double = 0, double = 0 );double calculateInterest() / 计算利息return interestrate * getBalance();private:double interestrate;#endifSavingAccount.cpp#include using std:endl;using std:cout;#include SavingAccount.hSavingAccount:SavingAccount( double YuE, double LiLv ):Account( YuE ) / 为基类中的数据成员初始化if( LiLv = 0 & LiLv = 1 )interestrate = LiLv;elseinterestrate = 0;cout Unvalid input! Interestrate is setted to 0! endl; / 不合要求输出提示信息cout saving interestrate is interestrate endl;CheckingAccount.h#include using std:endl;using std:cout;#include CheckingAccount.hCheckingAccount:CheckingAccount( double YuE, double JiaoYiFei ) / 构造函数初始化余额和交易额:Account( YuE )if( JiaoYiFei = 0 )feechargedpertransaction = JiaoYiFei;elsefeechargedpertransaction = 0;cout Unvalid input! Feechargedpertransaction is setted to 0! endl; / 不合要求输出提示信息cout fee charged for per transaction is feechargedpertransaction endl;void CheckingAccount:credit( double deposit ) / 存钱交易if( Account:credit( deposit ) = true ) / 收取交易费相当于取钱Account:debit( feechargedpertransaction );elsecout Trade Faided! endl;void CheckingAccount:debit( double withdraw ) / 取钱交易if ( Account:debit( withdraw ) = true ) / 同理 收取交易费相当于取钱Account:debit( feechargedpertransaction );elsecout Trade Faided! endl;test_Account.cpp#include using std:endl;using std:cout;#include Account.h#include SavingAccount.h#include CheckingAccount.hint main()Account account( 2000.0 );SavingAccount saving( 1500, 0.05 );CheckingAccount checking( 5000, 2 );/测试基类对象cout nAccount befor operate is account.getBalance() endl;account.credit( 222 );cout Account after credit is account.getBalance() endl;account.debit( 2500 );cout Account after debit is account.getBalance() endl;/测试存款账户对象cout nSavingAccount befor operate is saving.getBalance() endl;saving.credit( saving.calculateInterest() );/ 将利息加到余额上利用存款cout SavingAccount after credit is saving.getBalance() endl;saving.debit( 200 );cout SavingAccount after debit is saving.getBalance() endl;/测试支票账户cout nCheckingAccount befor operate is chec

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论