confused about class in a class in yet another class

Jun 28, 2012 at 11:05pm
I know it's such a basic question, but I am just blinded..

I have a class Account, then a class AccountList that contains an array of type Account, then class Bank that contains AccountList.

in class Account there is a method int get_balance() ,
in class Bank I need to implement a method int total_balance(),
which will add all balances of Accounts that are in the AccountList.

I understand I should run a 'for-loop' through the list and call get_balance() and add them up +=, but I am confused about the actual syntax. I can call methods one level lower no problem though.. any suggestions? Thanks.


this is what i tried and obviously it's wrong

1
2
3
4
5
6
7
8
9
10
11
int Bank::total_balance(){
    
    int totalBalance;
      
    for (int i = 0; i<accountList.size(); i++) {
        totalBalance += accountList.get(i).get_balance();
    }

    return totalBalance;
}

here are the declarations of my classes:

Account.h

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
class Account {
    int account_number;
    int balance;
    
public:
    Account();
    Account(int anr, int b);
    int get_account_number();
    void set_account_number(int anr);
    int get_balance();
    bool set_balance(int b);
    bool debit(int amount);
    bool credit(int amount);
    bool transfer(Account& other, int amount);
    void print();
    void println();
};


AccountList.h

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
class AccountList {
    
    Account aList[3]; // 
    int sz;
    int capacity;
public:
    AccountList();
    bool add(Account& a);
    Account get(int i);
    int size();
    Account remove(int i);
    int find(int anr);
    void println();
    
};


Bank.h

1
2
3
4
5
6
7
8
9
10
11
12
13
class Bank {

    AccountList accountList;
    
public:
    Bank();
    bool add_aacount(Account& a);
    bool remove_account(int i);
    int total_balance();
    void print();
    void println();
    
};
Last edited on Jun 28, 2012 at 11:18pm
Jun 28, 2012 at 11:16pm
You're missing the initialisation:
1
2
3
4
5
6
7
8
9
10
int Bank::total_balance(){
    
    int totalBalance = 0;
      
    for (int i = 0; i<accountList.size(); i++) {
        totalBalance += accountList.get(i).get_balance();
    }

    return totalBalance;
}
Jun 28, 2012 at 11:29pm
I thought it automatically initializes to 0, but yes, c++ is nasty with these things. Thanks. However my problem persists. It returns a 0 all the time. If i call get_balance() of class Account in main, it returns correctly. But in Bank.cpp wont do it.
Last edited on Jun 28, 2012 at 11:38pm
Jun 28, 2012 at 11:33pm
Then what should it return, according to you?
If there are no accounts, it goes without saying that it will return zero.
Jun 28, 2012 at 11:58pm
Well accounts are declared and initialized in main, then added to AccountList. So according to me they are there...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <iostream>
//#include "Account.h"
//#include "AccountList.h"
#include "bank.h"

int main (int argc, const char * argv[])
{
    AccountList al1 = AccountList();
    Account a1 = Account(65,100);
    Account a2 = Account(130,200);
    Account a3 = Account(260,150);
    
    Bank b1 = Bank();
    
    al1.add(a1);
    al1.add(a2);
    al1.add(a3);
    al1.println();

    cout<<a1.get_balance()<<endl;
    cout<<b1.total_balance()<<endl;

    return 0;
    
}


here's what it prints out:

AccountList[(Account(65,100))(Account(130,200))(Account(260,150))]
100
0
Jun 29, 2012 at 12:18am
Where do you add "all" to b1?
Jun 29, 2012 at 12:18am
You're adding the accounts to al1, not to the bank...
Jun 29, 2012 at 12:31am
Alright! I see my mistake now. I had a silly assumption that since Bank contained AccountList, everything would be in the Bank too. But then why would I need add_account() in the Bank.. Thank you guys!
Topic archived. No new replies allowed.