Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I have updated my previously posted C++ question to ensure that I have included all of the necessary documentation in order to complete Part 2

I have updated my previously posted C++ question to ensure that I have included all of the necessary documentation in order to complete Part 2 - Bank Account of the assigned lab in its entirety. Please note that the lab below is somewhat lengthy, and I do not expect you to answer each question! If coding the full lab is too much to ask, please focus soley on Part 2 of the lab, titled Bank Account and the subsections that follow. I hope that this helps! Thank you!!!

Create a Bank Account program in C++ that meets the following requirements. Be sure to write unit tests for each public method before you implement it! Also, remember to include an interactive demo under the filename account_demo.cpp.

C++ - Pointers & Dynamic Memory Allocation:

1 Introduction The functions in the following subsections can all go in one big file called pointer practice.cpp.

1.1 Basics Write a function, int square_1( int* p), that takes a pointer to an int and returns the square of the int that it points to. Write a function, void square_2( int* p), that takes a pointer to an int and replaces that int (the one pointed to by p) with its square. Write a function, void square_3( int& p), that takes an int by reference and replaces it with its square. Write a function void display_square () that reads a number from the user and displays its square three times, once using each of the above functions. Write a main that calls display_square as a simple test.

1.2 Arrays Write a function, void replace_with_increment ( int* p, size_t n) that takes the base pointer of an array of size n as an argument and increments each of the items in the array. Write a function, void display_increment_array (), that makes an array with 10, 20, and 30 in it, passes it to replace_with_increment, and prints its elements after that call. Modify main to call this function and test it.

1.3 Dynamic Allocation Write a function, counter* make_broken_counter(), that creates a counter as a local variable and returns its address. Compile your program with all warnings enabled (-Wall for command line folks, otherwise look in compiler settings in your IDE). Note the warnings/errors. Write a function, void display_broken_counter (), that uses the function above to make a counter, increments and prints its count. Then have it call a few other functions (doesnt matter what functions as long as they some local variables), then try to increment the counter and print its count again. Call this function from your main. What happens? Write a function, counter* make_counter(), that creates a counter using new and returns the resulting pointer.

Write a function, void display_counter (), that uses the function above to make a counter, increments and prints its count. Then have it call a few other functions (doesnt matter what functions as long as they some local variables), then try to increment the counter and print its count again. Call this function from your main. What happens? Write a function, counter* make_counter (int start), that creates a counter using new, passing start to its constructor, and returns the resulting pointer. Write a function, void display_counter_2 (), that calls this function and displays the resulting counters count. Modify main to call this function... Write a function, counter* bunch_of_counters (size_t number), that uses new to create an array of number counters. Return the base pointer of the array. Write a function, void display_counters (), that uses the function above to make an array of 25 counters, increments them each 5 times and then prints each of their counts. Modify main to call this function.

** NOTE: Keep all previously written code, but comment the code out of the program using //, or /* and */, before beginning the following section of the lab!! The exercises in the previous section are designed to help you become familiar with pointers & dynamically allocated memory. REMEMBER to save your code from this section & submit it with the rest of your lab assignment!!!!!

2 Bank Account In the following subsections you will create an account class that manages a dynamically-sized collection of transactions. As we have not learned about dealing with some aspects of dynamic memory allocation in classes, your code will leak memory. Do not use this class as a reference for future projects (please put a note in it saying just that). The goal is to learn the basics before cluttering your mind and code with C++-isms.

2.1 The transaction class Create a class called transaction with the following instance variables: int amount string type Create a two-argument constructor that allows the caller to specify values for all of these variables. Create (const) getter methods for each of these variables.

2.2 Test first! Remember to write unit tests for each public method before you implement it.

2.3 The account class basics Create an account class with the following instance variables: int balance balance in cents size_t transactions_cap capacity of transactions list

size_t transactions_size number of used slots in transaction array transaction ** transactions base pointer to dynamically allocated array of pointers to transactions

Write a constructor that initializes balance to zero, transactions size to zero, transactions cap to 30 and dynamically allocates a collection of transaction cap transaction pointers.

Write methods deposit and withdraw that simply update the balance variable for now. Include assertions to make sure that the amount deposited/withdrawn was valid.

2.4 Managing the transactions array

Implement the following private methods: void add_transaction ( transaction *t ) that: Checks if there is enough room in the transactions array and, if not, calls resize_transactions () Adds the transaction *t to the end of the partially filled transactions array. Increments transactions_size void resize_transactions() doubles the capacity of the transactions array, copying old elements over to the new array and updating instance variables as appropriate.

Implement the following public methods: size_t get_transactions_size() const returns the number of elements in the transactions array transaction * get_transaction (size_t i) const (Precondition: i < transactions cap ) returns the transaction at index i in the transactions collect

Modify withdraw and deposit so that they create transaction objects describing the transaction and add them to the transaction list (use type deposit or withdraw as appropriate). Add unit tests for withdraw and deposit to make sure that these transactions are created and that they contain the correct data.

2.5 Interactive demo Create a demo that works as the example shows below. --- Current balance: 0 Enter operation: (d)eposit, (w)ithdraw, (v)iew transaction or (e)xit: d amount to deposit: 1000 --- Current balance: 1000 Enter operation: (d)eposit, (w)ithdraw, (v)iew transaction or (e)xit: v There are 1 transactions. Give transaction you wish to view: 1 Transaction 1: amount = 1000, type = deposit --- Current balance: 1000 Enter operation: (d)eposit, (w)ithdraw, (v)iew transaction or (e)xit: w amount to withdraw: 50 --- Current balance: 950 Enter operation: (d)eposit, (w)ithdraw, (v)iew transaction or (e)xit: v There are 2 transactions. Give transaction you wish to view: 9 Invalid transaction number. Enter a number between 1 and 2. There are 2 transactions. Give transaction you wish to view: 2 Transaction 2: amount = 50, type = withdraw --- Current balance: 950 Enter operation: (d)eposit, (w)ithdraw, (v)iew transaction or (e)xit: x Invalid command. --- Current balance: 950 Enter operation: (d)eposit, (w)ithdraw, (v)iew transaction or (e)xit: e Bye!

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Pro Oracle Fusion Applications Installation And Administration

Authors: Tushar Thakker

1st Edition

1484209834, 9781484209837

More Books

Students also viewed these Databases questions

Question

17. What were the objections made by opponents of the PPACA? LO24.6

Answered: 1 week ago