In: Computer Science
C++ programming------------
Enhance the Account class to compute the interest on the current
balance. An account has initial balance of $10,000.00, and 6%
percent annual interest is compounded monthly until the investment
is double.
Write a main function to determine the number of months to double
the initial investment. Create function name "double investment"
for this project. Create a menu to allow the user to enter the
initial investment and the annual interest rate. Please create a
header file(Account.h) and a cpp file (Account.cpp). Below is code
provided that needs to be altered to add "double investment"
function. Please include code and output screenshots to
show validation of functionality as well as comments to understand
your code.
Account.h file given**
#ifndef Account_h
#define Account_h
#include <iostream>
using namespace std;
class Account
{
public:
double amount;
Account(double a);
void deposit(double a);
void withdraw(double a);
double get_balance();
};
#endif
Account.cpp file given**
#include "Account.h"
#include <iostream>
using namespace std;
//creates account and sets amount with users account set-up value
Account::Account(double a){
amount = a;
}
void Account::deposit(double a){
if(a < 0){
return;
}
//adds to amount in account once deposited
amount += a;
}
void Account::withdraw(double a){
if(amount-a < 0){
return;
}
//decreases amount in account once withdrawn
amount -= a;
}
//returns balance of account
double Account::get_balance(){
return amount;
}
int main()
{
Account my_account(100); // Set up my account with $100
my_account.deposit(50);
my_account.withdraw(175); // Penalty of $20 will apply
my_account.withdraw(25);
cout << "Account balance: " << my_account.get_balance() << "\n";
my_account.withdraw(my_account.get_balance()); // withdraw all
cout << "Account balance: " << my_account.get_balance() << "\n";
return 0;
}
We know the formula for compound interest
where,
A = amount of money accumulated after n years, including interest.
P = principal amount (the initial amount you borrow or deposit)
r = annual rate of interest (as a decimal)
n = number of times the interest is compounded per year
t = number of years the amount is deposited or borrowed for.
According to question, we have to double the initial investment i.e.,
A = 2P
and n = 12 because it is compounded monthly
therefore,
Taking log both side
We can see that final t value depends on r value not on P value.
Below is the complete code:
Account.h
#include <iostream>
using namespace std;
class Account
{
public:
double amount;
Account(double a);
void deposit(double a);
void withdraw(double a);
double get_balance();
int double_investment(double P, double r); //function
is added
};
Account.cpp
#include "Account.h"
#include <iostream>
#include<math.h>
using namespace std;
//creates account and sets amount with users account set-up
value
Account::Account(double a){
amount = a;
}
void Account::deposit(double a){
if(a < 0){
return;
}
//adds to amount in account once
deposited
amount += a;
}
void Account::withdraw(double a){
if(amount-a < 0){
return;
}
//decreases amount in account once withdrawn
amount -= a;
}
//returns balance of account
double Account::get_balance(){
return amount;
}
//calculate months required to double the investment
int Account::double_investment(double P, double r)
{
//convert percentage of rate into decimal
r = r/100;
//implement formula for time which was derived in the
description
//i.e., t = log(2)/(12*log(1+(r/12)))
double t = log10(2)/(12*log10(1+(r/12)));
//store total month
int month=0;
month = t*12;
return month;
}
int main()
{
Account my_account(100); // Set up my account with $10
my_account.deposit(50);
my_account.withdraw(175); // Penalty of $20 will apply
my_account.withdraw(25);
cout << "Account balance: " << my_account.get_balance() << "\n";
my_account.withdraw(my_account.get_balance()); // withdraw all
cout << "Account balance: " << my_account.get_balance() << "\n";
//Take inital amount and rate as input from user
int P, r;
cout<<"Enter initial investment :";
cin>>P;
cout<<"Enter annual rate in percent :";
cin>>r;
//call funtion
cout<<"Total month required to double the
investment =
"<<my_account.double_investment(P,r)<<"\n";
return 0;
}