classes and object orientated programming
Jun 24, 2015 at 4:00pm UTC
ok so my problem is that my mutator functions are not working when you run the program. its a bank program were the user can deposit or withdraw money. when i use the mutator functions to deposit an amount, they do not add to the total balance.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218
main.cpp
#include <iostream>
#include <string>
#include "Bank.h"
using namespace std;
void printBackInfo();
int main()
{
Bank::printBankInfo();
cout << "Michael created an account and deposited 500" ;
Bank Michael("Michael" , 0001, 500);
Bank Sarah;
cout << endl << "Sarah created an account and deposited 1000" ;
Sarah.setName("Sarah" );
Sarah.setAcctNumber(0002);
Sarah.setBalance(1000);
Bank::printBankInfo();
cout << endl << "Dick created an account and deposited 1500" ;
Bank Dick("Dick" , 0003, 1500);
Bank::printBankInfo();
cout << endl << "Dick set his balance to 1200" ;
Dick.setBalance(1200);
Bank::printBankInfo();
cout << endl << "Sarah deposited 700" ;
Sarah.deposit(700);
Bank::printBankInfo();
cout << "Michael's account was terminated due to lack of use. " ;
Michael.~Bank();
Bank::printBankInfo();
cout << "\n\n" ;
system("PAUSE" );
return 0;
}
bank.cpp
#include "Bank.h"
// static memeber variables
int Bank::totalAccounts = 0;
double Bank::bankBalance = 10000.0;
Bank::Bank()
{
acctNumber = 0;
balance = 0.0;
totalAccounts++; // increments the static variable by one
}
Bank::Bank(string newName, int newAcctNumber, double newBalance)
{
name = newName;
acctNumber = newAcctNumber;
balance = newBalance;
totalAccounts++; // increments the static variable by one
bankBalance += newBalance; // allows the balance in the account to be added up when a user makes a deposit
}
Bank::~Bank()
{
totalAccounts--;
bankBalance -= balance;
}
// accessor definitions
string Bank::getName() const
{
return name;
}
int Bank::getAcctNumber() const
{
return acctNumber;
}
double Bank::getBalance() const
{
return balance;
}
// mutator functions
void Bank::setName(string newName)
{
name = newName;
}
void Bank::setAcctNumber(int newAcctNumber)
{
newAcctNumber = acctNumber;
}
void Bank::setBalance(double newBalance)
{
bankBalance -= balance;
newBalance = balance;
bankBalance += balance;
}
void Bank:: withdraw(double withdraw)
{
balance -= withdraw;
bankBalance -= withdraw;
}
void Bank::deposit(double deposit)
{
balance += deposit;
bankBalance += deposit;
}
void Bank::printBankInfo()
{
cout << endl << "Number of accounts: " << totalAccounts << endl;
cout << "Total Balance: " << bankBalance << endl;
}
bank.h
#pragma once
#ifndef BANK_H
#define BANK_H
#include <iostream>
#include <string>
using namespace std;
class Bank
{
public :
// default destructor
Bank();
// overload constructor
Bank(string, int , double );
// destructor function
~Bank();
// Accessor functions
string getName() const ;
// getName - gets name of user
// @return string - returns name of user
int getAcctNumber() const ;
// getAcctNumber - gets account number
// @return int - returns account number
double getBalance() const ;
// getBalance - gets the balance
// @return double - returns the balance
// mutator functions
void setName(string);
// setName - sets name of user
// @param string - name of user
void setAcctNumber(int );
// setAccctNumber - sets the account number
// @param int - account number of user
void setBalance(double );
// setBalance - sets the balance of the user
// @param - balance of user
void withdraw(double );
// withdraw - withdraw money
// @param double - money withdrawn
void deposit(double );
// deposit - deposit money
// @param double - money deposited
static void printBankInfo();
private :
// memeber variables
string name;
int acctNumber;
double balance;
static int totalAccounts;
static double bankBalance;
};
#endif
Jun 24, 2015 at 4:06pm UTC
Haven't checked them all, but isn't this backwards?
1 2 3 4
void Bank::setAcctNumber(int newAcctNumber)
{
newAcctNumber = acctNumber;
)
Andy
Jun 24, 2015 at 4:21pm UTC
yes it was sir/mam... this was also backwards.
1 2 3 4 5 6
void Bank::setBalance(double newBalance)
{
bankBalance -= balance;
newBalance = balance;
bankBalance += balance;
}
thank you very much, hopefuly i dont make this mistake again.
Topic archived. No new replies allowed.