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(){}
};
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.