I having issues displaying the final balance in this program. I'm especially having issues on adding the objects/constructors at the very end of my main.cpp file.
FILE 1: MAIN.CPP
//////////////////////////////////////鈥?br>
#include %26lt;iostream%26gt;
#include %26lt;cmath%26gt;
#include %26lt;ctime%26gt; //for time function
#include %26lt;time.h%26gt; //C time header
#include %26lt;cstdlib%26gt; //for rand() %26amp; srand() functions
#include "header.h"
using namespace std;
int main()
{
//prompt user to enter his/her name
cout %26lt;%26lt; "\nPlease enter your name: ";
char name[80]; //80 characters maximun
cin.getline(name, 80);
//prompt user to enter balance
cout %26lt;%26lt; "Enter your current balance: ";
double balance;
cin %26gt;%26gt; balance;
//prompt user to enter interest rate
cout %26lt;%26lt; "Enter the annual interest rate: ";
double annualInterestRate;
cin %26gt;%26gt; annualInterestRate;
//prompt user to enter withdraw amount
cout %26lt;%26lt; "Enter amount you want to withdraw (enter 0 if none): ";
double withdraw;
cin %26gt;%26gt; withdraw;
//prompt user to enter deposit amount
cout %26lt;%26lt; "Enter amount wou want to deposit (enter 0 if none): ";
double deposit;
cin %26gt;%26gt; withdraw;
//HERE'S MY PROBLEM:
cout %26lt;%26lt; "Your final balance is " %26lt;%26lt; amount2.getBalance(balance) + Balance::deposit(deposit) + Balance::withDraw(withdraw) %26lt;%26lt; endl;
return 0;
}
FILE 2: HEADER.H
//////////////////////////////////////鈥?br>
//create a object for the balance
//(see Exercise 9.3)
class Balance
{
private:
//the user's balance
double balance;
double amountWithdraw;
double amountDeposit;
public:
//construct a default balance object
Balance(); //prototype constructor
//construct a balance object
Balance(double newBalance); //prototype constructor
//gain access to balance, which is a private variable
double Balance::getBalance(double accessBalance);
//add the desposit to the current balance
double deposit(double accessBalance);
//subtract the withdraw from the current balance
double withDraw(double accessBalance);
}; //semicolon required!
//create a object for the annual interest rate
//(see Exercise 9.3)
class AnnualInterestRate
{
private:
double monthlyInterest; //for getInterestRate function
double withdrawAmount; //for withDraw function
double depositAmount; //for deposit function
public:
//construct a default annual interest rate object
AnnualInterestRate(); //prototype constructor
//construct a annual interest rate object
AnnualInterestRate(double newMonthlyInterest); //prototype constructor
//return the monthly interest rate
double getMonthlyInterestRate(double newMonthlyInterest); //prototype function
}; //semicolon required!
FILE 3: CONSTRUCTORS.CPP
//////////////////////////////////////鈥?br>
//(see Exercise 9.3)
Balance::Balance()
{
balance = 0;
}
//construct a balance object
//(see Exercise 9.3)
Balance::Balance(double newBalance)
{
balance = newBalance;
}
//gain access to balance, which is set to private
//(see Chp.9_header.h)
double Balance::getBalance(double accessBalance)
{
return balance;
}
//add the deposit to the balance
//(see Chp.9_header.h)
double Balance::deposit(double accessBalance)
{
return balance + accessBalance;
}
//subtract the withdraw from the balance
double Balance::withDraw(double accessBalance)
{
return balance - accessBalance;
}
//construct a default annual interest rate object
//(see Exercise 9.3)
AnnualInterestRate::AnnualInterestRate鈥?br>
{
monthlyInterest = 0;
}
//construct a annual interest rate object
//(see Exercise 9.3)
AnnualInterestRate::AnnualInterestRate鈥?newMonthlyInterest)
{
monthlyInterest = newMonthlyInterest;
}
//return the monthly interest rate
//(see Exercise 9.3)
double AnnualInterestRate::getMonthlyInterestRa鈥?newMonthlyInterest)
{
//calculate the monthly interest rate
double monthlyInterest = (newMonthlyInterest /12) * 100;
return monthlyInterest;
}|||I don't think classes work the way you think they work. Here's an example bank account class:
class BankAccount
{
private:
double balance;
public:
BankAccount(){
balance = 0;
}
BankAccount(double startBalance){
balance = startBalance;
}
double deposit(double amount){
balance += amount;
return balance;
}
double withdraw(double amount){
balance -= amount;
return balance;
}
double getBalance(){
return balance;
}
};
You would use it the following way:
double value;
cout%26lt;%26lt;"Enter a starting balance: "; cin%26gt;%26gt;value;
BankAccount myAccount(value);
cout%26lt;%26lt;"Enter a deposit amount: "; cin%26gt;%26gt;value;
myAccount.deposit(value);
cout%26lt;%26lt;"Enter a withdraw amount: "; cin%26gt;%26gt;value;
myAccount.withdraw(value);
cout%26lt;%26lt;"Final account value: "%26lt;%26lt;myAccount.getBalance()%26lt;%26lt;endl;
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment