I have a private data member called balance in a base class called bankAccount. I wanted my derived class checkingAccount to be able to use balance so I made it protected but i’ve noticed that my derived class seems to be able to still access balance even when it is put in the private section of my base class. I thought this was impossible? Does anyone know what muight be going on?
Base Class:
class bankAccount
{
public:
bankAccount();
void setAccountInfo(int accountNumTemp, double balanceTemp);
void applyTransaction(char accountType, char transactionTypeTemp, int amountTemp, int j);
private:
int accountNumber;
double balance;
};
Derived Class:
class checkingAccount: public bankAccount
{
public:
checkingAccount();
double applyTransaction(char transactionTypeTemp, int amountTemp, int c, double balance);
private:
float interestRate;
int minimumBalance;
float serviceCharge;
};
base class function member where the derived class function member receives the datamember in question:
void bankAccount:: applyTransaction(char accountType, char transactionTypeTemp, int amountTemp, int j)
{
int c;
c = j;
checkingAccount ca;
balance = ca.applyTransaction(transactionTypeTemp, amountTemp, c, balance);
}
Derived Class function member where the private data member of Base Class is used:
double checkingAccount:: applyTransaction(char transactionTypeTemp, int amountTemp, int c, double balance)
{
if (transactionTypeTemp == 'D')
{
balance = balance + amountTemp;
}
else if (transactionTypeTemp == 'W')
{
if (balance < amountTemp)
{
cout << "error: transaction number " << c + 1 << " never occured due to insufficent funds." << endl;
}
else
{
balance = balance - amountTemp;
if(balance < minimumBalance) //if last transaction brought the balance below minimum balance
{
balance = (balance - serviceCharge); //apply service charge
}
}
}
return balance;
}
checkingAccount::applyTransactiondoesn’t see or touch the base class’sbalancemember. It’s using, resetting and returning its ownbalanceargument.