You are here:

C++/Bank account

Advertisement


Question
I 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.


Answer
First 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.  

C++

All Answers


Answers by Expert:


Ask Experts

Volunteer


Ralph McArdell

Expertise

I am a software developer with more than 15 years C++ experience and over 25 years experience developing a wide variety of applications for Windows NT/2000/XP, UNIX, Linux and other platforms. I can help with basic to advanced C++, C (although I do not write just-C much if at all these days so maybe ask in the C section about purely C matters), software development and many platform specific and system development problems.

Experience

My career started in the mid 1980s working as a batch process operator for the now defunct Inner London Education Authority, working on Prime mini computers. I then moved into the role of Programmer / Analyst, also on the Primes, then into technical support and finally into the micro computing section, using a variety of 16 and 8 bit machines. Following the demise of the ILEA I worked for a small company, now gone, called Hodos. I worked on a part task train simulator using C and the Intel DVI (Digital Video Interactive) - the hardware based predecessor to Indeo. Other projects included a CGI based train simulator (different goals to the first), and various other projects in C and Visual Basic (er, version 1 that is). When Hodos went into receivership I went freelance and finally managed to start working in C++. I initially had contracts working on train simulators (surprise) and multimedia - I worked on many of the Dorling Kindersley CD-ROM titles and wrote the screensaver games for the Wallace and Gromit Cracking Animator CD. My more recent contracts have been more traditionally IT based, working predominately in C++ on MS Windows NT, 2000. XP, Linux and UN*X. These projects have had wide ranging additional skill sets including system analysis and design, databases and SQL in various guises, C#, client server and remoting, cross porting applications between platforms and various client development processes. I have an interest in the development of the C++ core language and libraries and try to keep up with at least some of the papers on the ISO C++ Standard Committee site at http://www.open-std.org/jtc1/sc22/wg21/.

Education/Credentials

©2016 About.com. All rights reserved.