G.9. Class Transaction

Class TRansaction (Figs. G.15G.16) 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 G.15 expands upon the transaction header file first developed in Section 13.10. Lines 13, 1719 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 bodythis 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 2426 declare the class's private data members. Recall from the class diagram of Fig. 13.29 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. 13.28all 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 will soon see, transaction's constructor initializes these references. Note that the forward declarations in lines 67 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.

Figure G.15. transaction class definition.

(This item is displayed on pages 1304 - 1305 in the print version)

 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

Figure G.16. TRansaction class member-function definitions.

(This item is displayed on pages 1305 - 1306 in the print version)

 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. G.15 and defined in lines 815 of Fig. G.16) 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 functionsgetAccountNumber (declared in line 17 of Fig. G.15 and defined in lines 1821 of Fig. G.16), getScreen (declared in line 18 of Fig. G.15 and defined in lines 2427 of Fig. G.16) and getBankDatabase (declared in line 19 of Fig. G.15 and defined in lines 3033 of Fig. G.16). 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. G.15). 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.

Introduction to Computers, the Internet and World Wide Web

Introduction to C++ Programming

Introduction to Classes and Objects

Control Statements: Part 1

Control Statements: Part 2

Functions and an Introduction to Recursion

Arrays and Vectors

Pointers and Pointer-Based Strings

Classes: A Deeper Look, Part 1

Classes: A Deeper Look, Part 2

Operator Overloading; String and Array Objects

Object-Oriented Programming: Inheritance

Object-Oriented Programming: Polymorphism

Templates

Stream Input/Output

Exception Handling

File Processing

Class string and String Stream Processing

Web Programming

Searching and Sorting

Data Structures

Bits, Characters, C-Strings and structs

Standard Template Library (STL)

Other Topics

Appendix A. Operator Precedence and Associativity Chart

Appendix B. ASCII Character Set

Appendix C. Fundamental Types

Appendix D. Number Systems

Appendix E. C Legacy Code Topics

Appendix F. Preprocessor

Appendix G. ATM Case Study Code

Appendix H. UML 2: Additional Diagram Types

Appendix I. C++ Internet and Web Resources

Appendix J. Introduction to XHTML

Appendix K. XHTML Special Characters

Appendix L. Using the Visual Studio .NET Debugger

Appendix M. Using the GNU C++ Debugger

Bibliography



C++ How to Program
C++ How to Program (5th Edition)
ISBN: 0131857576
EAN: 2147483647
Year: 2004
Pages: 627

Flylib.com © 2008-2020.
If you may any questions please contact us: flylib@qtcs.net