C++/Bank account
Expert: Ralph McArdell - 7/30/2006
QuestionI was working on this program C++ call bank account.
The program should have 3 user inputs. Account name, Account number & Enter amount.
Problem : The account number should start with 12345 for saving account and 22345 for current account.The first number will determine which account is it.
example. when i key in account number 12345, it will derived to another class call saving account.
when i key in account number 22345, it will derived to another class call current account.
Can you please help me on this. Thank you.
AnswerFirst I going to state upfront that I am not going to write any code or develop your program too much for you. What I will do is give you hint on how to get from the account number to the required type.
It seems to me that you will require a base class or a base interface (a class containing only public pure virtual methods), called maybe Account or IAccount (the I prefix indicating an interface) or AccountBase or something similar. I shall use the name Account.
From this base type you derive two class types: CurrentAccount and SavingsAccount. The following is a very simple diagram of the class hierarchy. You need to view my answer using a fixed space font such as Courier or Courier New otherwise the spacing of the characters will be wrong (as it will most likely be if viewed on the AllExperts website):
!-CurrentAccount
Account<-!
!-SavingsAccount
The <- indicates a generalisation/specialisation relationship thus:
Generalisation <- Specialisation
That is the base class is a generalisation of the classes derived from it and they are specialisations of the base class.
Remember that inheritance forms an is-a relationship. Thus CurrentAccount is an Account and SavingsAccount is an Account. This is important.
One point to mention: the phrasing of your question indicated that you may think you can derive new types on the fly at runtime:
"when i key in account number 12345, it will
derived to another class call saving account"
This is not possible in C++. In C++ you define all these types when you write the code. They have to exist when you compile the code. C++ is a statically typed language which means that all types have to be defined by compile time. You cannot dynamically create new types at runtime.
Now we need to create some code that can convert an account number into an account of the correct type. The easiest way is to use a function. Function that do this sort of thing are often called a factory functions because they create new objects.
The function will be given an account number and return a new account object of the type matching the account number. I am going to keep account numbers as character strings - they have little use as integers, and some accounts I have include letters in the account 'number'. It will also make it easy to determine the type of account required. To indicate that the account number is not being used as a number in the arithmetic sense I use the term account id instead. The outline for this function is as follows:
Account MakeAccountFromAccountId( string accountId )
Begin
Enum AccountTypeIndicatorType { Savings='1', Current='2' }
Account newAccount(null)
Switch on accountId[0]
Begin
Case Savings:
newAccount = new SavingsAccount( accountId )
Break
Case Current:
newAccount = new CurrentAccount( accountId )
Break
Default:
Throw Bad Account Type Exception
End Switch
Return newAccount
End Function
The above does not take into account (no pun intended) the C++ type system. It would translate better into Java or C#. The return type and type of newAccount for example should be Account * and the accountId string should be passed as a const reference (if it is a std::string or similar) or a const char * if it is a C string.
The function MakeAccountFromAccountId takes the account id string and returns an Account object - or rather a reference to an Account object. In fact in C++ it is more natural to return a pointer type, which is still a reference type in the broader sense. Which exact type of Account is returned depends on the accountId parameter - this is polymorphism in action.
The first thing I do is define enumerated values for the account type indication characters. It is always good practice to give generic values names that are relevant to the problem / solution domain.
Next I switch on the first character of the account id string. If it is the value for savings accounts newAccount receives a pointer to a new SavingsAccountObject and if it is the value for a current account newAccount receives a pointer to a new CurrentAccount object. If it is neither an exception is thrown.
Finally the new account object is returned.
I pass the accountId value to each of the specific account type constructors as I guess this is a likely item they will require.
I hope this points you in the right direction.