全文预览已结束
下载本文档
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
Lab Exercise Polymorphic BankingI Lab ObjectivesIn this lab, you will practice:1) Creating an Account base class that contains virtual functions and derived classes SavingsAccount and CheckingAccount.2) Defining virtual functions.3) Calling virtual functions.4) Downcasting with a pointer with the dynamic_cast operator.II Description of the Problem Develop a polymorphic banking program using the Account hierarchy created in Exercise 12.10. Create a vector of Account pointers to SavingsAccount and CheckingAccount objects. For each Account in the vector, allow the user to specify an amount of money to withdraw from the Account using member function debit and an amount of money to deposit into the Account using member function credit. As you process each Account, determine its type. If an Account is a SavingsAccount, calculate the amount of interest owed to the Account using member function calculateInterest, then add the interest to the account balance using member function credit. After processing an Account, print the updated account balance obtained by invoking base class member function getBalance. III Sample OutputIV Problem-Solving Tips1. To achieve polymorphism, declare the functions that should be called polymorphically as virtual. To indicate a virtual function within a class definition, add “virtual” before the function prototype. When the virtual functions are redefined in a derived class, those member function prototypes should also be preceded by the keyword virtual as a good programming practice.2. To determine if a pointer to an Account object is actually pointing to a SavingsAccount object, downcast it to a SavingsAccount * using the dynamic_cast operator. If the pointer returned by this operation is not the null pointer (i.e., 0) then the object is a SavingsAccount object and that pointer can be used to access members unique to class SavingsAccount.3. Remember that your compiler may require you to enable run-time type information (RTTI) for this particular project before this program will run correctly.V Your Solution#ifndef CHECKING_H#define CHECKING_H#include Account.hclass CheckingAccount : public Accountpublic: CheckingAccount( double, double );private: double transactionFee; void chargeFee(); #ifndef ACCOUNT_H#define ACCOUNT_Hclass Accountpublic: Account( double ); void setBalance( double ); double getBalance(); private: double balance; ; #ifndef SAVINGS_H#define SAVINGS_H#include Account.h class SavingsAccount : public Accountpublic: SavingsAccount( double, double ); double calculateInterest();private: double interestRate; ;#endif#include using std:cout;using std:endl;#include Account.h Account:Account( double initialBalance ) if ( initialBalance = 0.0 ) balance = initialBalance; else cout Error: Initial balance cannot be negative. balance ) cout Debit amount exceeded account balance. endl; return false; else / debit amount does not exceed balance balance = balance - amount; return true; void Account:setBalance( double newBalance ) balance = newBalance; double Account:getBalance() return balance;#include using std:cout;using std:endl;#include CheckingAccount.h CheckingAccount:CheckingAccount( double initialBalance, double fee ) : Account( initialBalance ) transactionFee = ( fee 0.0 ) ? 0.0 : fee; void CheckingAccount:credit( double amount ) Account:credit( amount ); chargeFee(); bool CheckingAccount:debit( double amount ) bool success = Account:debit( amount ); if ( success ) chargeFee(); return true; else return false; void CheckingAccount:chargeFee() Account:setBalance( getBalance() - transactionFee ); cout $ transactionFee transaction fee charged. endl;#include using std:cout;using std:cin;using std:endl;#include using std:setprecision;using std:fixed;#include using std:vector;#include Account.h / Account class definition#include SavingsAccount.h#include CheckingAccount.h int main() accounts 0 = new SavingsAccount( 25.0, .03 ); accounts 1 = new CheckingAccount( 80.0, 1.0 ); accounts 2 = new SavingsAccount( 200.0, .015 ); accounts 3 = new CheckingAccount( 400.0, .5 ); cout fixed setprecision( 2 ); for ( size_t i = 0; i accounts.size(); i+ ) cout Account i + 1 balance: $ /* Call the getBalance function through Account pointer i */; double withdrawalAmount = 0.0; cout nEnter an amount to withdraw from Account i + 1 withdrawalAmount; double depositAmount = 0.0; cout Enter an amount to deposit into Account i + 1 depositAmount; if (savingsAccountPtr isnt 0) double interestEarned = cout Adding $ interestEarned interest to Account i + 1 (a SavingsAccount) endl; cout Updated Account i + 1 balance: $ /* Call the getBalance function through Account pointer i */ nn; retur
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 事业单位联考《综合应用能力》试题及答案解析(A类)
- 2025年安全员B证考试试题学生专用附答案详解
- 2025年安全员B证考试试题一(预热题)附答案详解
- 2025年安全员B证考试试题一附答案详解ab卷
- 安全教育课标题课件
- 组训自我介绍
- 中专毕业生自我鉴定一千字
- 广西高职单招职业适应性测试考前备考题库及答案解析
- 徐州生物工程职业技术学院单招面试题库及答案解析
- 心肺复苏试题及答案50题
- 基层支部联建活动方案
- 高电压与绝缘技术论文
- 2024DBJ33T1095-预拌砂浆应用技术规程
- 5、二级供应商品质管理
- 2025年江西省交通投资集团有限责任公司社会招聘43人笔试备考题库及答案详解1套
- 2025年上海高三物理高考重点知识点归纳总结(复习必背)
- 医院医学影像科科室年终总结
- 高一家长会化学教师课件
- 2025年危险化学品安全考试题库(危险化学品安全操作规范修订与培训效果)试题
- 2025年中国电脑CPU散热器市场调查研究报告
- 2025年下半年海南省药品监督管理局直属单位招聘事业单位人员易考易错模拟试题(共500题)试卷后附参考答案
评论
0/150
提交评论