Write a class to keep track of a balance in a bank account with a varying annual
ID: 3561750 • Letter: W
Question
Write a 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
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.