C++ Tasks: Create a SavingsAccount class. 1.Define a static data member annualIn
ID: 3573072 • Letter: C
Question
C++
Tasks:
Create a SavingsAccount class.
1.Define a static data member annualInterestRate (type double value such as 0.03, i.e. three percent) for all the savings accounts.
2.Each instance of the class contains the following private data members:
-accountNumber (integer from 1000 to 4999),
-accountOwner (owner’s name as a string),
-accountBalance (type double value).
3. Provide the following member functions:
-calculateMonthlyInterest() computes the monthly interest using formula:
m_interest = accountBalance * annualInterstRate / 12;
This interest should be added to accountBalance.
-deposit(double amount) adds amount to accountBalnce.
-withdraw(double amount) deduct amount from accountBalance. No overdraft is allowed. The function should return a false if overdraft, otherwise returns a true value.
-checkBalance() to print out the current accountBalance. This is a const function and will not modify the content of savingsAccount object.
4.Provide a static member function modifyInterestRate that sets the static member annualInterestRate to a new value (for example, 0.05 for five percent).
5. Define a friend function, transferFund(SavingsAccount& a, SavingsAccount& b, double& amount) which withdraws $amount from account a and deposit it to account b. The function should return false if transfer fails (due to insufficient fund), else returns a true.
B. Write a driver program to test class SavingsAccount.
-Create three instances of SavingsAccount, say savings1, savings2 and savings3, using constructors with initializer list to set the values of accountNumber, accountOwner and accountBalance.
-Set the annualInterestRate to 3 percent (0.03). Then calculate the monthly interest and print the new balances for each of the savers.
-Make some transactions (deposits and/or withdraws) from each savings account. Print some messages showing these transactions (accountNumber, amount of transaction and resulting balance).
-Check and print out the balances of all three accounts.
-Then set the annualInterestRate to 5 percent (0.05), calculate the next month’s interest and print the new balance for each of the savers.
-Transfer a small amount of fund (say $100) from one account to another using the transferFund() function. Print out the message about this transaction (the amount transferred, the from/to accountNumber and accountBalnce, transfer success/fail, etc.).
Explanation / Answer
#ifndef SAVINGSACCOUNT_H
#define SAVINGSACCOUNT_H
class savingsAccount
{
private:
static double annualInterestRate; // static = shared, only one instance
double accountBalance;
public:
savingsAccount();
savingsAccount(double);
void setAnnualInterestRate(double);//declare your constructor
void setaccountBalance(double);
double getAnnualInterestRate(double);// declare your overloaded constructor
double getaccountBalance(double);
void calculateMonthlyInterest();
static void modifyInterestRate(double newInterestRate);
};
#endif
#include <iostream>
#include <iomanip>
#include "Savings Account Class.h"
using namespace std;
void savingsAccount::setAnnualInterestRate(double annual)
{
annualInterestRate = annual;
}
void savingsAccount::setaccountBalance(double balance)
{
accountBalance= balance;
}
double savingsAccount::getAnnualInterestRate(double)
{
return annualInterestRate;
}
double savingsAccount::getaccountBalance(double)
{
return accountBalance;
}
void savingsAccount::calculateMonthlyInterest()
{
accountBalance = ((accountBalance * annualInterestRate) / 12) + accountBalance;
}
savingsAccount::savingsAccount(double balance)
{
//annualInterestRate = annual;
accountBalance = balance;
}
int main()
{
savingsAccount savings1(2000.00);
savingsAccount savings2(3000.00);
savingsAccount savings3(4000.00);
savingsAccount.modifyInterestRate(0.03);
savings1.calculateMonthlyInterest();
cout << "savings1 account balance is" <<savings1.getaccountBalance() << endl;
savings2.calculateMonthlyInterest();
cout << "savings2 account balance is" <<savings2.getaccountBalance() <<
savings3.calculateMonthlyInterest();
cout << "savings3 account balance is" <<savings3.getaccountBalance() << endl;
savingsAccount.modifyInterestRate(.04);
savings1.calculateMonthlyInterest();
cout << "savings1 account balance is" << savings1.getaccountBalance() << endl;
savings2.calculateMonthlyInterest();
cout << "savings2 account balance is" << savings2.getaccountBalance() << endl;
savings3.calculateMonthlyInterest();
cout << "savings3 account balance is" << savings3.getaccountBalance() << endl;
system("Pause");
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.