Download presentation
Presentation is loading. Please wait.
Published byVerity McCarthy Modified over 8 years ago
1
1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 3 More About Classes Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall
2
2 The const Specifier When added to the end of a function heading, it tells the compiler that no changes should be made to any private members during the execution of that function The use of const specifiers aids in debugging programs: the compiler will catch the error if ( numDependents = 3 )
3
3 The const Specifier (cont.) const can also be used for parameters Objects, or parameters that can be objects (DataType parameters) are often passed by reference for speed –in pass by value, it can take a long time to copy –in pass by reference, only the address is copied The use of const here specifies that the parameter should not change – called passing by const reference
4
4 Rules for Passing Objects Pass objects by value when the function will change them and you don’t want the change to be reflected to the caller Pass objects by reference when you want changes to be reflected to the caller Pass objects by const reference for speed when objects won’t be changed – the compiler will catch mistaken changes
5
5 Constructors Constructors are special class functions that have unusual features: –No return type –Name must be the same as the class name –Called automatically when an object is declared Constructors are commonly used to initialize data members
6
6 Constructors (cont.) More than one constructor can be written for a class; constructors differ by the number and types of parameters A constructor with no parameters is called a default constructor If a constructor is not written by the programmer, the compiler supplies a default constructor which does nothing
7
7 1 template 2 class Checkbook 3 { 4 public: 5Checkbook( ); 6Checkbook( float initBalance ); 7void setBalance( float amount ); 8bool writeCheck( const DataType & amount ); 9void deposit( float amount ); 10float getBalance( ) const; 11DataType getLastCheck( ) const; 12float getLastDeposit( ) const; 13 private:. Example
8
8 1 // checkbook.cpp -- The function definitions of the class 2 // template for the Checkbook 3 4 template 5 Checkbook ::Checkbook( ) 6 { 7 } 8 9 template 10 Checkbook ::Checkbook( float initBalance ) 11 { 12balance = initBalance; 13 } Constructor Definitions
9
9 1 int main( ) 2 { 3float bal; 4cout << "Enter your initial balance: "; 5cin >> bal; 6 7Checkbook cbook( bal ); 8 9cbook.deposit( 500.00 );. Use of Second Constructor
10
10 Modifying Classes The default constructor and the setBalance function now seem removable BUT … the resulting class would not be backward compatible with older clients’ code if we did remove them
11
11 Modifying Classes (cont.) When changing a class, always make sure it will still work with older programs. If the clients need to make minor changes to their programs, they can still compile them with the new class. The next example shows a further modification of the Checkbook class, so that it stores an array of checks.
12
12 Check Array 1 // comments for overloaded operators go here 2 3 const int CAPACITY = 5; 4 5 // The templates for an associated CheckInfo and 6 // Checkbook must match in DataType 7 template 8 struct CheckInfo { 9int numChecks; 10DataType checks[ CAPACITY ]; 11 };
13
13 12 template 13 class Checkbook 14 { 15 public: 16Checkbook( ); 17Checkbook( float initBalance ); 18void setBalance( float amount ); 19bool writeCheck( const DataType & amount ); 20void deposit( float amount ); Check Array (cont.) public section continued
14
14 21float getBalance( ) const; 22DataType getLastCheck( ) const; 23// getLastChecks returns up to CAPACITY checks in a 24// CheckInfo struct. The number of checks is also in the 25// CheckInfo struct. Checks in the checks array are stored 26// in order with the latest check first 27CheckInfo getLastChecks( ) const; 28float getLastDeposit( ) const; Check Array (cont.)
15
15 29 private: 30float balance; 31int numChecks; // number of checks in array 32int lastIndex; // the index of the last check that 33 // was written 34DataType lastChecks[ CAPACITY ]; // saves 35 // checks 36float lastDeposit; 37 }; 38 39 #include "checkbook.cpp" Check Array (cont.)
16
16 Checkbook.cpp 40 template 41 Checkbook ::Checkbook( ) 42 { 43lastIndex = -1; 44 } 45 46 template 47 Checkbook ::Checkbook( float initBalance ) 48 { 49balance = initBalance; 50 lastIndex = -1; 51 }
17
17 Checkbook.cpp (cont.) The CAPACITY of the lastChecks array is set to 5 In the writeCheck function, when the array is filled, new checks are added to the beginning of the array again The array will contain the latest 5 checks This type of array is known as a circular array
18
18 Checkbook.cpp (cont.) $225$250 $100 $125 0 1 2 3 4 lastIndex numChecks: 4 new check: $175
19
19 Checkbook.cpp (cont.) $225$250 $100 $125 0 1 2 3 4 lastIndex numChecks: 4 new check: $175
20
20 Checkbook.cpp (cont.) $225$250 $100 $125$175 0 1 2 3 4 lastIndex numChecks: 5 new check: $175
21
21 Checkbook.cpp (cont.) $225$250 $100 $125$175 0 1 2 3 4 lastIndex numChecks: 5 new check: $50
22
22 Checkbook.cpp (cont.) $225$250 $100 $125$175 0 1 2 3 4 numChecks: 5 new check: $50 lastIndex
23
23 Checkbook.cpp (cont.) $50$250 $100 $125$175 0 1 2 3 4 lastIndex numChecks: 5 new check: $50
24
24 Checkbook.cpp (cont.) $50$250 $100 $125$175 0 1 2 3 4 lastIndex numChecks: 5 new check: $75
25
25 Checkbook.cpp (cont.) $50$250 $100 $125$175 0 1 2 3 4 lastIndex numChecks: 5 new check: $75
26
26 Checkbook.cpp (cont.) $50$75 $100 $125$175 0 1 2 3 4 lastIndex numChecks: 5 new check: $75
27
27 Checkbook.cpp (cont.) $50$75 $100 $125$175 0 1 2 3 4 lastIndex numChecks: 5
28
28 52 template 53 bool Checkbook ::writeCheck( 54const DataType & amount ) 55 { 56if ( amount > balance ) 57return false; 58balance -= amount; 59 60lastIndex++; 61if ( lastIndex == CAPACITY ) 62lastIndex = 0; 63lastChecks[ lastIndex ] = amount; 64if ( numChecks != CAPACITY ) 65numChecks++; 66 67return true; 68 } Checkbook.cpp (cont.) shown next…
29
29 52 template 53 bool Checkbook ::writeCheck( 54const DataType & amount ) 55 { 56if ( amount > balance ) 57return false; 58balance -= amount; 59 60lastIndex++; 61if ( lastIndex == CAPACITY ) 62lastIndex = 0; 63lastChecks[ lastIndex ] = amount; 64if ( numChecks != CAPACITY ) 65numChecks++; 66 67return true; 68 } Checkbook.cpp (cont.)
30
30 52 template 53 bool Checkbook ::writeCheck( 54const DataType & amount ) 55 { 56if ( amount > balance ) 57return false; 58balance -= amount; 59 60lastIndex++; 61if ( lastIndex == CAPACITY ) 62lastIndex = 0; 63lastChecks[ lastIndex ] = amount; 64if ( numChecks != CAPACITY ) 65numChecks++; 66 67return true; 68 } Checkbook.cpp (cont.)
31
31 69 template 70 DataType Checkbook ::getLastCheck( ) const 71 { 72return lastChecks[ lastIndex ]; 73 } Checkbook.cpp (cont.) The getLastCheck function must be written differently, but the old clients’ code which use it will not be affected.
32
32 74 template 75 CheckInfo Checkbook :: 76getLastChecks( ) const 77 { 78CheckInfo info; 79info.numChecks = numChecks; 80 81for ( int i = 0, j = lastIndex; i < numChecks; i++, j-- ) { 82if ( j == -1 ) 83j = CAPACITY - 1; 84info.checks[ i ] = lastChecks[ j ]; 85} 86 87return info; 88 } Checkbook.cpp (cont.)
33
33 74 template 75 CheckInfo Checkbook :: 76getLastChecks( ) const 77 { 78CheckInfo info; 79info.numChecks = numChecks; 80 81for ( int i = 0, j = lastIndex; i < numChecks; i++, j-- ) { 82if ( j == -1 ) 83j = CAPACITY - 1; 84info.checks[ i ] = lastChecks[ j ]; 85} 86 87return info; 88 } Checkbook.cpp (cont.) Let’s take a look at how this loop works.
34
34 81for ( int i = 0, j = lastIndex; i < numChecks; i++, j-- ) { 82if ( j == -1 ) 83j = CAPACITY - 1; 84info.checks[ i ] = lastChecks[ j ]; 85} Checkbook.cpp (cont.) Let’s take a look at how this loop works.
35
35 81for ( int i = 0, j = lastIndex; i < numChecks; i++, j-- ) { 82if ( j == -1 ) 83j = CAPACITY - 1; 84info.checks[ i ] = lastChecks[ j ]; 85} Checkbook.cpp (cont.) $50$75 $100 $125$175 0 1 2 3 4 lastIndex lastChecks 0 1 2 3 4 info.checks We want checks from latest to oldest: $75, $50, $175, $125, $100 numChecks : 5
36
36 81for ( int i = 0, j = lastIndex; i < numChecks; i++, j-- ) { 82if ( j == -1 ) 83j = CAPACITY - 1; 84info.checks[ i ] = lastChecks[ j ]; 85} Checkbook.cpp (cont.) $50$75 $100 $125$175 0 1 2 3 4 lastIndex lastChecks 0 1 2 3 4 info.checks numChecks : 5
37
37 81for ( int i = 0, j = lastIndex; i < numChecks; i++, j-- ) { 82if ( j == -1 ) 83j = CAPACITY - 1; 84info.checks[ i ] = lastChecks[ j ]; 85} Checkbook.cpp (cont.) $50$75 $100 $125$175 0 1 2 3 4 j lastChecks 0 1 2 3 4 info.checks i numChecks : 5
38
38 81for ( int i = 0, j = lastIndex ; i < numChecks; i++, j-- ) { 82if ( j == -1 ) 83j = CAPACITY - 1; 84info.checks[ i ] = lastChecks[ j ]; 85} Checkbook.cpp (cont.) $50$75 $100 $125$175 0 1 2 3 4 j lastChecks 0 1 2 3 4 info.checks i numChecks : 5
39
39 81for ( int i = 0, j = lastIndex; i < numChecks; i++, j-- ) { 82if ( j == -1 ) 83j = CAPACITY - 1; 84info.checks[ i ] = lastChecks[ j ]; 85} Checkbook.cpp (cont.) $50$75 $100 $125$175 0 1 2 3 4 j lastChecks 0 1 2 3 4 info.checks i numChecks : 5
40
40 81for ( int i = 0, j = lastIndex; i < numChecks; i++, j-- ) { 82if ( j == -1 ) 83j = CAPACITY - 1; 84info.checks[ i ] = lastChecks[ j ]; 85} Checkbook.cpp (cont.) $50$75 $100 $125$175 0 1 2 3 4 j lastChecks 0 1 2 3 4 info.checks i numChecks : 5
41
41 81for ( int i = 0, j = lastIndex; i < numChecks; i++, j-- ) { 82if ( j == -1 ) 83j = CAPACITY - 1; 84info.checks[ i ] = lastChecks[ j ]; 85} Checkbook.cpp (cont.) $50$75 $100 $125$175 0 1 2 3 4 j lastChecks 0 1 2 3 4 info.checks i numChecks : 5
42
42 81for ( int i = 0, j = lastIndex; i < numChecks; i++, j-- ) { 82if ( j == -1 ) 83j = CAPACITY - 1; 84info.checks[ i ] = lastChecks[ j ]; 85} Checkbook.cpp (cont.) $50$75 $100 $125$175 0 1 2 3 4 j lastChecks $75 0 1 2 3 4 info.checks i numChecks : 5
43
43 81for ( int i = 0, j = lastIndex; i < numChecks ; i++, j-- ) { 82if ( j == -1 ) 83j = CAPACITY - 1; 84info.checks[ i ] = lastChecks[ j ]; 85} Checkbook.cpp (cont.) $50$75 $100 $125$175 0 1 2 3 4 j lastChecks $75 0 1 2 3 4 info.checks i numChecks : 5
44
44 81for ( int i = 0, j = lastIndex; i < numChecks ; i++, j-- ) { 82if ( j == -1 ) 83j = CAPACITY - 1; 84info.checks[ i ] = lastChecks[ j ]; 85} Checkbook.cpp (cont.) $50$75 $100 $125$175 0 1 2 3 4 j lastChecks $75 0 1 2 3 4 info.checks i numChecks : 5
45
45 81for ( int i = 0, j = lastIndex ; i < numChecks; i++, j-- ) { 82if ( j == -1 ) 83j = CAPACITY - 1; 84info.checks[ i ] = lastChecks[ j ]; 85} Checkbook.cpp (cont.) $50$75 $100 $125$175 0 1 2 3 4 j lastChecks $75 0 1 2 3 4 info.checks i numChecks : 5
46
46 81for ( int i = 0, j = lastIndex; i < numChecks; i++, j-- ) { 82if ( j == -1 ) 83j = CAPACITY - 1; 84info.checks[ i ] = lastChecks[ j ]; 85} Checkbook.cpp (cont.) $50$75 $100 $125$175 0 1 2 3 4 j lastChecks $75 0 1 2 3 4 info.checks i numChecks : 5
47
47 81for ( int i = 0, j = lastIndex; i < numChecks; i++, j-- ) { 82if ( j == -1 ) 83j = CAPACITY - 1; 84info.checks[ i ] = lastChecks[ j ]; 85} Checkbook.cpp (cont.) $50$75 $100 $125$175 0 1 2 3 4 j lastChecks $75 0 1 2 3 4 info.checks i numChecks : 5
48
48 81for ( int i = 0, j = lastIndex; i < numChecks; i++, j-- ) { 82if ( j == -1 ) 83j = CAPACITY - 1; 84info.checks[ i ] = lastChecks[ j ]; 85} Checkbook.cpp (cont.) $50$75 $100 $125$175 0 1 2 3 4 j lastChecks $75 0 1 2 3 4 info.checks i numChecks : 5
49
49 81for ( int i = 0, j = lastIndex; i < numChecks; i++, j-- ) { 82if ( j == -1 ) 83j = CAPACITY - 1; 84info.checks[ i ] = lastChecks[ j ]; 85} Checkbook.cpp (cont.) $50$75 $100 $125$175 0 1 2 3 4 j lastChecks $75 0 1 2 3 4 info.checks i $50 numChecks : 5
50
50 81for ( int i = 0, j = lastIndex; i < numChecks ; i++, j-- ) { 82if ( j == -1 ) 83j = CAPACITY - 1; 84info.checks[ i ] = lastChecks[ j ]; 85} Checkbook.cpp (cont.) $50$75 $100 $125$175 0 1 2 3 4 j lastChecks $75 0 1 2 3 4 info.checks i $50 numChecks : 5
51
51 81for ( int i = 0, j = lastIndex; i < numChecks ; i++, j-- ) { 82if ( j == -1 ) 83j = CAPACITY - 1; 84info.checks[ i ] = lastChecks[ j ]; 85} Checkbook.cpp (cont.) $50$75 $100 $125$175 0 1 2 3 4 j : -1 lastChecks $75 0 1 2 3 4 info.checks i $50 numChecks : 5
52
52 81for ( int i = 0, j = lastIndex ; i < numChecks; i++, j-- ) { 82if ( j == -1 ) 83j = CAPACITY - 1; 84info.checks[ i ] = lastChecks[ j ]; 85} Checkbook.cpp (cont.) $50$75 $100 $125$175 0 1 2 3 4 j : -1 lastChecks $75 0 1 2 3 4 info.checks i $50 numChecks : 5
53
53 81for ( int i = 0, j = lastIndex; i < numChecks; i++, j-- ) { 82if ( j == -1 ) 83j = CAPACITY - 1; 84info.checks[ i ] = lastChecks[ j ]; 85} Checkbook.cpp (cont.) $50$75 $100 $125$175 0 1 2 3 4 lastChecks $75 0 1 2 3 4 info.checks i $50 numChecks : 5 j : -1
54
54 81for ( int i = 0, j = lastIndex; i < numChecks; i++, j-- ) { 82if ( j == -1 ) 83j = CAPACITY - 1; 84info.checks[ i ] = lastChecks[ j ]; 85} Checkbook.cpp (cont.) $50$75 $100 $125$175 0 1 2 3 4 lastChecks $75 0 1 2 3 4 info.checks i $50 numChecks : 5 j : -1
55
55 Checkbook.cpp (cont.) $50$75 $100 $125$175 0 1 2 3 4 j lastChecks $75 0 1 2 3 4 info.checks i $50 numChecks : 5 81for ( int i = 0, j = lastIndex; i < numChecks; i++, j-- ) { 82if ( j == -1 ) 83j = CAPACITY - 1; 84info.checks[ i ] = lastChecks[ j ]; 85}
56
56 81for ( int i = 0, j = lastIndex; i < numChecks; i++, j-- ) { 82if ( j == -1 ) 83j = CAPACITY - 1; 84info.checks[ i ] = lastChecks[ j ]; 85} Checkbook.cpp (cont.) $50$75 $100 $125$175 0 1 2 3 4 j lastChecks $75 0 1 2 3 4 info.checks i $50 numChecks : 5
57
57 Checkbook.cpp (cont.) $50$75 $100 $125$175 0 1 2 3 4 j lastChecks $75 0 1 2 3 4 info.checks i $50 numChecks : 5 81for ( int i = 0, j = lastIndex; i < numChecks; i++, j-- ) { 82if ( j == -1 ) 83j = CAPACITY - 1; 84info.checks[ i ] = lastChecks[ j ]; 85}
58
58 Checkbook.cpp (cont.) $50$75 $100 $125$175 0 1 2 3 4 j lastChecks $75$175 0 1 2 3 4 info.checks i $50 numChecks : 5 81for ( int i = 0, j = lastIndex; i < numChecks; i++, j-- ) { 82if ( j == -1 ) 83j = CAPACITY - 1; 84info.checks[ i ] = lastChecks[ j ]; 85}
59
59 81for ( int i = 0, j = lastIndex; i < numChecks ; i++, j-- ) { 82if ( j == -1 ) 83j = CAPACITY - 1; 84info.checks[ i ] = lastChecks[ j ]; 85} Checkbook.cpp (cont.) $50$75 $100 $125$175 0 1 2 3 4 j lastChecks $75$175 0 1 2 3 4 info.checks i $50 numChecks : 5
60
60 Checkbook.cpp (cont.) $50$75 $100 $125$175 0 1 2 3 4 j lastChecks $75$175 0 1 2 3 4 info.checks i $50 numChecks : 5 81for ( int i = 0, j = lastIndex; i < numChecks ; i++, j-- ) { 82if ( j == -1 ) 83j = CAPACITY - 1; 84info.checks[ i ] = lastChecks[ j ]; 85}
61
61 Checkbook.cpp (cont.) $50$75 $100 $125$175 0 1 2 3 4 j lastChecks $75$175 0 1 2 3 4 info.checks i $50 numChecks : 5 81for ( int i = 0, j = lastIndex ; i < numChecks; i++, j-- ) { 82if ( j == -1 ) 83j = CAPACITY - 1; 84info.checks[ i ] = lastChecks[ j ]; 85}
62
62 Checkbook.cpp (cont.) $50$75 $100 $125$175 0 1 2 3 4 j lastChecks $75$175 0 1 2 3 4 info.checks i $50 numChecks : 5 81for ( int i = 0, j = lastIndex; i < numChecks; i++, j-- ) { 82if ( j == -1 ) 83j = CAPACITY - 1; 84info.checks[ i ] = lastChecks[ j ]; 85}
63
63 81for ( int i = 0, j = lastIndex; i < numChecks; i++, j-- ) { 82if ( j == -1 ) 83j = CAPACITY - 1; 84info.checks[ i ] = lastChecks[ j ]; 85} Checkbook.cpp (cont.) $50$75 $100 $125$175 0 1 2 3 4 j lastChecks $75$175 0 1 2 3 4 info.checks i $50 numChecks : 5
64
64 Checkbook.cpp (cont.) $50$75 $100 $125$175 0 1 2 3 4 j lastChecks $75$175 0 1 2 3 4 info.checks i $50 numChecks : 5 81for ( int i = 0, j = lastIndex; i < numChecks; i++, j-- ) { 82if ( j == -1 ) 83j = CAPACITY - 1; 84info.checks[ i ] = lastChecks[ j ]; 85}
65
65 Checkbook.cpp (cont.) $50$75 $100 $125$175 0 1 2 3 4 j lastChecks $75$175$125 0 1 2 3 4 info.checks i $50 numChecks : 5 81for ( int i = 0, j = lastIndex; i < numChecks; i++, j-- ) { 82if ( j == -1 ) 83j = CAPACITY - 1; 84info.checks[ i ] = lastChecks[ j ]; 85}
66
66 81for ( int i = 0, j = lastIndex; i < numChecks ; i++, j-- ) { 82if ( j == -1 ) 83j = CAPACITY - 1; 84info.checks[ i ] = lastChecks[ j ]; 85} Checkbook.cpp (cont.) $50$75 $100 $125$175 0 1 2 3 4 j lastChecks $75$175$125 0 1 2 3 4 info.checks i $50 numChecks : 5
67
67 Checkbook.cpp (cont.) $50$75 $100 $125$175 0 1 2 3 4 j lastChecks $75$175$125 0 1 2 3 4 info.checks i $50 numChecks : 5 81for ( int i = 0, j = lastIndex; i < numChecks ; i++, j-- ) { 82if ( j == -1 ) 83j = CAPACITY - 1; 84info.checks[ i ] = lastChecks[ j ]; 85}
68
68 Checkbook.cpp (cont.) $50$75 $100 $125$175 0 1 2 3 4 j lastChecks $75$175$125 0 1 2 3 4 info.checks i $50 numChecks : 5 81for ( int i = 0, j = lastIndex ; i < numChecks; i++, j-- ) { 82if ( j == -1 ) 83j = CAPACITY - 1; 84info.checks[ i ] = lastChecks[ j ]; 85}
69
69 Checkbook.cpp (cont.) $50$75 $100 $125$175 0 1 2 3 4 j lastChecks $75$175$125 0 1 2 3 4 info.checks i $50 numChecks : 5 81for ( int i = 0, j = lastIndex; i < numChecks; i++, j-- ) { 82if ( j == -1 ) 83j = CAPACITY - 1; 84info.checks[ i ] = lastChecks[ j ]; 85}
70
70 81for ( int i = 0, j = lastIndex; i < numChecks; i++, j-- ) { 82if ( j == -1 ) 83j = CAPACITY - 1; 84info.checks[ i ] = lastChecks[ j ]; 85} Checkbook.cpp (cont.) $50$75 $100 $125$175 0 1 2 3 4 j lastChecks $75$175$125 0 1 2 3 4 info.checks i $50 numChecks : 5
71
71 Checkbook.cpp (cont.) $50$75 $100 $125$175 0 1 2 3 4 j lastChecks $75$175$125 0 1 2 3 4 info.checks i $50 numChecks : 5 81for ( int i = 0, j = lastIndex; i < numChecks; i++, j-- ) { 82if ( j == -1 ) 83j = CAPACITY - 1; 84info.checks[ i ] = lastChecks[ j ]; 85}
72
72 Checkbook.cpp (cont.) $50$75 $100 $125$175 0 1 2 3 4 j lastChecks $75$175$125$100 0 1 2 3 4 info.checks i $50 numChecks : 5 81for ( int i = 0, j = lastIndex; i < numChecks; i++, j-- ) { 82if ( j == -1 ) 83j = CAPACITY - 1; 84info.checks[ i ] = lastChecks[ j ]; 85}
73
73 81for ( int i = 0, j = lastIndex; i < numChecks ; i++, j-- ) { 82if ( j == -1 ) 83j = CAPACITY - 1; 84info.checks[ i ] = lastChecks[ j ]; 85} Checkbook.cpp (cont.) $50$75 $100 $125$175 0 1 2 3 4 j lastChecks $75$175$125$100 0 1 2 3 4 info.checks i $50 numChecks : 5
74
74 Checkbook.cpp (cont.) $50$75 $100 $125$175 0 1 2 3 4 j lastChecks $75$175$125$100 0 1 2 3 4 info.checks i : 5 $50 numChecks : 5 81for ( int i = 0, j = lastIndex; i < numChecks ; i++, j-- ) { 82if ( j == -1 ) 83j = CAPACITY - 1; 84info.checks[ i ] = lastChecks[ j ]; 85}
75
75 81for ( int i = 0, j = lastIndex ; i < numChecks; i++, j-- ) { 82if ( j == -1 ) 83j = CAPACITY - 1; 84info.checks[ i ] = lastChecks[ j ]; 85} Checkbook.cpp (cont.) $50$75 $100 $125$175 0 1 2 3 4 j lastChecks $75$175$125$100 0 1 2 3 4 info.checks i : 5 $50 numChecks : 5
76
76 81for ( int i = 0, j = lastIndex; i < numChecks; i++, j-- ) { 82if ( j == -1 ) 83j = CAPACITY - 1; 84info.checks[ i ] = lastChecks[ j ]; 85} Checkbook.cpp (cont.) $50$75 $100 $125$175 0 1 2 3 4 j lastChecks $75$175$125$100 0 1 2 3 4 info.checks i : 5 $50 DONE! numChecks : 5
77
77 Data Translation The class interface (public section) is sometimes involved in doing this kind of data translation The data is translated from a form convenient for the class programmer to a form convenient for the client –another reason it is good that the client doesn’t have access to private data
78
78 Client Program 89 #include 90 #include 91 #include "checkbook.h" 92 93 using namespace std; 94 95 int menu( ); 96 97 const int CHECK = 1, DEPOSIT = 2, BALANCE = 3, QUIT = 4; 98 99 int main( ) 100 {
79
79 99 int main( ) 100 { 101float balance; 102cout << "Enter the initial balance: $"; 103cin >> balance; 104Checkbook cb( balance ); 105float amount; 106int choice; 107bool checkAccepted = false; 108 109cout << fixed << showpoint << setprecision( 2 ); 110choice = menu( ); Client Program (cont.)
80
80 111while ( choice != QUIT ) { 112 if ( choice == CHECK ) { 113 cout << "Enter check amount: $"; 114 cin >> amount; 115 if ( cb.writeCheck( amount ) ) { 116cout << "Check accepted." << endl; 117checkAccepted = true; 118} 119 else 120cout << "Your balance is not high " << 121 "enough for that check." << endl; 122 }.. // other choices are written here. 123choice = menu( ) 124} Client Program (cont.)
81
81 111while ( choice != QUIT ) { 112 if ( choice == CHECK ) { 113 cout << "Enter check amount: $"; 114 cin >> amount; 115 if ( cb.writeCheck( amount ) ) { 116cout << "Check accepted." << endl; 117checkAccepted = true; 118} 119 else 120cout << "Your balance is not high " << 121 "enough for that check." << endl; 122 }.. // other choices are written here. 123choice = menu( ) 124} Client Program (cont.)
82
82 111while ( choice != QUIT ) { 112 if ( choice == CHECK ) { 113 cout << "Enter check amount: $"; 114 cin >> amount; 115 if ( cb.writeCheck( amount ) ) { 116cout << "Check accepted." << endl; 117checkAccepted = true; 118} 119 else 120cout << "Your balance is not high " << 121 "enough for that check." << endl; 122 }.. // other choices are written here. 123choice = menu( ) 124} Client Program (cont.)
83
83 111while ( choice != QUIT ) { 112 if ( choice == CHECK ) { 113 cout << "Enter check amount: $"; 114 cin >> amount; 115 if ( cb.writeCheck( amount ) ) { 116cout << "Check accepted." << endl; 117checkAccepted = true; 118} 119 else 120cout << "Your balance is not high " << 121 "enough for that check." << endl; 122 }.. // other choices are written here. 123choice = menu( ) 124} Client Program (cont.)
84
84 125if ( checkAccepted ) { 126 cout << "Your last check was: $" 127<< cb.getLastCheck( ) << endl; 128 CheckInfo ci; 129 ci = cb.getLastChecks( ); 130 if ( ci.numChecks > 1 ) { 131cout << "Your last checks are:" << endl; 132for ( int i = 0; i < ci.numChecks; i++ ) 133cout << "$" << ci.checks[ i ] << endl; 134} 135 }. Client Program (cont.)
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.