Class Transaction
(Figs. 23.28–23.29) is an abstract base class that represents the notion of an ATM transaction. It contains the common features of derived classes BalanceInquiry
, Withdrawal
and Deposit
. Figure 23.28 expands upon the Transaction
header file first developed in Section 23.3. Lines 13, 17–19 and 22 contain function prototypes for the class’s constructor and four member functions, which we discuss shortly. Line 15 defines a virtual destructor with an empty body—this makes all derived-class destructors virtual
(even those defined implicitly by the compiler) and ensures that dynamically allocated derived-class objects get destroyed properly when they are deleted via a base-class pointer. Lines 24–26 declare the class’s private
data members. Recall from the class diagram of Fig. 23.11 that class Transaction
contains an attribute accountNumber
(implemented in line 24) that indicates the account involved in the Transaction
. We derive data members screen
(line 25) and bankDatabase
(line 26) from class Transaction
’s associations modeled in Fig. 23.10—all transactions require access to the ATM’s screen and the bank’s database, so we include references to a Screen
and a BankDatabase
as data members of class Transaction
. As you’ll soon see, Transaction
’s constructor initializes these references. The forward declarations in lines 6–7 signify that the header file contains references to objects of classes Screen
and BankDatabase
, but that the definitions of these classes lie outside the header file.
1 // Transaction.h
2 // Transaction abstract base class definition.
3 #ifndef TRANSACTION_H
4 #define TRANSACTION_H
5
6 class Screen; // forward declaration of class Screen
7 class BankDatabase; // forward declaration of class BankDatabase
8
9 class Transaction
10 {
11 public:
12 // constructor initializes common features of all Transactions
13 Transaction( int, Screen &, BankDatabase & );
14
15 virtual ~Transaction() { } // virtual destructor with empty body
16
17 int getAccountNumber() const; // return account number
18 Screen &getScreen() const; // return reference to screen
19 BankDatabase &getBankDatabase() const; // return reference to database
20
21 // pure virtual function to perform the transaction
22 virtual void execute() = 0; // overridden in derived classes
23 private:
24 int accountNumber; // indicates account involved
25 Screen &screen; // reference to the screen of the ATM
26 BankDatabase &bankDatabase; // reference to the account info database
27 }; // end class Transaction
28
29 #endif // TRANSACTION_H
1 // Transaction.cpp
2 // Member-function definitions for class Transaction.
3 #include "Transaction.h" // Transaction class definition
4 #include "Screen.h" // Screen class definition
5 #include "BankDatabase.h" // BankDatabase class definition
6
7 // constructor initializes common features of all Transactions
8 Transaction::Transaction( int userAccountNumber, Screen &atmScreen,
9 BankDatabase &atmBankDatabase )
10 : accountNumber( userAccountNumber ),
11 screen( atmScreen ),
12 bankDatabase( atmBankDatabase )
13 {
14 // empty body
15 } // end Transaction constructor
16
17 // return account number
18 int Transaction::getAccountNumber() const
19 {
20 return accountNumber;
21 } // end function getAccountNumber
22
23 // return reference to screen
24 Screen &Transaction::getScreen() const
25 {
26 return screen;
27 } // end function getScreen
28
29 // return reference to bank database
30 BankDatabase &Transaction::getBankDatabase() const
31 {
32 return bankDatabase;
33 } // end function getBankDatabase
Class Transaction
has a constructor (declared in line 13 of Fig. 23.28 and defined in lines 8–15 of Fig. 23.29) that takes the current user’s account number and references to the ATM’s screen and the bank’s database as arguments. Because Transaction
is an abstract class, this constructor will never be called directly to instantiate Transaction
objects. Instead, the constructors of the Transaction
derived classes will use base-class initializer syntax to invoke this constructor.
Class Transaction
has three public
get functions—getAccountNumber
(declared in line 17 of Fig. 23.28 and defined in lines 18–21 of Fig. 23.29), getScreen
(declared in line 18 of Fig. 23.28 and defined in lines 24–27 of Fig. 23.29) and getBankDatabase
(declared in line 19 of Fig. 23.28 and defined in lines 30–33 of Fig. 23.29). Transaction
derived classes inherit these member functions from Transaction
and use them to gain access to class Transaction
’s private
data members.
Class Transaction
also declares a pure virtual
function execute
(line 22 of Fig. 23.28). It does not make sense to provide an implementation for this member function, because a generic transaction cannot be executed. Thus, we declare this member function to be a pure virtual
function and force each Transaction
derived class to provide its own concrete implementation that executes that particular type of transaction.
3.138.204.8