Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lecture 1 -- 1 Computer Science I - Martin Hardwick Making the bank object oriented rIn upcoming lectures we will be looking at efficiency issues rIn order.

Similar presentations


Presentation on theme: "Lecture 1 -- 1 Computer Science I - Martin Hardwick Making the bank object oriented rIn upcoming lectures we will be looking at efficiency issues rIn order."— Presentation transcript:

1 Lecture 1 -- 1 Computer Science I - Martin Hardwick Making the bank object oriented rIn upcoming lectures we will be looking at efficiency issues rIn order to study these issues we need implement the banking example as classes and vectors rBy making these changes we will be able to swap algorithms for the bank functions and measure efficiency. rThe two things we need to do are l Change the acct struct into an object l Make the bank a class that stores its data in a vector

2 Lecture 1 -- 2 Computer Science I - Martin Hardwick Making the struct an object rWe want to convert this struct to an object rAn object has data and methods rAn object has public and private sections rGood practice puts the data in the private rGood practice puts access methods also known as get and put methods in the public section rAfter we have done the conversion we can add other methods to the object. rFor example add_funds (double amount) is_bankrupt() struct acct { int acctnum; string name; double balance; };

3 Lecture 1 -- 3 Computer Science I - Martin Hardwick Account Class r Contents of the acct.h file #include class acct { public: acct (); acct (int num, string name, double bal); string get_name (); int get_acctnum (); double get_balance (); double put_balance (double bal); private: int acctnum; string name; doublebalance; }; rThere must be a corresponding account.cpp file with the member functions’ implementations. rThe ‘acct’ functions create the object. One with default values and the other with new values rThe get functions return each of the values. rThe put function allows the balance to be changed. rThe semi-colon at the end of the class is very important

4 Lecture 1 -- 4 Computer Science I - Martin Hardwick Acct implementation r Contents of the acct.cpp file #include #include “acct.h” double acct::get_bal () { return balance; } void acct::put_balance (double bal) { balance = bal; return; } string acct::get_name () { return name; } void acct::put_name (string nme) { name = nme; return; } rA method is a function that starts with the name of the class followed by “::”. acct::get_bal l is a method of the acct class rA get method returns the value. rA put method changes the value. rNormally there are gets and puts for every data item rYou MUST use the :: notation or the method will not belong to your class. l It will be an ordinary function that cannot access the member data l So you will see compiler errors complaining about undefined variables.

5 Lecture 1 -- 5 Computer Science I - Martin Hardwick Constructor methods r More contents of the acct.cpp file // default for constructor for arrays etc. acct:acct () { acctnum = -1; name = “unknown”; balance = 0; } // sensible constructor for new values acct:acct (int num, string nme, double bal) { acctnum = num; name = nme; balance = bal; } rA constructor method has the same name as the class. rConstructor methods are used to set initial values for the object. rThere has to be a constructor that takes no arguments. l This constructor is used to fill arrays and other data structures rUsually there is at least one other constructor that sets initial values for all the private data. rThe default constructor should set the private data to values that show the data has not yet been set l Our example sets the acctnum to -1

6 Lecture 1 -- 6 Computer Science I - Martin Hardwick Bank Simulation (banking.h) #include #include “acct.h” using namespace std; class banking { private: vector bank;// bank accounts public: banking (); int find(int goal); void insert(acct newacct); int size ();// current size of the bank acct get (int loc);// account at a location }; rNext we will make a class for the bank rThis class wraps the functions that we made before so that we can find and insert bank accounts. rWe also have a function to return a bank account instance after it has been found using the find function rA vector is used to hold the list of bank accounts. rA bank account consists of an account number, the name of the person who owns it, and the current balance. l Our new C++ class does this

7 Lecture 1 -- 7 Computer Science I - Martin Hardwick Bank Simulation (2) #include “banking.h” int main () //PURPOSE: simulate a small bank //PRECONDITIONS: existing // accounts in file account.txt // in project folder //POSTCONDITIONS: finds, inserts, // deletes, and lists accounts { // list of bank accounts banking my_bank; // file with list of bank accounts ifstreamvault; // user request stringcommand; // account number to find intgoal; rThe main function creates the list of bank accounts and processes user commands to perform transactions. rThe number of accounts is unlimited. rThe existing accounts are read from a file at the start of the program. l this creates the bank vector for the program to use rUser commands are typed as strings (e.g., find, insert, exit).

8 Lecture 1 -- 8 Computer Science I - Martin Hardwick Bank Simulation (3) // subscript of account in bank intloc; // a bank account acctaccount; int num; string nme; double bal; // read existing accounts from file vault.open("accounts.txt"); vault >> num >> nme >> bal; while (!vault.eof()) { account = account (num, nme, bal); my_bank.insert (account); vault >> num >> nme >> bal; } // display dollar values correctly cout << setiosflags(ios::fixed) << setprecision(2); rThe program assumes that a file named accounts.txt contains the data for existing accounts. rThis is the standard while loop for reading data from a file until the end of file is reached. rWe now use the insert method to add the data to the bank

9 Lecture 1 -- 9 Computer Science I - Martin Hardwick Bank Simulation (4) // get ready to process commands cout << "The bank is now open." ; cout << endl << endl; // loop to process user commands cout << endl << "----------" << endl; cout << "Enter command " << "(exit to stop): "; cin >> command; while (command != "exit") { if (command == "find") { // FIND COMMAND // get account to find cout << "Enter account num: "; cin >> goal; // search for account loc = my_bank.find(goal); rFirst case is the find command. l get account number to find from user l call Member function find to search the account list for this account

