




已阅读5页,还剩4页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
使用习题12.10中创建的Account类层次结构开发一个具有多态性的银行系统程序。创建一个Account指针的vector对象,其中的指针指向SavingsAccount 对象和CheckingAccount对象。对于该vector对象中每个Account ,允许用户使用成员函数debit指定要从Account取出的货币金额,并允许用户使用成员函数credit指定要存入该Account的货币金额。处理每个Account时,应判定它的类型。如果Account是SavingsAccount,就使用成员函数,就使用成员函数calculateInterest计算该Account应得的利息,然后使用成员函数credit把利息加到帐户余额上。处理完一个Account后,通过调用基类成员函数getBalance打印更新后的帐户余额。Account.h#ifndef ACCOUNT_H#define ACCOUNT_Hclass Accountpublic:Account( double = 0 );virtual bool credit( double );virtual bool debit( double );virtual 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#ifndef CHECKINGACCOUNT_H#define CHECKINGACCOUNT_H#include Account.hclass CheckingAccount:public Accountpublic:CheckingAccount( double = 0, double = 0 );virtual bool credit( double );virtual bool debit( double );private:double feechargedpertransaction;#endifCheckingAccount.cpp#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;bool CheckingAccount:credit( double deposit ) / 存钱交易if( Account:credit( deposit ) = true ) / 收取交易费相当于取钱Account:debit( feechargedpertransaction );return true;elsecout Trade Faided! endl;return false;bool CheckingAccount:debit( double withdraw ) / 取钱交易if ( Account:debit( withdraw ) = true )Account:debit( feechargedpertransaction ); / 同理 收取交易费相当于取钱return true;elsecout Trade Faided! endl;return false;test_Account.cpp#include using std:endl;using std:cout;using std:cin;#include using std:vector;#include #include Account.h#include SavingAccount.h#include CheckingAccount.hvoid virtualViaPointer( Account * const, SavingAccount &, CheckingAccount & );/用户通过此自定义函数进行交易int main()/初始化各个账户Account account( 2000.0 ); SavingAccount saving( 2000,0.1);CheckingAccount checking( 5000, 2 );vector accounts(3);/用vector指针指向各个账户accounts0 = &account;accounts1 = &saving;accounts2 = &checking;for(int i = 0; i 3; i+) /每个账户轮流执行交易virtualViaPointer( accountsi, saving, checking );return 0;void virtualViaPointer( Account * const baseClassPtr , SavingAccount & s, CheckingAccount & c)int t = 1, n;/通过比较是否指向同一个地址判断账户的类型是否相同if( baseClassPtr = &s )cout nis SavingAccountn;cout SavingAccount before operation is getBalance() endl;else if( baseClassPtr = &c )cout nis CheckingAccountn;cout CheckingAccount before operation is getBalance() endl;elsecout nis Accountn;cout Account before operation is getBalance() endl;while( t = 1 | t = 2 )cout n请选择服务:1:存款 2:取款 其他键:退卡. t;switch(t)case 1: cout n;cout credit( n );if( baseClassPtr = &s )SavingAccount *savingPtr = (SavingAccount*)baseClassPtr;/向下强制类型转换 来调用派生类中有而基类没有的函数baseClassPtr-credit( savingPtr-calculateInterest() );break;case 2: cout n;cout debit( n )
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 泛函分析期末考试题库及答案
- 邮储考试题库及答案
- 厦门市保安考试题库及答案
- 妇幼保健学考试题库及答案
- 文职厨师考试题库及答案
- 教师资格证考试题库及答案幼儿园
- 外包业务承揽合同书
- 2025年新疆人防工程防护设备安装合同书模板
- 2025年广西梧州市公安辅警招聘知识考试题(含答案)
- 实验室仪器设备供应合同协议内容
- 万科郡西别墅课件
- 食品有限公司制冷机安全风险分级管控清单
- 西南科技大学833材料科学基础2016-2022年考研初试真题
- 香港注册社会工作者工作守则
- GB/T 15115-1994压铸铝合金
- GB/T 12357.1-2004通信用多模光纤第1部分:A1类多模光纤特性
- 胸外科围手术期呼吸功能锻炼的意义培训课件
- 朗读技巧与朗读教学课件
- (新版)海南自由贸易港建设总体方案考试题库(含答案)
- 丰田汽车新产品开发及项目管理
- 人教版新教材高中英语选择性必修一全册课文及翻译(中英word)
评论
0/150
提交评论