Hello

I am slowly learning more and more about C++ and more importantly programming technique and I have reached the wall that I am sure quite a few before me have probably hit - refrences/pointers and management of the heap. Whilst I try and familiarize myself with the above, I am trying to stay ontop of the stuff that I have already learnt, but in doing this have met another wall in which I believe is quite important to get past.

I have basically made a bank account program with help from another question I asked here, and have got to the point where I would like to implement a createNewAccount() method, which at first seemed pretty simple to me until I had a good think about it and how it would be done.

The problem I am having is creating objects inside of the program, is this even possible? From my understanding, each bankAccount must have its own object named appropriatly and this is what I am struggling to achieve. For example, for my case I could just use the accountID for the name of each object and have a method as following which would in turn produce the constructor which would sort everything else out.

void createNewAccount()
{
    bankAccount lastAccNumber++
}

Is this above even possible? Can you make an object using the contents of variables in this fashion? Or must it always be a predefined string?

If the above is actually do able, as said above it would work in my case, but what do you do if the object you are using that does have a incremental key field like for example Flowers or Coins?

I know it is a very large question and probably not explained well, but the topic is really something I need to understand and any help on it would be greatly appreicated.

Thank you

class bankAccount
{
public:
    bankAccount() : accNumber(lastAccNumber++), accBalance(0) {};
    int getNumber()  const { return accNumber; }
    int getBalance() const { return accBalance; }
    void plusBalance(int amountAdded) { accBalance = accBalance + amountAdded; }
    void minusBalance(int amountMinus) { accBalance -= amountMinus; }

private:
    int accNumber;
    int accBalance;
    static int lastAccNumber;
};

int bankAccount::lastAccNumber = 1;

The problem I am having is creating objects inside of the program, is this even possible?

Yes, of course it's possible. Otherwise objects would be nigh useless.

Is this above even possible?

Not without enough difficulty to make it impractical. What you probably want are unnamed objects, or a collection of them in this case. For that there are collection classes such as std::vector. For example:

#include <iostream>
#include <vector>

class bankAccount
{
public:
    bankAccount() : accNumber(lastAccNumber++), accBalance(0) {};
    int getNumber()  const { return accNumber; }
    int getBalance() const { return accBalance; }
    void plusBalance(int amountAdded) { accBalance = accBalance + amountAdded; }
    void minusBalance(int amountMinus) { accBalance -= amountMinus; }
private:
    int accNumber;
    int accBalance;
    static int lastAccNumber;
};

int bankAccount::lastAccNumber = 1;

int main()
{
    std::vector<bankAccount> accounts;

    // Create 10 accounts
    for (int i = 0; i < 10; ++i)
        accounts.push_back(bankAccount());

    // Credit the 3rd account
    accounts[2].plusBalance(123);
    accounts[2].plusBalance(5);
    accounts[2].plusBalance(11);

    // Get current info for the 3rd account
    std::cout<<"Account #"<< accounts[2].getNumber() <<'\n'
             <<"Balance: "<< accounts[2].getBalance() <<'\n';
}

You can learn more about std::vector here.

Ah that makes perfect sense and I have got a working system up using that now. One question I do have is the way in which you define the vector, is it safe/good programming structure to declare it outside the main as below?

vector<bankAccount> vAccounts;

void createAccount()
{
    vAccounts.push_back(bankAccount());
}

int main()
{
    // the main
}

is it safe/good programming structure to declare it outside the main as below?

Global variables are generally perceived as poor practice because anything and everything can touch them. This makes debugging exceptionally difficult. It's recommended that you pass the variables around as function arguments if possible:

void createAccount(vector<bankAccount>& vAccounts)
{
    vAccounts.push_back(bankAccount());
}

int main()
{
    vector<bankAccount> vAccounts;

    ...

    createAccount(vAccounts);

    ...
}

Ah okay, I don't know why I put it outside the main anyway, not thinking I guess.

Thanks for your help, really appreciate it!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.