{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

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

Info icon This 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); BankAccount(const BankAccount &account); // Member Functions void setBalance(float newBalance); void setInterestRate(float newInterestRate); bool checkAccountNumber(string testAccountNumber); bool checkPIN(string testPIN); void deposit(float amount); void withdraw(float amount); const modifier
Image of page 1

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

View Full Document Right 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
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.
Image of page 3

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

View Full Document Right 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); }
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());
Image of page 5

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

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

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern