I'm trying to create my first hierarchy with the following classes (Account, CheckingAccount, and SavingsAccount) and can't figure out how to link the classes together.
Also, should the balance value be public in the header?
Currently it is not, and it shows "error in this context" every time it's mentioned in this main code.
[stackoverflow questions] Is using pastebin instead of including the code with the question okay? Is there a faster way to indent by 4? Oh well.
header:
class Account
{
public:
Account(double);
void creditBalance(double);
void debitBalance(double);
double getBalance() const;
protected:
double balance;
};
class SavingsAccount : public Account
{
public:
SavingsAccount(double, double);
double calculateInterest();
private:
double interest = 10;
};
class CheckingAccount : public Account
{
public:
CheckingAccount(double, double);
void feeCreditBalance(double);
void feeDebitBalance(double);
private:
double fee = 10;
};
CPP file
#include "12.10.h"
#include <iostream>
using namespace std;
Account::Account(double initBal)
{
if(initBal < 0)
initBal = 0;
balance = initBal;
cerr << "Initial balance was invalid.";
}
void Account::creditBalance(double plus)
{
if(plus > 0)
balance += plus;
else
cout << "Cannot credit negative.";
}
void Account::debitBalance(double minus)
{
if(minus <= balance)
balance -= minus;
else
cout << "Debit amount exceeded account balance.";
}
double Account::getBalance() const
{
return balance;
}
SavingsAccount::SavingsAccount(double initBal,double intrst):Account(initBal)
{
if(initBal < 0){
initBal = 0;
cerr << "Initial balance was invalid.";
}
balance = initBal;
if(intrst<0)
intrst=0;
interest = intrst;
}
double SavingsAccount::calculateInterest()
{
if(interest>=0)
balance=balance+(balance*(0.01*interest));
return balance;
}
CheckingAccount::CheckingAccount(double initBal, double phi) : Account(initBal)
{
if(initBal < 0)
initBal = 0;
balance = initBal;
cerr << "Initial balance was invalid.";
if(phi < 0)
phi = 0;
fee = phi;
}
void CheckingAccount::feeCreditBalance(double plus)
{
if(plus > 0){
balance += plus;
balance -= fee;
}
else
cout << "Cannot credit negative.";
}
void CheckingAccount::feeDebitBalance(double minus)
{
if(minus <= balance){
balance -= minus;
balance -= fee;
}
else
cout << "Debit amount exceeded account balance.";
}
I have updated the content of my code, there are 6 errors that I can't find. I have a feeling it has to do with the altered feeDebit and feeCreditBalance.
Are they supposed to keep the same name as the original debit and creditBalance ones?
What is the syntax for the redefining of these functions?