Hi,
I got help with it, I needed guards on my BankAccount.h and to move them to the end of the file instead of just the end of the class. Thank you though :) |
It doesn't sound quite right to me, instead you need to have separate files. The class declaration goes into a header file (I prefer .hpp rather than .h, but that's just my preference), and the class function definitions go into a .cpp file.
That way header files can be included wherever they are needed - in multiple cpp files where required.
As in :
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
|
#ifndef CHECKINGACCOUNT_H
#define CHECKINGACCOUNT_H
using namespace std; // *** try to avoid doing this
class CheckingAccount : public BankAccount
{
public:
CheckingAccount(double balance);
//CheckingAccount(double balance);
void set_interestRate(double rate);
double get_interestRate();
void set_minBalance(double minBal);
double get_minBalance();
void set_fee(double fee);
double get_fee();
void postInterest();
bool checkMin();
void withdrawl(double amount);
void writeCheck(double amount);
void printInfo();
//void CheckingAccount::printInfo()
protected:
double _rate, _minBalance, _fee;
};
#endif
|
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
|
#include <iostream>
#include <iomanip>
#include "BankAccount.h"
// using namespace std; // *** don't have this in a header file
CheckingAccount::CheckingAccount(double balance)//Since the tester that was provided doesnt set the rate, min balance or fee I just set it here
: BankAccount(balance)
{
//_number = get_actNumber();
_fee = 15;
_minBalance = 200;
_rate = 0.04;
}
void CheckingAccount::set_interestRate(double rate)
{
_rate = rate;
}
double CheckingAccount::get_interestRate()
{
return _rate;
}
void CheckingAccount::set_minBalance(double minBal)
{
_minBalance = minBal;
}
double CheckingAccount::get_minBalance()
{
return _minBalance;
}
bool CheckingAccount::checkMin()
{
if (_balance >= _minBalance)
{
return true;
}
else
return false;
}
void CheckingAccount::set_fee(double fee)
{
_fee = fee;
}
double CheckingAccount::get_fee()
{
return _fee;
}
void CheckingAccount::postInterest()
{
_balance += (_balance * _rate);
}
void CheckingAccount::withdrawl(double amount)
{
if (_balance < amount)
{
cout << "insufficient funds. Withdrawl rejected, does not affect balance." << endl;
}
else//I assume the bank will allow a withdrawl as long as the balance is greater than the amount even if the fee drops the account into the negative
{
if (_balance - amount >= _minBalance)
{
_balance = _balance - amount;
}
else
{
_balance -= (amount + _fee);
}
}
}
void CheckingAccount::writeCheck(double amount)
{
withdrawl(amount);
}
void CheckingAccount::printInfo()
{
cout << "Interest Checking ACCT#:" << get_actNumber() << " Balance: $" << setprecision(2) << fixed << get_balance() << endl;
}
|
I have just cut & paste your code, none of it is tested - hopefully you get the idea of what I am saying.
It's good practise for a constructor initialiser list to set values for all the class data members. Also, derived classes initialiser lists should call base class constructors so that
all their data is set.
Try to avoid having get / set functions for everything, this program doesn't seem to need them - values are set once. So no need for the set functions
Get functions aren't needed by class member functions, as they have direct access to all class members. So in the
PrintInfo
function, you don't need to call a get function to access the class data.
With the variable & function names, try to avoid abbreviating everything - longer names are often clearer. Try to incorporate the exact variable name in a function that uses it. For example not
get_actNumber()
with
_number
Protected data members are also often not a good idea. Instead a class has an
interface of public functions that access private data. Note that this doesn't mean get / set functions for everything - only provide functions for what is needed. You can make a function that has a reference to a class object as an argument, then that function can access all the public functions that belong to that class.
With
static int _number;
, because it is
static
, there is only 1 value across all objects of that class. I am fairly sure this is not what you want. You could have a static variable (in the
BankAccount
class) that stores the
last account number allocated.
With the
BankAccount::get_actNumber()
it adds 100 to the account number every time it is called, so if it is called multiple times for 1 account the account number changes. Combined with the previous point about being static, that makes for quite a bit of confusion. One shouldn't rely on a function only being called once.
Any way I hope all this helps a bit :+)
Edit:
If you are going to have a get function,then it should only return the value, not do something else to it.