Hello

I am designing a bank account program to retouch on where I left C++ at and am having a few problems. I wish to have a unique account number for each bank account which is represented by an object e.g. account1, account2, account3. I am struggling to implement something which will make the account numbers unique because i do not know how to track the already previously stated account numbers.

class bankAccount
{
public:
    bankAccount();
    ~bankAccount() {}
    int getNumber() { return accountNumber; }
    int getBalance() { return accountBalance; }

private:
    int accountNumber;
    int accountBalance;
    int setAccountNumber();
    int lastAccountNumber();
};

Above is my class decleration, and as you can see I was going to try and put in something that made a variable in each object with the previous accountNumber and then use a simple increment function to make it go up once each time a new account was made. I am decieded that this is probably not the best way, and there must be a way to do it using refrences/pointers and overiding the ++ operator to increment the accountNumber.

Any help on the matter or advice in which direction to go would be greatly appreicated

There are a number of ways to do this, depending on the features you require.

If all you want is a unique account number for each new account that is created, you can control that with a factory function and a static "last" account number being tracked. In that case, you probably also want to make the account non-copyable and only carry the account through a smart pointer (shared_ptr) which you can get either from the standard library (if your compiler is recent enough), or through TR1 (extension to standard library), or through the Boost library. Here is a simple example of what I described:

class bankAccount
{
public:
    ~bankAccount() {}
    int getNumber() { return accountNumber; }
    int getBalance() { return accountBalance; }
private:
    // private constructor:
    bankAccount(int aAccountNumber) : accountNumber(aAccountNumber), accountBalance(0) { };

    bankAccount(const bankAccount&); // non-copyable
    bankAccount& operator=(const bankAccount&); // non-assignable

    int accountNumber;
    int accountBalance;
public:
    // public factory function:
    static std::shared_ptr<bankAccount> CreateNewAccount();
};

// in the cpp file:

std::shared_ptr<bankAccount> bankAccount::CreateNewAccount() {
    static int lastAccountNumber = 0;  // static 'last' account number.
    return std::shared_ptr<bankAccount>(new bankAccount(lastAccountNumber++));
};

And that's it. Because the variable lastAccountNumber is static within the factory function, it will only be initialized once, upon the first call to the factory function, and will conserve its value afterwards, incrementing at each new account being issued.

If you want more features, such as being able to look-up a particular account by number, then you need a class that handles the list of existing accounts. You could call that class the "bank" class, as it (and it alone) can issue new accounts, and keeps track of them. A simple way to do that is as follows:

class bank; // forward-declaration

class bankAccount
{
public:
    ~bankAccount() {}
    int getNumber() { return accountNumber; }
    int getBalance() { return accountBalance; }

    friend class bank;  // befriend the 'bank' class.
private:
    // private constructor:
    bankAccount(int aAccountNumber) : accountNumber(aAccountNumber), accountBalance(0) { };

    bankAccount(const bankAccount&); // non-copyable
    bankAccount& operator=(const bankAccount&); // non-assignable

    int accountNumber;
    int accountBalance;
};

class bank {
private:
    std::vector< std::shared_ptr< bankAccount > > accounts;
public:
    std::shared_ptr< bankAccount > issueNewAccount() {
        accounts.push_back(std::shared_ptr< bankAccount >(new bankAccount(accounts.size()));
        return accounts.back();
    };

    std::shared_ptr< bankAccount > getAccount(int aAccountNumber) const {
        return accounts[aAccountNumber];
    };

    // ...
};

You could also make the bank class a Singleton class. And then, there are obviously a number of variations to these schemes depending on the features you want.

You could use a static variable to manage the count for you. Fore example:

class Account {                                                                 
    static int next_account_number;                                             
    int account_number;                                                         
public:                                                                         
    Account () : account_number(++next_account_number) {}                       
    int Number () const { return account_number; }                              
};                                                                              

int Account::next_account_number = 0;                                           

int main () {                                                                   
    Account a, b, c, d;                                                         
    std::cout << "A : " << a.Number () << std::endl;                            
    std::cout << "B : " << b.Number () << std::endl;                            
    std::cout << "C : " << c.Number () << std::endl;                            
    std::cout << "D : " << d.Number () << std::endl;                            
    return 0;                                                                   
}                       

The next_account_number is shared among all instances of the class while the account_number is unique to each. One drawback to this approach is that you have to initialize the static variable outside of the class definition.

Thanks for both the answers, I worked out myself the idea of just making a global variable and incrementing it each time an account was made, but I figured that was bad programming practice.

I really should have known about the use of static in this situation!

Thanks for the help

This article has been dead for over six months. Start a new discussion instead.