Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a class that simulates a bank account. Customers can deposit and withdraw

ID: 3759436 • Letter: W

Question

Write a class that simulates a bank account. Customers can deposit and withdraw funds. If sufficient funds are not available for withdrawal, a $10 overdraft penalty is charged. Then create an instance, customer name is Tom and initial balance is 200. He tries to withdraw 150 at the first time, then to withdraw 100, but the balance is not enough, so he failed to withdraw money and had to pay for 10 penalty. A day later, he deposited 100 to his account.

Using header files and implementation files format talked about in the lecture, you can find the example codes in the course content "examples from class".

Format requirements:

1. You should put the class in the header file(account.h).

2. You should implement the class functions in the source file(account.cpp).

3. You should write your test codes in another file(main.cpp).

Explanation / Answer

1.For the above mention condition, the code are below

firstly we created the header file name (account.h).

class Account {

public:

BankAccount();/** Constructs a bank account with zero balance. */

BankAccount(double initial_balance); /**deposit into this account.*/

void deposit(double amount); /** charges a penalty if sufficient funds are not available. */

void withdraw(double amount); /** Adds interest to the account. */

void add_interest(double rate); /** get balance */

double get_balance() const;

private:

};

2.after that impement class member function(account.cpp).

#include <stdlib.h>

#include <iostream>

#include "account.h"

using namespace std;

double BankAccount::get_balance() const

{

return balance;

}

void BankAccount::deposit(double amount)

{

balance = balance + amount;

}

/**The withdraw member function needs to charge a penalty if sufficient funds are not available:*/

void BankAccount::withdraw(double amount)

{ const double PENALTY = 10;

if (amount > balance)

{

balance = balance - PENALTY;

}

else

{

balance = balance - amount;

}

/**compute the interest and call the deposit member function to add the interest to the balance*/

void BankAccount::add_interest(double rate) {

double amount = balance * rate / 100;

deposit(amount);

}

BankAccount::BankAccount()

{

balance = 0;

}

BankAccount::BankAccount(double initial_balance)

{

balance = initial_balance;

}

3.Below is a simple test program that exercises all member functions in the (main.cpp):

#include <iostream>

#include <stdlib.h>

#include <iomanip>

#include <string>

using namespace std;

int main()

{

BankAccount tom_account(200);

harrys_account.withdraw(150);

tom_account.add_interest(1);

cout << fixed << setprecision(2) << tom_account.get_balance() << endl;

return 0;

}

#include <stdlib.h>

#include <iostream>

#include "account.h"

using namespace std;

double BankAccount::get_balance() const

{

return balance;

}

void BankAccount::deposit(double amount)

{

balance = balance + amount;

}

/**The withdraw member function needs to charge a penalty if sufficient funds are not available:*/

void BankAccount::withdraw(double amount)

{ const double PENALTY = 10;

if (amount > balance)

{

balance = balance - PENALTY;

}

else

{

balance = balance - amount;

}

/**compute the interest and call the deposit member function to add the interest to the balance*/

void BankAccount::add_interest(double rate) {

double amount = balance * rate / 100;

deposit(amount);

}

BankAccount::BankAccount()

{

balance = 0;

}

BankAccount::BankAccount(double initial_balance)

{

balance = initial_balance;

}

3.Below is a simple test program that exercises all member functions in the (main.cpp):

#include <iostream>

#include <stdlib.h>

#include <iomanip>

#include <string>

using namespace std;

int main()

{

BankAccount tom_account(200);

harrys_account.withdraw(150);

tom_account.add_interest(1);

cout << fixed << setprecision(2) << tom_account.get_balance() << endl;

return 0;

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote