diff --git a/201816040130/4th Lab exercises.docx b/201816040130/4th Lab exercises.docx new file mode 100644 index 0000000..0d1f0e7 Binary files /dev/null and b/201816040130/4th Lab exercises.docx differ diff --git a/201816040130/Account.cpp b/201816040130/Account.cpp new file mode 100644 index 0000000..fa97557 --- /dev/null +++ b/201816040130/Account.cpp @@ -0,0 +1,70 @@ +// Lab 1: Account.cpp +// Member-function definitions for class Account. +#include +using namespace std; + +#include "Account.h" // include definition of class Account + +// Account constructor initializes data member balance +Account::Account( double initialBalance ) +{ + // if initialBalance is greater than or equal to 0.0, set this value + // as the balance of the Account + if ( initialBalance >= 0.0 ) + balance = initialBalance; + else // otherwise, output message and set balance to 0.0 + { + cout << "Error: Initial balance cannot be negative." << endl; + balance = 0.0; + } // end if...else +} // end Account constructor + +// credit (add) an amount to the account balance +void Account::credit( double amount ) +{ + balance = balance + amount; // add amount to balance +} // end function credit + +// debit (subtract) an amount from the account balance +// return bool indicating whether money was debited +bool Account::debit( double amount ) +{ + if ( amount > balance ) // debit amount exceeds balance + { + cout << "Debit amount exceeded account balance." << endl; + return false; + } // end if + else // debit amount does not exceed balance + { + balance = balance - amount; + return true; + } // end else +} // end function debit + +// set the account balance +void Account::setBalance( double newBalance ) +{ + balance = newBalance; +} // end function setBalance + +// return the account balance +double Account::getBalance() +{ + return balance; +} // end function getBalance + + +/************************************************************************** + * (C) Copyright 1992-2012 by Deitel & Associates, Inc. and * + * Pearson Education, Inc. All Rights Reserved. * + * * + * DISCLAIMER: The authors and publisher of this book have used their * + * best efforts in preparing the book. These efforts include the * + * development, research, and testing of the theories and programs * + * to determine their effectiveness. The authors and publisher make * + * no warranty of any kind, expressed or implied, with regard to these * + * programs or to the documentation contained in these books. The authors * + * and publisher shall not be liable in any event for incidental or * + * consequential damages in connection with, or arising out of, the * + * furnishing, performance, or use of these programs. * + **************************************************************************/ diff --git a/201816040130/Account.h b/201816040130/Account.h new file mode 100644 index 0000000..e32c082 --- /dev/null +++ b/201816040130/Account.h @@ -0,0 +1,34 @@ +// Lab 1: Account.h +// Definition of Account class. +#ifndef ACCOUNT_H +#define ACCOUNT_H + +class Account +{ +public: + Account( double ); // constructor initializes balance + virtual void credit(double); + virtual bool debit(double); + void setBalance( double ); // sets the account balance + double getBalance(); // return the account balance +private: + double balance; // data member that stores the balance +}; // end class Account + +#endif + + +/************************************************************************** + * (C) Copyright 1992-2012 by Deitel & Associates, Inc. and * + * Pearson Education, Inc. All Rights Reserved. * + * * + * DISCLAIMER: The authors and publisher of this book have used their * + * best efforts in preparing the book. These efforts include the * + * development, research, and testing of the theories and programs * + * to determine their effectiveness. The authors and publisher make * + * no warranty of any kind, expressed or implied, with regard to these * + * programs or to the documentation contained in these books. The authors * + * and publisher shall not be liable in any event for incidental or * + * consequential damages in connection with, or arising out of, the * + * furnishing, performance, or use of these programs. * + **************************************************************************/ diff --git a/201816040130/CheckingAccount.cpp b/201816040130/CheckingAccount.cpp new file mode 100644 index 0000000..1c04a2a --- /dev/null +++ b/201816040130/CheckingAccount.cpp @@ -0,0 +1,57 @@ +// Lab 1: CheckingAccount.cpp +// Member-function definitions for class CheckingAccount. +#include +using namespace std; + +#include "CheckingAccount.h" // CheckingAccount class definition + +// constructor initializes balance and transaction fee +CheckingAccount::CheckingAccount( double initialBalance, double fee ) + : Account( initialBalance ) // initialize base class +{ + transactionFee = ( fee < 0.0 ) ? 0.0 : fee; // set transaction fee +} // end CheckingAccount constructor + +// credit (add) an amount to the account balance and charge fee +void CheckingAccount::credit( double amount ) +{ + Account::credit( amount ); // always succeeds + chargeFee(); +} // end function credit + +// debit (subtract) an amount from the account balance and charge fee +bool CheckingAccount::debit( double amount ) +{ + bool success = Account::debit( amount ); // attempt to debit + + if ( success ) // if money was debited, charge fee and return true + { + chargeFee(); + return true; + } // end if + else // otherwise, do not charge fee and return false + return false; +} // end function debit + +// subtract transaction fee +void CheckingAccount::chargeFee() +{ + Account::setBalance( getBalance() - transactionFee ); + cout << "$" << transactionFee << " transaction fee charged." << endl; +} // end function chargeFee + + +/************************************************************************** + * (C) Copyright 1992-2012 by Deitel & Associates, Inc. and * + * Pearson Education, Inc. All Rights Reserved. * + * * + * DISCLAIMER: The authors and publisher of this book have used their * + * best efforts in preparing the book. These efforts include the * + * development, research, and testing of the theories and programs * + * to determine their effectiveness. The authors and publisher make * + * no warranty of any kind, expressed or implied, with regard to these * + * programs or to the documentation contained in these books. The authors * + * and publisher shall not be liable in any event for incidental or * + * consequential damages in connection with, or arising out of, the * + * furnishing, performance, or use of these programs. * + **************************************************************************/ diff --git a/201816040130/CheckingAccount.h b/201816040130/CheckingAccount.h new file mode 100644 index 0000000..1b1ae16 --- /dev/null +++ b/201816040130/CheckingAccount.h @@ -0,0 +1,38 @@ +// Lab 1: CheckingAccount.h +// Definition of CheckingAccount class. +#ifndef CHECKING_H +#define CHECKING_H + +#include "Account.h" // Account class definition + +class CheckingAccount : public Account +{ +public: + // constructor initializes balance and transaction fee + CheckingAccount( double, double ); + virtual void credit(double) override; + virtual bool debit(double) override; +private: + double transactionFee; // fee charged per transaction + + // utility function to charge fee + void chargeFee(); +}; // end class CheckingAccount + +#endif + + +/************************************************************************** + * (C) Copyright 1992-2012 by Deitel & Associates, Inc. and * + * Pearson Education, Inc. All Rights Reserved. * + * * + * DISCLAIMER: The authors and publisher of this book have used their * + * best efforts in preparing the book. These efforts include the * + * development, research, and testing of the theories and programs * + * to determine their effectiveness. The authors and publisher make * + * no warranty of any kind, expressed or implied, with regard to these * + * programs or to the documentation contained in these books. The authors * + * and publisher shall not be liable in any event for incidental or * + * consequential damages in connection with, or arising out of, the * + * furnishing, performance, or use of these programs. * + **************************************************************************/ diff --git a/201816040130/SavingsAccount.cpp b/201816040130/SavingsAccount.cpp new file mode 100644 index 0000000..603e18b --- /dev/null +++ b/201816040130/SavingsAccount.cpp @@ -0,0 +1,32 @@ +// Lab 1: SavingsAccount.cpp +// Member-function definitions for class SavingsAccount. +#include "SavingsAccount.h" // SavingsAccount class definition + +// constructor initializes balance and interest rate +SavingsAccount::SavingsAccount( double initialBalance, double rate ) + : Account( initialBalance ) // initialize base class +{ + interestRate = ( rate < 0.0 ) ? 0.0 : rate; // set interestRate +} // end SavingsAccount constructor + +// return the amount of interest earned +double SavingsAccount::calculateInterest() +{ + return getBalance() * interestRate; +} // end function calculateInterest + + +/************************************************************************** + * (C) Copyright 1992-2012 by Deitel & Associates, Inc. and * + * Pearson Education, Inc. All Rights Reserved. * + * * + * DISCLAIMER: The authors and publisher of this book have used their * + * best efforts in preparing the book. These efforts include the * + * development, research, and testing of the theories and programs * + * to determine their effectiveness. The authors and publisher make * + * no warranty of any kind, expressed or implied, with regard to these * + * programs or to the documentation contained in these books. The authors * + * and publisher shall not be liable in any event for incidental or * + * consequential damages in connection with, or arising out of, the * + * furnishing, performance, or use of these programs. * + **************************************************************************/ diff --git a/201816040130/SavingsAccount.h b/201816040130/SavingsAccount.h new file mode 100644 index 0000000..f959fa9 --- /dev/null +++ b/201816040130/SavingsAccount.h @@ -0,0 +1,35 @@ +// Lab 1: SavingsAccount.h +// Definition of SavingsAccount class. +#ifndef SAVINGS_H +#define SAVINGS_H + +#include "Account.h" // Account class definition + +class SavingsAccount : public Account +{ +public: + // constructor initializes balance and interest rate + SavingsAccount( double, double ); + + double calculateInterest(); // determine interest owed +private: + double interestRate; // interest rate (percentage) earned by account +}; // end class SavingsAccount + +#endif + + +/************************************************************************** + * (C) Copyright 1992-2012 by Deitel & Associates, Inc. and * + * Pearson Education, Inc. All Rights Reserved. * + * * + * DISCLAIMER: The authors and publisher of this book have used their * + * best efforts in preparing the book. These efforts include the * + * development, research, and testing of the theories and programs * + * to determine their effectiveness. The authors and publisher make * + * no warranty of any kind, expressed or implied, with regard to these * + * programs or to the documentation contained in these books. The authors * + * and publisher shall not be liable in any event for incidental or * + * consequential damages in connection with, or arising out of, the * + * furnishing, performance, or use of these programs. * + **************************************************************************/ diff --git a/201816040130/polymorphicBanking.cpp b/201816040130/polymorphicBanking.cpp new file mode 100644 index 0000000..52743a7 --- /dev/null +++ b/201816040130/polymorphicBanking.cpp @@ -0,0 +1,75 @@ +// Lab 1: polymorphicBanking.cpp +// Processing Accounts polymorphically. +#include +#include +#include +using namespace std; + +#include "Account.h" // Account class definition +#include "SavingsAccount.h" // SavingsAccount class definition +#include "CheckingAccount.h" // CheckingAccount class definition + +int main() +{ + // create vector accounts + + vectoraccounts(4); + // initialize vector with Accounts + 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 ); + + // loop through vector, prompting user for debit and credit amounts + for ( size_t i = 0; i < accounts.size(); i++ ) + { + cout << "Account " << i + 1 << " balance: $" + << accounts[i] -> getBalance(); + double withdrawalAmount = 0.0; + cout << "\nEnter an amount to withdraw from Account " << i + 1 + << ": "; + cin >> withdrawalAmount; + accounts[i] -> debit(withdrawalAmount); + + double depositAmount = 0.0; + cout << "Enter an amount to deposit into Account " << i + 1 + << ": "; + cin >> depositAmount; + accounts[i] -> credit(withdrawalAmount); + // downcast pointer + SavingsAccount *savingsAccountPtr = + dynamic_cast (accounts[i]); + + // if Account is a SavingsAccount, calculate and add interest + if ( savingsAccountPtr != nullptr) + { + double interestEarned = savingsAccountPtr -> calculateInterest(); + cout << "Adding $" << interestEarned << " interest to Account " + << i + 1 << " (a SavingsAccount)" << endl; + + savingsAccountPtr -> credit(interestEarned); + } // end if + + cout << "Updated Account " << i + 1 << " balance: $" + << accounts[i] -> getBalance() + << "\n\n"; + } // end for +} // end main + + +/************************************************************************** + * (C) Copyright 1992-2012 by Deitel & Associates, Inc. and * + * Pearson Education, Inc. All Rights Reserved. * + * * + * DISCLAIMER: The authors and publisher of this book have used their * + * best efforts in preparing the book. These efforts include the * + * development, research, and testing of the theories and programs * + * to determine their effectiveness. The authors and publisher make * + * no warranty of any kind, expressed or implied, with regard to these * + * programs or to the documentation contained in these books. The authors * + * and publisher shall not be liable in any event for incidental or * + * consequential damages in connection with, or arising out of, the * + * furnishing, performance, or use of these programs. * + **************************************************************************/