0

Just so this does not seem too out of context and so I don't have to re-post ALL my code, here is my original question: Object is initializing to unwanted value

Question: How do I design the following code, within the for loop, so that it is a Transaction method, such as void Transaction::promptUser() and then asks the user whether they would like to perform a transaction on their checking or savings account? It will then ask what transaction they will want to do and it will affect the corresponding account.

   int main () {
 BankAccount checking(0.00);
    BankAccount savings(0.00);
    Transaction c(checking);
    Transaction s(savings);
    for(int i = 0; i < 10 ; i++) {
        cout << "Make an option" << endl;
        cout << "1. Checking "   << endl;
        cout << "2. Savings"     << endl;

        int choice;
        cin >> choice;
        if (choice == 1) {
            c.prompt();
            c.printReciept();
        }
        else {
            s.prompt();
            s.printReciept();
        }
    }
}
Community
  • 1
  • 1
Maxim
  • 725
  • 1
  • 8
  • 24
  • 1
    I saw your other question, which was resolved, but this one doesn't make much sense (and is lacking a question mark). – chris Sep 09 '12 at 05:03
  • 1
    So, what is the question, again? – Kuba hasn't forgotten Monica Sep 09 '12 at 05:05
  • Sorry, Here is a more formal question to my problem: How can I make a function that chooses, conditionally, which bank account (i.e checking or savings) to use for the transaction? OR should I make another class that has this function as a member? – Maxim Sep 09 '12 at 05:05
  • I have no idea what the question is here. It's also not acceptable to "reask" the same question again in hopes of getting a different answer ("It already has an answer so I am re-asking in order to get a fresh answer."). You need to edit this to ask an actual question (that is different from the other one), and *edit the question* instead of posting information in comments. (Adding that information to the question itself means that people will see it without having to read all the comments, and it makes your question more clear. It also reduces noise and clutter.) – Ken White Sep 09 '12 at 05:08
  • This is actually a different question than his original. I concur he should post that comment up in the actual question. but likewise would have been better off to forget the aforementioned prior question and simply state his new problem with sample attached. And, we would likely need complete code so observers of *this* question have context without having to dive into the latter. – WhozCraig Sep 09 '12 at 05:11
  • I am new to this forum as well as asking programming questions in general, for that matter. I admit that I make mistakes and would appreciate all criticism as long as it is constructive and directed towards me. Thank you. – Maxim Sep 09 '12 at 05:15

2 Answers2

2

Create an Account class which implements printReciept() and other methods common to any account type. Then create CheckingAccount and SavingsAccount inheriting from Account.

Joshua Berry
  • 2,230
  • 3
  • 21
  • 24
1

I can point out one thing you better do right now:

class Transaction {
private:
    BankAccount m_bao;
public:
    Transaction(BankAccount&);
    void displayOptions();  
    void printReciept();
};

That BankAccount needs to be by reference (pointer or ref, makes no difference to me, but I'm a ref-kinda-guy). It should be a reference and initialized at construction in the object-initializer list:

class Transaction {
private:
    BankAccount& m_bao;
public:
    Transaction(BankAccount&);
    void displayOptions();  
    void printReciept();
};

and

Transaction::Transaction(BankAccount& bao)
 : m_bao(bao)
{
}

I hope it is clear why this is the case. Each transaction was making a copy of the bank account, then modifying that copy.The original account was untouched. Bad. You likely discovered this but were unclear as to why it was happening, thus the Transaction objects were kept out of the for-loop. In actuality, your transactions were holding the accumulated modifications to their own internal bank account copy. The original was unmodified.

WhozCraig
  • 65,258
  • 11
  • 75
  • 141
  • While this was not a direct answer to my question, it was useful advice. I am always anxious at the idea of returning/using references but I am guessing that in this case, initializing m_bao(bao) would be correct compared to m_bao=bao, am I close? – Maxim Sep 09 '12 at 05:21
  • You can't even do the latter if the member is a reference. you *must* initialize it in the initializer-list, which is the correct thing to do. What you had prior was creating a default-constructed bank account in the transaction object, then firing the default assignment operator to copy in the parameter, which remained untouched after that. Play around in a debugger with the above code to see, and above all, keep plugging. everyone here learns at some time how to do this. – WhozCraig Sep 09 '12 at 05:23