Hello and thanks for opening this post.
I was doing an exercise out of a C++ book asking to create sa SavingsAccount class. Now, the point of program was to practice on static data members, and I got that to work flawlessly.
However, I can see my program is inflexible in the way it calculates monthly interest. Long story short, to calculate new monthly interest rate, I need a new balance every time.
The way I choose to do this is to find the new savings balance in main() and then pass it as an argument into calculateMonthlyInterest() member function in SavingsAccount class. That way, new savings balance is calculated off the new balance I pass from main().
I can see this method of finding next month's savings balance is inefficient because balance argument to calculateMonthlyInterest must change every time in main() into a new savings balance. I can also see that to find savings balance after suppose the 6th month, I have to find savings balance for every other month before it so I can pass the correct argument for balance.
I am interested what you guys have to say on how I should solve this dilemma. My code is below:
SavingsAccount.h // class definition
Code:
// dynamic_mem.h
// SavingsAccount class definition
#ifndef SAVINGSACCOUNT_H
#define SAVINGSACCOUNT_H
class SavingsAccount {
public:
SavingsAccount(double); // constructor
~SavingsAccount(); // destructor
// set function
void setBalance(double);
// get functions
double getBalance() const;
double calculateMonthlyInterest(double);
// passes old savings value as argument
// returns new Savings Balance
// static member function
static void modifyInterestRate(double);
// send user-entered annual interest rate as argument
private:
// static data member
static double annualInterestRate;
double savingsBalance;
double balance;
};
#endif
SavingsAccount.cpp // Member function definitions
Code:
#include <iostream>
using std::cout;
using std::endl;
#include "SavingsAccount.h" // SavingsAccount class definition
// define and initialize static data member
double SavingsAccount::annualInterestRate = 0.00;
// define static member function that sets annualInterestRate to a user-passed value
void SavingsAccount::modifyInterestRate(double rate) {
annualInterestRate = rate;
}
SavingsAccount::SavingsAccount(double bal) { // user passes initial balance as argument of object
setBalance(bal);
calculateMonthlyInterest(bal); // finds monthly interest of balance and adds it to "total" savings balance
}
void SavingsAccount::setBalance(double bal) {
balance = bal;
}
double SavingsAccount::getBalance() const {
return balance;
}
double SavingsAccount::calculateMonthlyInterest(double balance) {
double monthlyInterest = (balance * annualInterestRate) / 12; // calculate monthly interest
savingsBalance = balance + monthlyInterest;
return savingsBalance;
}
SavingsAccount::~SavingsAccount() {
annualInterestRate = 0.00; // set static data member to consistent state it was in
}
main.cpp // driver to test Class SavingsAccount
Code:
#include <iostream>
using std::cout;
using std::endl;
using std::fixed;
#include <iomanip>
using std::setw;
using std::setprecision;
#include "SavingsAccount.h" // SavingsAccount class definition
int main()
{
SavingsAccount::modifyInterestRate(.03); //change annual interest rate to 3%
SavingsAccount saver1(2000.00); // set initial balances of saver1 and saver2
SavingsAccount saver2(3000.00);
double savings_1 = saver1.calculateMonthlyInterest(2000.00);
double savings_2 = saver2.calculateMonthlyInterest(3000.00);
cout << "After 1st month, saver1 with an initial balance of " << setprecision(2)
<< fixed << saver1.getBalance() << " has a new \nsavings balance of "
<< savings_1 << endl;
cout << "After 1st month, saver2 with an initial balance of " << saver2.getBalance()
<< " has a new \nsavings balance of " << savings_2 << endl;
SavingsAccount::modifyInterestRate(.04); //change interest rate to 4%
cout << "After 2nd month, saver1 with a previous balance of " << savings_1
<< " has a new \nsavings balance of " << saver1.calculateMonthlyInterest(savings_1) << endl;
cout << "After 2nd month, saver2 with a previous balance of " << savings_2
<< " has a new \nsavings balance of " << saver2.calculateMonthlyInterest(savings_2) << endl;
return 0;
}
Thank you for your help in advance.
Justin