Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Do not delete or comment out any code. Note that this recitation may introduce some syntax or library methods that were not shown in lecture.
Do not delete or comment out any code. Note that this recitation may introduce some syntax or library methods that were not shown in lecture. Hopefully we have explained it clearly here, but of course, please ask questions if anything is unclear. The topic for this recitation is bank accounts. No, I can't think why anyone would want to write such code, but I have had students say that would be more "interesting" than warriors or dragons or ... Go figure. 1. Define a struct that can hold information about accounts a. First: . Each instance will hold the name for the account and an account number. . We have created an input file that has at least three accounts, called accounts.txt. Read a file of account information, filling a vector of account objects. define local variables corresponding to each data item Loop through the file, reading into these variables Each time through the loop, define a local instance of the account struct, assign the values that were read to the fields of the struct Close the file and push back the object onto your vector. Display all of the objects. b. Clear the vector, reopen the file (using the same stream variable, i.e. call the open method), and repeat the above, except using curly braced initializers to initialize the instance. E.g. if we had a struct Thing with two fields, we could define and initialize an instance with: . Thing thingOne{valueForFirstField, valueForSecond Field}; A user of Eclipse reported needing to specify c++14 in their build. 2. Define a class to hold accounts A Redo the above steps from task 1 that you did with the struct version. . . Use the same stream variable! Obviously, use the class's constructor to initialize the fields Write getters to access the fields in the accounts when printing. B Add an output operator for your class . Do not make it a friend! Repeat the print loop using this output operator C Now make the output operator a friend, by adding a friend prototype to your account class definition. . Comment out the line where you are using getters. (Yes, I know we said you shouldn't comment out any code...) Add a line in the output operator that prints the fields "directly", i.e. without using the getters. D Redo your input but pass temporary instances to the push back method. . This means that you will not define a variable to hold the account, but instead the vector push back method will be passed a temporary object defined as the argument to push back, . E.g. If I had a class Foo whose constructor took three ints, and a vector of Foo's called vf, could add an instance of Foo to the vector with: yf.push back(Foo(2,4,6)); E. Note that we keep making objects and then adding copies of those objects onto the vector (since that's what push back does). Here we will use the vector's emplace backmethod. Instead of passing account objects to emplace back, we can just pass the arguments that we would pass to the account's constructor and emplace back will initialize an instance in the vector! .Repeat the filling of the vector using emplace backand again display the contents. 3. Add transactions to your world. A A transaction will be implemented as a class and have . . a field to indicate whether this is a deposit or a withdrawal. a field to say how much is being deposited or withdrawn. B The account class will keep track of transactions applied to it. It will need a vector to store this history of transactions. C It will also need a "balance" field to indicate how much is in the account. D How will we use this? The account will have methods "deposit" and "withdrawal" which will be passed the amount . will add an appropriate transaction object to the history and modify the balance as needed. The output operator for the account will need to change so that it can display (you format it) the history, i.e. the transactions. E Test the above code out by reading in a file (transactions.txt) that has commands such as Account moe 6 Deposit 6 10 Withdraw 6 100 Note that the number 6 in the deposit and withdraw commands refers to the account. You will have to locate the account in your collection of accounts. Withdrawals should not be allowed to put the account in the red, so if there are insufficient funds in the account then generate an error message and go on. 4. Now make the transaction class private within the account class. What else do you have to change to make this work? a. One small hint. You will want to put the whole definition of the Transaction class's output operator inside the Transaction class. 5. If you have time in lab, create a client class. a. Every account is "tied to" a client, i.e. the account should have a field that is a client. This would replace the field that just held the account's name. b. The client class will not be defined inside the account class, but when an account is created, it's client field will be initialized from the information about who the client is. (I.e. the account constructor will take in the fields that must be initialized within the client.) c. What fields and methods should go in the client class? We will leave that up to you. Obviously the client needs a name. Perhaps also a SSN (social security number)? d. Update the output operator for your account class to display the client information and test all operations. Transactions.txt Account moe 6 Deposit 6 10 Withdraw 6 100 Account larry. 28 Withdraw 6 100 Deposit 6 10 Deposit 6 10 Withdraw 6 5 Account curly 42 Accounts.txt moe 6 larry. 28 curly 42 /* rec03-base.cpp */ // Provided #include #include #include #include using namespace // Task 1: The Account struct. Call it Accounts // Task 2: The Account class. Call it Accounts // 2.b: Accountil output operator, not a friend. // 2.2: Accounts output operator, reimplemented as a friend. // Task 3 // Transaction class definition // Account class definition // Task 4 // Transaction class definition with embedded Account class int main 1 // Task 1: account as struct // la. Fill vector of account structs from file "Taskla: " wwwwww "Filling vector of struct objects, define a local struct instance" " and set fields explicitly: " 1b mm "Task1b: " "Filling vector of struct objects, using {} initialization: " www //================================== // Task 2: account as class 11 2a "============== " " Task2a:" " Filling vector of class objects, using local class object: " "--- " " Task2b: " "output using output operator with getters " " Task2c: " "output using output operator as friend without getters " // Note code here will be same as above. " Task2d: " "Filling vector of class objects, using temporary class object: " "accounts.txt" mm " Task2e: " " Filling vector of class objects, using emailacam back: " // Task 3 "============== " " Task 3: Accounts and Transaction: " w // Task 4 "============== " mm " Task 4: Accounts and Transaction: " "Account class is nested inside Transaction class
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started