oop initialize objects and passing of objects b/w classes

I have 2 questions, both are asked in comments in:

1)class ATM constructor ATM()
2)class Withdraw constructor

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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
class ATM{

 BankDatabase bankDb;
 int accountNo;
 Screen screen;
 Dialpad dialpad;

 public:
    ATM()
    {
    	// is this correct use?
    	// any BETTER method?
    	BankDatabase TempDb();
        bankDb = TempDb
        // similarly for Screen and Dialpad
        screen = ??
        dialpad = ??
    }

    DoWithdraw()
    {
    	Withdraw withdraw(bankDb , accountNo , screen);

    	withdraw.doSomething();// doSomething makes use of bankDb and accountNo.
    }
};

class Withdraw{
 BankDatabase bankDb;
 int accountNo;
 Screen screen;
// will bankDb be the same ATM::bankDb ? and similarly for screen
  public:
    Withdraw(BankDatabase db , int acc , Screen sc):bankDb(db),accountNo(acc),screen(sc){}
};

class BankDatabase{
   vector<Account>account;

   public:
   	BankDatabase() {}

};

class Account{
	int accNo, minBalance , currBalance;

	public:
        Account(int account , int balance):accNo(account),currBalance(balance){}
};

class Screen{
   public:
    Screen(){}
};

class Dialpad{
   public:
    Dialpad(){}
};
Last edited on
Your BankDatabase probably should not be a member of your ATM class. A Bank typically has more than one ATM. Each ATM would not have a copy of the bank's database. When looking at class relationships, apply the "has a" or "is a" rule. A bank "has a" database and a bank "has a" ATM.

Lines 13-14: No need to create a local copy just to assign it. BankDatabase's default constructor is invoked at line 3. Ditto for screen and dialpad.

// will bankDb be the same ATM::bankDb ? and similarly for screen

No, those are separate instances. You're passing BankDatabase by value, which means it's copied. Any changes that Withdraw makes to the copy won't be reflected in the original. You want to pass by reference instead.




> BankDatabase TempDb();
that's a function declaration. The function `TempDb()' returns a `BankDatabase' object and does not take any parameter.

BankDatabase TempDb; would create an object using the default constructor
or BankDatabase TempDb{}; if you prefer (c++11)


I don't understand where your confusion comes from, as you are using initialization list in `Withdraw' and `Account'


> When looking at class relationships, apply the "has a" or "is a" rule.
> A bank "has a" database and a bank "has a" ATM.
an ATM "uses" a database
am i right with the below concept ?

suppose BankDatabase took 3 arguments : arg1, arg2, arg3 then there could be 2 methods to initialize it :

1)
1
2
3
BankDatabase TempDb(arg1,arg2,arg3);
    bankDb = TempDb
    

assuming we have copy constructor for BankDatabase.

2) ATM():BankDb(arg1,arg2,arg3){}

And for withdraw ctor:
Withdraw(BankDatabase &db , int &acc , Screen &sc):bankDb(db),accountNo(acc),screen(sc){}
Last edited on
Topic archived. No new replies allowed.