Write a c++ class to keep track of a balance in a bank account with a varying an
ID: 3664397 • Letter: W
Question
Write a c++ class to keep track of a balance in a bank account with a varying annual interest rate. The constructor will set both the balance and the annual interest rate to some initial values (with defaults of zero). The class should have member functions to change or retrieve the current balance or interest rate. There should also be functions to make a deposit (add to the balance) or a withdrawal (subtract from the balance). Finally, there should be a function that adds interest to the balance at the current interest rate. This function should have a parameter indicating how many years worth of interest are to be added (for example, 0.5 years indicates that the account should have six months interest added). Use the class as part of an interactive program that allows the user to determine how long an initial balance will take to grow to a given value. The program should allow the user to specify the initial balance and the interest rate, and whether there are additional yearly deposits.
Explanation / Answer
class BankAccount
{
private:
double Balance;
double InterestRate;
public:
BankAccount();
BankAccount(double B, double IR){Balance=B; InterestRate=IR;}
void setB(double newB);
double getB() const;
void setIR(double IR);
double getIR() const;
void Deposit(double amount);
void Withdraw(double amount);
void addInterest(int year);
};
#include <iostream>
#include <string.h>
#include <stdio.h>
BankAccount::BankAccount()
{
Balance = 0;
InterestRate = 0;
}
void BankAccount::setB(double newBalance)
{
Balance = newBalance;
}
void BankAccount::setIR(double newInterestRate)
{
InterestRate = newInterestRate;
}
double BankAccount::getB() const
{
return Balance;
}
double BankAccount::getIR() const
{
return InterestRate;
}
void BankAccount::Deposit(double amount)
{
Balance = Balance + amount;
}
void BankAccount::Withdraw(double amount)
{
Balance = Balance - amount;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.