How is this private data member of a base class being accessed by a derived class?
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 c开发者_如何转开发lass. 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::applyTransaction
doesn't see or touch the base class's balance
member. It's using, resetting and returning its own balance
argument.
balance
in that context is the 4th argument to the function (checkingAccount:: applyTransaction
); it is not the data member of bankAccount
.
精彩评论