• Document: Lab Exercise Polymorphic Banking
  • Size: 187.83 KB
  • Uploaded: 2019-02-12 23:16:22
  • Status: Successfully converted


Some snippets from your converted document:

Chapter 13 Object-Oriented Programming: Polymorphism 19 Lab Exercises Lab Exercise — Polymorphic Banking Name: Date: Section: This problem is intended to be solved in a closed-lab session with a teaching assistant or instructor present. The problem is divided into five parts: 1. Lab Objectives 2. Description of the Problem 3. Sample Output 4. Program Template (Fig. L 13.2–Fig. L 13.8) 5. Problem-Solving Tips The program template represents a complete working C++ program, with one or more key lines of code replaced with comments. Read the problem description and examine the sample output; then study the template code. Using the problem-solving tips as a guide, replace the /* */ comments with C++ code. Compile and execute the program. Compare your output with the sample output provided. The source code for the template is available at www.deitel.com/books/cpphtp6/. [Note: You must be a registered user and logged in at www.deitel.com to download this code. Registration is quick and easy.] Lab Objectives This lab was designed to reinforce programming concepts from Chapter 13 of C++ How To Program: Sixth Edi- tion. In this lab, you will practice: • Creating an Account base class that contains virtual functions and derived classes SavingsAccount and CheckingAccount. • Defining virtual functions. • Calling virtual functions. • Downcasting with a pointer with the dynamic_cast operator. 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 func- tion credit. After processing an Account, print the updated account balance obtained by invoking base class member function getBalance. © 2008 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved. 20 Object-Oriented Programming: Polymorphism Chapter13 Lab Exercises Name: Lab Exercise — Polymorphic Banking Sample Output Account 1 balance: $25.00 Enter an amount to withdraw from Account 1: 15.00 Enter an amount to deposit into Account 1: 10.50 Adding $0.61 interest to Account 1 (a SavingsAccount) Updated Account 1 balance: $21.11 Account 2 balance: $80.00 Enter an amount to withdraw from Account 2: 90.00 Debit amount exceeded account balance. Enter an amount to deposit into Account 2: 45.00 $1.00 transaction fee charged. Updated Account 2 balance: $124.00 Account 3 balance: $200.00 Enter an amount to withdraw from Account 3: 75.50 Enter an amount to deposit into Account 3: 300.00 Adding $6.37 interest to Account 3 (a SavingsAccount) Updated Account 3 balance: $430.87 Account 4 balance: $400.00 Enter an amount to withdraw from Account 4: 56.81 $0.50 transaction fee charged. Enter an amount to deposit into Account 4: 37.83 $0.50 transaction fee charged. Updated Account 4 balance: $380.02 Template 1 // Lab 1: Account.h 2 // Definition of Account class. 3 #ifndef ACCOUNT_H 4 #define ACCOUNT_H 5 6 class Account 7 { 8 public: 9 Account( double ); // constructor initializes balance 10 /* Write a function prototype for virtual function credit */ 11 /* Write a function prototype for virtual function debit */ 12 void setBalance( double ); // sets the account balance 13 double getBalance(); // return the account balance 14 private: 15 double balance; // data member that stores the balance 16 }; // end class Account 17 18 #endif Fig. L 13.2 | Account.h. © 2008 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved. Chapter 13 Object-Oriented Programming: Polymorphism 21 Lab Exercises Name: Lab Exercise — Polymorphic Banking 1 // Lab 1: Account.cpp 2 // Member-function definitions for class Account. 3 #include <iostream> 4 using std::cout; 5 using std::endl; 6 7 #include "Account.h" // include definition of class Account 8 9 // Account constructor initializes data member balance 10 Account::Account( double initialBalance ) 11 { 12 // if initialBalance is gre

Recently converted files (publicly available):