10 Lecture 1 -- 10 Computer Science I - Martin Hardwick Member Function Find (1) int banking::find(int goal) //PURPOSE: search for a goal account // in the list of accounts //POSTCONDITIONS: returns the // subscript of goal account or -1 if // the goal account is not found { int k;// loop variable rParameters: list of accounts, number of accounts, number of the goal account to find. rReturn: subscript of goal account if found, or –1 to indicate that it is not in the list. rSearch the list from its beginning, element by element until: l find the account l reach the end of the list rThis is called a sequential search.

11 Lecture 1 -- 11 Computer Science I - Martin Hardwick Member Function find (2) //search for the goal account in //the list of accounts for (k=0; k<bank.size(); k++) { if (bank[k].get_num() == goal) { return k; } // didn't find goal account return -1; } rIf the goal number is in the account list, it will be found by the for loop. l if it is found at subscript k, return k and terminate the function rIf the loop ended because k is no longer less than numaccts, then the entire list was searched and the goal account was not found. l in this case, return -1

12 Lecture 1 -- 12 Computer Science I - Martin Hardwick Bank Simulation (5) // display account data // or error message if (loc >= 0) { account = my_bank.get (loc) cout << "Account: " << account.get_num() << "\t" << "Owner: " << account.get_name() << "\t" << "Balance: " << account.get_balance() << endl; } else { cout << "Account " << goal <<" does not exist." << endl; } rRemember, just before this is the call to function find with the result saved in variable loc. rIf loc is greater than or equal to 0, it must be the subscript of the goal account in the account list. l in this case, display the account data. l First get the account from the bank l Call get methods to get the values

13 Lecture 1 -- 13 Computer Science I - Martin Hardwick Bank Simulation (6) else if (command == "list") { // LIST COMMAND cout << "List of accounts:" << endl; for (loc=0; loc<my_bank.size(); loc++) { account = my_bank.get(i); cout << "Account: " << account.get_num () << "\t" << "Owner: " << account.get_name() << "\t" << "Balance: " << account.get_balance() << endl; } rThe second case inside the while loop that processes user commands is the list command. rDisplay the account data for all accounts in the account list. rProbably better to make this a method in the banking class rCalled dump or something similar banking::dump ();

14 Lecture 1 -- 14 Computer Science I - Martin Hardwick Bank Simulation (7) else if (command == "insert") { // INSERT COMMAND cout << "Enter new account data:" << endl; cout << "Account number: "; cin >> account.num; cout << "Account name: "; cin >> account.name; cout << "Account balance: "; cin >> account.balance; my_bank.insert(account); } rThe third case inside the while loop that processes user commands is the insert command. l This is now a method of the banking class

15 Lecture 1 -- 15 Computer Science I - Martin Hardwick Function insert (1) void banking::insert (acct newacct) //PURPOSE: insert a new account into // the account list // //POSTCONDITIONS: account list // modified to add new account, // order of account list maintained { intk,j;// loop variables rParameters: new account to add to the list. rSince the list order must be maintained, we must find where in the list the new account should be placed. rOnce we know where it goes, we must make room in the list to insert the new account.

16 Lecture 1 -- 16 Computer Science I - Martin Hardwick Inserting Into An Order List 1.Find where the new item goes. 2.Make room for the new item. 3.Insert the new item into the list. 0 10 20 30 40506070 8 Number of elements in the list: Insert 45 70605045 9

17 Lecture 1 -- 17 Computer Science I - Martin Hardwick Function insert (2) // find place to put new account in list // to maintain order of list k=0; while ((k < bank.size()) && (bank[k].get_num < newacct.get_num()) ){ k = k + 1; } // add new account to list if (k == bank.size()) { // goes at end bank.push_back (newacct); } rThe loop terminates when: l we find the first account in the list with a larger account number l we reach the end of the list having found no larger account numbers rIf there are no larger account number in the list, then k equals the number of accounts at the end of the loop. l in this case, add the new account to the end of the list

18 Lecture 1 -- 18 Computer Science I - Martin Hardwick Function insert (3) else { // goes in middle // make room to add it bank.push_back (newacct); for (j=bank.size()-1; j>k; j--) { bank[j] = bank[j-1]; } // add it to vacated spot bank[k] = newacct; } rOtherwise, the new account belongs in element k of the list. rMove all elements from k to the end of the list, one element to the right. l it is easiest to do this in reverse order rNow we can put the new account into element k of the list without overwriting another account.

19 Lecture 1 -- 19 Computer Science I - Martin Hardwick Bank Simulation (8) else { // INVALID COMMAND cout << "Invalid command." << endl; } cout << endl << "----------" << endl; cout << "Enter a command "; << "(exit to stop): "; cin >> command; } // exit command from user cout << endl << "----------" << endl; cout << "The bank is now closed."; cout << endl << endl; return 0; } rThe final case in the while loop processing user commands is the default case for invalid commands. l display a suitable error message rWhen the user types the exit command, the while loop terminates. l display a message closing the bank and terminate the program l a “real” bank program would write the new account list to a file l this file would be loaded into the program the next time it runs


Download ppt "Lecture 1 -- 1 Computer Science I - Martin Hardwick Making the bank object oriented rIn upcoming lectures we will be looking at efficiency issues rIn order."

Similar presentations


Ads by Google