Hi ppl, i am having difficulty in declaring virtual functions for abstract classes. I have this assignment due next week. I am trying to implement types of account of a bank account class. The virtual functions are deposit and withdraw, each behave differently depending on the account type selected by user.
It's not really an issue with virtual functions. I didn't test it, but at the first glance, you seem to have implemented the virtual functions correctly. The problem is that you are not initializing the base class correctly when you instantiate derived classes.
Your base class, Account, is default constructible. And that is fine because you need to be able to create an array of Account type. Your base class can also be constructed with some parameters. When you instantiate derived classes, CheckingAcc, SavingAcc, and VIPAcc, you want to invoke the second constructor(That is the constructor that accept parameters) of the base class.
Also, note that the functions Withdraw() and Deposit() in Account have the first character capitalized. In VIPAcc and SavingAcc, you have 'w'ithdraw() and 'd'eposit() instead of 'W'ithdraw() and 'D'eposit(). You end up creating new virtual functions instead of providing implementation for existing virtual functions. You will want to fix that.
Thanks for pointing that out, I have fixed the capitalizing in pure virtual function declaration.
i have fixed both the issues. Can you tell me why you choose {} for invoking second constructor?
Thanks for clearing that out. My program is compiling fine now.
Now i cannot figure out how to implement virtual functions for different account types.
My assignment tells me an example:
1) For Saving accounts:
a) For every third deposit there is a $1 fee deducted from the account
b) For every third withdraw there is a $2 fee deducted from the account
Does that mean i have to make two new protected variables in Account class (noWithdraws, noDeposits) and set them equal to nCount when function is called and then increment?
> Account, is default constructible. And that is fine because you need to be
> able to create an array of Account type.
`Account' is an abstract class, you can't create objects of type `Account', much less an array. There is no need for it to be default constructible.
In OP's code there is an array of pointers.
> Now i cannot figure out how to implement virtual functions for different
> account types. virtualint Deposit(double &dBalance, unsignedint &nCount) ¿do you deposit a balance? ¿who should be responsible to track the number of operations?
stop coding, go (back) to the paper.
Also, dBalance = dBalance; //A=A
> For every third deposit there is a $1 fee deducted from the account
¿so the bank is charging me for giving them money? ¡what a great system!
> Does that mean i have to make two new protected variables in Account
> class (noWithdraws, noDeposits) and set them equal to nCount when
> function is called and then increment?
¿what kind of question is that? Go ahead, try it, and when you fail come here to ask.
By the way, if you need to post several files, instead upload to github or similar.
> Account, is default constructible. And that is fine because you need to be
> able to create an array of Account type.
`Account' is an abstract class, you can't create objects of type `Account', much less an array. There is no need for it to be default constructible.
Another edit of post::::
Need help with implementation of checkingAcc
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
virtualvoid Withdraw(double &dBalance, unsignedint &nCount)
{
nCount = withdraws;
if (dBalance < 0)
cout << "Cannot withdraw an amount less than $0.00" << endl;
elseif (balance < dBalance)
cout << "Cannot withdraw an amount greater than the current balance" << endl;
else
balance -= dBalance;
withdraws++;
if (balance < 4000)
balance =balance - 5.0;
}
How do i implement this code so fee is deducted only after all the transactions are performed. My code is deducting -5 after each transaction is performed.
Edited: Each time user loses $5. Stupid bank..... :@
Thanks for the help ppl. hopefully m gona 100% on this assignment.
BTW while working on it I found this is a really flawed model of a banking system which made it even more confusing for me.