SavitchChap11and12 - Extending the Class Capabilities Let's...

Info iconThis preview shows pages 1–6. Sign up to view the full content.

View Full Document Right Arrow Icon
 Savitch -   CS 150 11 Extending the Class Capabilities Let’s extend the definition of the BankAccount class to illustrate some additional capabilities provided by C++ classes. //////////////////////////////////////////////////////////////////////// // BankAcct.h // // The class definition for the "NEW AND IMPROVED" BankAccount class. // //////////////////////////////////////////////////////////////////////// #ifndef BANK_ACCOUNT_H using namespace std; const int ACCOUNT_NBR_LENGTH = 13; const int PIN_LENGTH = 7; class BankAccount { public: // Constructors BankAccount(); BankAccount(string newAccountNumber, string newPIN, float newBalance, float newInterestRate); // Member Functions void setInterestRate(float newInterestRate); bool checkAccountNumber(string testAccountNumber); bool checkPIN(string testPIN); void deposit(float amount); void withdraw(float amount); const modifier
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
 Savitch -   CS 150 22 BankAcct.h (Continued) // Accessor Functions bool getActive() const; float getBalance() const; float getInterestRate() const; // Binary Operators bool operator == (const BankAccount &account) const; bool operator < (const BankAccount &account) const; bool operator <= (const BankAccount &account) const; bool operator > (const BankAccount &account) const; bool operator >= (const BankAccount &account) const; bool operator != (const BankAccount &account) const; // Friend Functions friend void merge(BankAccount &accountA, BankAccount &accountB); friend istream& operator >> (istream &sourceFileStream, BankAccount &account); friend ostream& operator << (ostream &destinationFileStream, const BankAccount &account); const modifiers overloaded operators friend functions
Background image of page 2
 Savitch -   CS 150 33 BankAcct.h (Continued) private: // Member Functions void setActive(bool newActive); void setAccountNumber(string newAccountNumber); void setPIN(string newPIN); // Accessor Functions string getAccountNumber() const; string getPIN() const; // Data Members string accountNumber; bool active; string pin; float balance; float interestRate; }; #define BANK_ACCOUNT_H #endif const modifiers We’ll examine the const modifiers, overloaded operators , and friend functions as we come to them in the BankAccount’s implementation file and in its driver program.
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
 Savitch -   CS 150 44 BankAcct.cpp ////////////////////////////////////////////////////////////////////////// // BankAcct.cpp // // // // The class implementation for the NEW AND IMPROVED BankAccount class. // ////////////////////////////////////////////////////////////////////////// #include <iomanip> #include <fstream> #include <cstring> #include "BankAcct.h" using namespace std; // Default constructor. Initializes no data members. // BankAccount::BankAccount() { } // Initializing constructor. Initializes appropriate data members // // to parameterized values; initializes active data member to true. // BankAccount::BankAccount(string newAccountNumber, string newPIN, float newBalance, float newInterestRate) { setAccountNumber(newAccountNumber); setPIN(newPIN); setBalance(newBalance); setInterestRate(newInterestRate); setActive(true); }
Background image of page 4
 Savitch -   CS 150 55 BankAcct.cpp (Continued) // Copy constructor. Duplicates the parameterized BankAccount. // BankAccount::BankAccount(const BankAccount &account) { string acctNbr; string pinNbr; account.getAccountNumber(acctNbr); setAccountNumber(acctNbr); setActive(account.getActive()); if (active) { account.getPIN(pinNbr); setPIN(pinNbr); setBalance(account.getBalance()); setInterestRate(account.getInterestRate()); } return; } const modifier
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 6
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 36

SavitchChap11and12 - Extending the Class Capabilities Let's...

This preview shows document pages 1 - 6. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online