C++ Classes and Data Structures
Download
Report
Transcript C++ Classes and Data Structures
C++ Classes and Data Structures
Jeffrey S. Childs
Chapter 3
More About Classes
Jeffrey S. Childs
Clarion University of PA
© 2008, Prentice Hall
1
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 )
2
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
3
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
4
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
5
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
6
Example
1 template <class DataType>
2 class Checkbook
3 {
4 public:
5
6
Checkbook( );
Checkbook( float initBalance );
7
void setBalance( float amount );
8
bool writeCheck( const DataType & amount );
9
void deposit( float amount );
10
11
12
float getBalance( ) const;
DataType getLastCheck( ) const;
float getLastDeposit( ) const;
13 private:
.
.
.
7
Constructor Definitions
1
2
3
4
5
6
7
8
9
10
11
12
13
// checkbook.cpp -- The function definitions of the class
// template for the Checkbook
template <class DataType>
Checkbook<DataType>::Checkbook( )
{
}
template <class DataType>
Checkbook<DataType>::Checkbook( float initBalance )
{
balance = initBalance;
}
8
Use of Second
Constructor
1 int main( )
2 {
3
float bal;
4
cout << "Enter your initial balance: ";
5
cin >> bal;
6
7
Checkbook<float> cbook( bal );
8
9
cbook.deposit( 500.00 );
.
.
.
9
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
10
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.
11
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 <class DataType>
8 struct CheckInfo {
9
int numChecks;
10
DataType checks[ CAPACITY ];
11 };
12
Check Array (cont.)
12 template <class DataType>
13 class Checkbook
14 {
15 public:
16
Checkbook( );
17
Checkbook( float initBalance );
18
void setBalance( float amount );
19
bool writeCheck( const DataType & amount );
20
void deposit( float amount ); public section continued
13
Check Array (cont.)
21
22
23
24
25
26
27
28
float getBalance( ) const;
DataType getLastCheck( ) const;
// getLastChecks returns up to CAPACITY checks in a
// CheckInfo struct. The number of checks is also in the
// CheckInfo struct. Checks in the checks array are stored
// in order with the latest check first
CheckInfo<DataType> getLastChecks( ) const;
float getLastDeposit( ) const;
14
Check Array (cont.)
29 private:
30
float balance;
31
int numChecks; // number of checks in array
32
int lastIndex; // the index of the last check that
33
// was written
34
DataType lastChecks[ CAPACITY ]; // saves
35
// checks
36
float lastDeposit;
37 };
38
39 #include "checkbook.cpp"
15
Checkbook.cpp
40
41
42
43
44
45
46
47
48
49
50
51
template <class DataType>
Checkbook<DataType>::Checkbook( )
{
lastIndex = -1;
}
template <class DataType>
Checkbook<DataType>::Checkbook( float initBalance )
{
balance = initBalance;
lastIndex = -1;
}
16
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
17
Checkbook.cpp (cont.)
numChecks: 4
new check: $175
lastIndex
0
1
$225 $250
2
3
4
$100 $125
18
Checkbook.cpp (cont.)
numChecks: 4
new check: $175
lastIndex
0
1
$225 $250
2
3
4
$100 $125
19
Checkbook.cpp (cont.)
numChecks: 5
new check: $175
lastIndex
0
1
$225 $250
2
3
4
$100 $125 $175
20
Checkbook.cpp (cont.)
numChecks: 5
new check: $50
lastIndex
0
1
$225 $250
2
3
4
$100 $125 $175
21
Checkbook.cpp (cont.)
numChecks: 5
new check: $50
lastIndex
0
1
$225 $250
2
3
4
$100 $125 $175
22
Checkbook.cpp (cont.)
numChecks: 5
new check: $50
lastIndex
0
$50
1
$250
2
3
4
$100 $125 $175
23
Checkbook.cpp (cont.)
numChecks: 5
new check: $75
lastIndex
0
$50
1
$250
2
3
4
$100 $125 $175
24
Checkbook.cpp (cont.)
numChecks: 5
new check: $75
lastIndex
0
$50
1
$250
2
3
4
$100 $125 $175
25
Checkbook.cpp (cont.)
numChecks: 5
new check: $75
0
$50
lastIndex
1
2
$75
3
4
$100 $125 $175
26
Checkbook.cpp (cont.)
numChecks: 5
0
$50
lastIndex
1
2
$75
3
4
$100 $125 $175
27
Checkbook.cpp (cont.)
52 template <class DataType>
53 bool Checkbook<DataType>::writeCheck(
54
const DataType & amount )
55 {
56
if ( amount > balance )
57
return false;
58
balance -= amount;
59
60
61
62
63
64
65
66
67
68 }
lastIndex++;
if ( lastIndex == CAPACITY )
lastIndex = 0;
lastChecks[ lastIndex ] = amount;
if ( numChecks != CAPACITY )
numChecks++;
shown next…
return true;
28
Checkbook.cpp (cont.)
52 template <class DataType>
53 bool Checkbook<DataType>::writeCheck(
54
const DataType & amount )
55 {
56
57
58
59
if ( amount > balance )
return false;
balance -= amount;
60
61
62
63
lastIndex++;
if ( lastIndex == CAPACITY )
lastIndex = 0;
lastChecks[ lastIndex ] = amount;
64
65
66
67
68 }
if ( numChecks != CAPACITY )
numChecks++;
return true;
29
Checkbook.cpp (cont.)
52 template <class DataType>
53 bool Checkbook<DataType>::writeCheck(
54
const DataType & amount )
55 {
56
57
58
59
60
61
62
63
if ( amount > balance )
return false;
balance -= amount;
lastIndex++;
if ( lastIndex == CAPACITY )
lastIndex = 0;
lastChecks[ lastIndex ] = amount;
64
if ( numChecks != CAPACITY )
65
numChecks++;
66
67
return true;
68 }
30
Checkbook.cpp (cont.)
69
70
71
72
73
template <class DataType>
DataType Checkbook<DataType>::getLastCheck( ) const
{
return lastChecks[ lastIndex ];
}
The getLastCheck function must be written
differently, but the old clients’ code which use it
will not be affected.
31
Checkbook.cpp (cont.)
74 template <class DataType>
75 CheckInfo<DataType> Checkbook<DataType>::
76
getLastChecks( ) const
77 {
78
CheckInfo<DataType> info;
79
info.numChecks = numChecks;
80
81
82
83
84
85
86
87
88 }
for ( int i = 0, j = lastIndex; i < numChecks; i++, j-- ) {
if ( j == -1 )
j = CAPACITY - 1;
info.checks[ i ] = lastChecks[ j ];
}
return info;
32
Checkbook.cpp (cont.)
74 template <class DataType>
75 CheckInfo<DataType> Checkbook<DataType>::
76
getLastChecks( ) const
77 {
78
79
80
CheckInfo<DataType> info;
info.numChecks = numChecks;
81
for ( int i = 0, j = lastIndex; i < numChecks; i++, j-- ) {
82
if ( j == -1 )
83
j = CAPACITY - 1;
84
info.checks[ i ] = lastChecks[ j ];
85
}
86
Let’s take a look at how this
87
return info;
loop works.
88 }
33
Checkbook.cpp (cont.)
81
82
83
84
85
for ( int i = 0, j = lastIndex; i < numChecks; i++, j-- ) {
if ( j == -1 )
j = CAPACITY - 1;
info.checks[ i ] = lastChecks[ j ];
}
Let’s take a look at how this
loop works.
34
Checkbook.cpp (cont.)
81
82
83
84
85
for ( int i = 0, j = lastIndex; i < numChecks; i++, j-- ) {
if ( j == -1 )
j = CAPACITY - 1;
info.checks[ i ] = lastChecks[ j ];
}
numChecks : 5
lastChecks
0
lastIndex
1
2
$50
$75
0
1
We want checks
from latest to
oldest: $75,
$50, $175, $125,
$100
3
4
$100 $125 $175
2
3
4
info.checks
35
Checkbook.cpp (cont.)
81
82
83
84
85
for ( int i = 0, j = lastIndex; i < numChecks; i++, j-- ) {
if ( j == -1 )
j = CAPACITY - 1;
info.checks[ i ] = lastChecks[ j ];
}
numChecks : 5
lastChecks
0
lastIndex
1
2
$50
$75
0
1
3
4
$100 $125 $175
2
3
4
info.checks
36
Checkbook.cpp (cont.)
81
82
83
84
85
for ( int i = 0, j = lastIndex; i < numChecks; i++, j-- ) {
if ( j == -1 )
j = CAPACITY - 1;
info.checks[ i ] = lastChecks[ j ];
}
numChecks : 5
lastChecks
0
j
1
$50
$75
i
0
1
2
3
4
$100 $125 $175
2
3
4
info.checks
37
Checkbook.cpp (cont.)
81
82
83
84
85
for ( int i = 0, j = lastIndex; i < numChecks; i++, j-- ) {
if ( j == -1 )
j = CAPACITY - 1;
info.checks[ i ] = lastChecks[ j ];
}
numChecks : 5
lastChecks
0
j
1
$50
$75
i
0
1
2
3
4
$100 $125 $175
2
3
4
info.checks
38
Checkbook.cpp (cont.)
81
for ( int i = 0, j = lastIndex; i < numChecks; i++, j-- ) {
82
if ( j == -1 )
83
84
85
j = CAPACITY - 1;
info.checks[ i ] = lastChecks[ j ];
}
numChecks : 5
lastChecks
0
j
1
$50
$75
i
0
1
2
3
4
$100 $125 $175
2
3
4
info.checks
39
Checkbook.cpp (cont.)
81
82
83
for ( int i = 0, j = lastIndex; i < numChecks; i++, j-- ) {
if ( j == -1 )
j = CAPACITY - 1;
84
85
info.checks[ i ] = lastChecks[ j ];
}
numChecks : 5
lastChecks
0
j
1
$50
$75
i
0
1
2
3
4
$100 $125 $175
2
3
4
info.checks
40
Checkbook.cpp (cont.)
81
82
83
for ( int i = 0, j = lastIndex; i < numChecks; i++, j-- ) {
if ( j == -1 )
j = CAPACITY - 1;
84
85
info.checks[ i ] = lastChecks[ j ];
}
numChecks : 5
lastChecks
0
j
1
$50
$75
i
0
1
2
3
4
$100 $125 $175
2
3
4
info.checks
41
Checkbook.cpp (cont.)
81
82
83
for ( int i = 0, j = lastIndex; i < numChecks; i++, j-- ) {
if ( j == -1 )
j = CAPACITY - 1;
84
85
info.checks[ i ] = lastChecks[ j ];
}
numChecks : 5
lastChecks
info.checks
0
j
1
$50
$75
i
0
1
2
3
4
$100 $125 $175
2
3
4
$75
42
Checkbook.cpp (cont.)
81
82
83
84
85
for ( int i = 0, j = lastIndex; i < numChecks; i++, j-- ) {
if ( j == -1 )
j = CAPACITY - 1;
info.checks[ i ] = lastChecks[ j ];
}
numChecks : 5
lastChecks
info.checks
0
j
1
$50
$75
i
0
1
2
3
4
$100 $125 $175
2
3
4
$75
43
Checkbook.cpp (cont.)
81
82
83
84
85
for ( int i = 0, j = lastIndex; i < numChecks; i++, j-- ) {
if ( j == -1 )
j = CAPACITY - 1;
info.checks[ i ] = lastChecks[ j ];
}
numChecks : 5
lastChecks
info.checks
j
0
1
$50
$75
0
i
1
2
3
4
$100 $125 $175
2
3
4
$75
44
Checkbook.cpp (cont.)
81
82
83
84
85
for ( int i = 0, j = lastIndex; i < numChecks; i++, j-- ) {
if ( j == -1 )
j = CAPACITY - 1;
info.checks[ i ] = lastChecks[ j ];
}
numChecks : 5
lastChecks
info.checks
j
0
1
$50
$75
0
i
1
2
3
4
$100 $125 $175
2
3
4
$75
45
Checkbook.cpp (cont.)
81
for ( int i = 0, j = lastIndex; i < numChecks; i++, j-- ) {
82
if ( j == -1 )
83
84
85
j = CAPACITY - 1;
info.checks[ i ] = lastChecks[ j ];
}
numChecks : 5
lastChecks
info.checks
j
0
1
$50
$75
0
i
1
2
3
4
$100 $125 $175
2
3
4
$75
46
Checkbook.cpp (cont.)
81
82
83
for ( int i = 0, j = lastIndex; i < numChecks; i++, j-- ) {
if ( j == -1 )
j = CAPACITY - 1;
84
85
info.checks[ i ] = lastChecks[ j ];
}
numChecks : 5
lastChecks
info.checks
j
0
1
$50
$75
0
i
1
2
3
4
$100 $125 $175
2
3
4
$75
47
Checkbook.cpp (cont.)
81
82
83
for ( int i = 0, j = lastIndex; i < numChecks; i++, j-- ) {
if ( j == -1 )
j = CAPACITY - 1;
84
85
info.checks[ i ] = lastChecks[ j ];
}
numChecks : 5
lastChecks
info.checks
j
0
1
$50
$75
0
i
1
2
3
4
$100 $125 $175
2
3
4
$75
48
Checkbook.cpp (cont.)
81
82
83
for ( int i = 0, j = lastIndex; i < numChecks; i++, j-- ) {
if ( j == -1 )
j = CAPACITY - 1;
84
85
info.checks[ i ] = lastChecks[ j ];
}
numChecks : 5
lastChecks
info.checks
j
0
1
$50
$75
0
i
1
$75
$50
2
3
4
$100 $125 $175
2
3
4
49
Checkbook.cpp (cont.)
81
82
83
84
85
for ( int i = 0, j = lastIndex; i < numChecks; i++, j-- ) {
if ( j == -1 )
j = CAPACITY - 1;
info.checks[ i ] = lastChecks[ j ];
}
numChecks : 5
lastChecks
info.checks
j
0
1
$50
$75
0
i
1
$75
$50
2
3
4
$100 $125 $175
2
3
4
50
Checkbook.cpp (cont.)
81
82
83
84
85
for ( int i = 0, j = lastIndex; i < numChecks; i++, j-- ) {
if ( j == -1 )
j = CAPACITY - 1;
info.checks[ i ] = lastChecks[ j ];
}
numChecks : 5
j : -1
lastChecks
info.checks
0
1
$50
$75
0
1
$75
$50
2
3
4
$100 $125 $175
i
2
3
4
51
Checkbook.cpp (cont.)
81
82
83
84
85
for ( int i = 0, j = lastIndex; i < numChecks; i++, j-- ) {
if ( j == -1 )
j = CAPACITY - 1;
info.checks[ i ] = lastChecks[ j ];
}
numChecks : 5
j : -1
lastChecks
info.checks
0
1
$50
$75
0
1
$75
$50
2
3
4
$100 $125 $175
i
2
3
4
52
Checkbook.cpp (cont.)
81
for ( int i = 0, j = lastIndex; i < numChecks; i++, j-- ) {
82
if ( j == -1 )
83
84
85
j = CAPACITY - 1;
info.checks[ i ] = lastChecks[ j ];
}
numChecks : 5
j : -1
lastChecks
info.checks
0
1
$50
$75
0
1
$75
$50
2
3
4
$100 $125 $175
i
2
3
4
53
Checkbook.cpp (cont.)
81
82
for ( int i = 0, j = lastIndex; i < numChecks; i++, j-- ) {
if ( j == -1 )
83
84
85
j = CAPACITY - 1;
info.checks[ i ] = lastChecks[ j ];
}
numChecks : 5
j : -1
lastChecks
info.checks
0
1
$50
$75
0
1
$75
$50
2
3
4
$100 $125 $175
i
2
3
4
54
Checkbook.cpp (cont.)
81
82
for ( int i = 0, j = lastIndex; i < numChecks; i++, j-- ) {
if ( j == -1 )
83
84
85
j = CAPACITY - 1;
info.checks[ i ] = lastChecks[ j ];
}
numChecks : 5
lastChecks
info.checks
0
1
$50
$75
0
1
$75
$50
2
3
j
4
$100 $125 $175
i
2
3
4
55
Checkbook.cpp (cont.)
81
82
83
for ( int i = 0, j = lastIndex; i < numChecks; i++, j-- ) {
if ( j == -1 )
j = CAPACITY - 1;
84
85
info.checks[ i ] = lastChecks[ j ];
}
numChecks : 5
lastChecks
info.checks
0
1
$50
$75
0
1
$75
$50
2
3
j
4
$100 $125 $175
i
2
3
4
56
Checkbook.cpp (cont.)
81
82
83
for ( int i = 0, j = lastIndex; i < numChecks; i++, j-- ) {
if ( j == -1 )
j = CAPACITY - 1;
84
85
info.checks[ i ] = lastChecks[ j ];
}
numChecks : 5
lastChecks
info.checks
0
1
$50
$75
0
1
$75
$50
2
3
j
4
$100 $125 $175
i
2
3
4
57
Checkbook.cpp (cont.)
81
82
83
for ( int i = 0, j = lastIndex; i < numChecks; i++, j-- ) {
if ( j == -1 )
j = CAPACITY - 1;
84
85
info.checks[ i ] = lastChecks[ j ];
}
numChecks : 5
lastChecks
info.checks
0
1
$50
$75
2
3
j
4
$100 $125 $175
0
1
i
2
$75
$50
$175
3
4
58
Checkbook.cpp (cont.)
81
82
83
84
85
for ( int i = 0, j = lastIndex; i < numChecks; i++, j-- ) {
if ( j == -1 )
j = CAPACITY - 1;
info.checks[ i ] = lastChecks[ j ];
}
numChecks : 5
lastChecks
info.checks
0
1
$50
$75
2
3
j
4
$100 $125 $175
0
1
i
2
$75
$50
$175
3
4
59
Checkbook.cpp (cont.)
81
82
83
84
85
for ( int i = 0, j = lastIndex; i < numChecks; i++, j-- ) {
if ( j == -1 )
j = CAPACITY - 1;
info.checks[ i ] = lastChecks[ j ];
}
numChecks : 5
lastChecks
info.checks
2
j
3
0
1
4
$50
$75
$100 $125 $175
0
1
2
i
3
$75
$50
$175
4
60
Checkbook.cpp (cont.)
81
82
83
84
85
for ( int i = 0, j = lastIndex; i < numChecks; i++, j-- ) {
if ( j == -1 )
j = CAPACITY - 1;
info.checks[ i ] = lastChecks[ j ];
}
numChecks : 5
lastChecks
info.checks
2
j
3
0
1
4
$50
$75
$100 $125 $175
0
1
2
i
3
$75
$50
$175
4
61
Checkbook.cpp (cont.)
81
for ( int i = 0, j = lastIndex; i < numChecks; i++, j-- ) {
82
if ( j == -1 )
83
84
85
j = CAPACITY - 1;
info.checks[ i ] = lastChecks[ j ];
}
numChecks : 5
lastChecks
info.checks
2
j
3
0
1
4
$50
$75
$100 $125 $175
0
1
2
i
3
$75
$50
$175
4
62
Checkbook.cpp (cont.)
81
82
83
for ( int i = 0, j = lastIndex; i < numChecks; i++, j-- ) {
if ( j == -1 )
j = CAPACITY - 1;
84
85
info.checks[ i ] = lastChecks[ j ];
}
numChecks : 5
lastChecks
info.checks
2
j
3
0
1
4
$50
$75
$100 $125 $175
0
1
2
i
3
$75
$50
$175
4
63
Checkbook.cpp (cont.)
81
82
83
for ( int i = 0, j = lastIndex; i < numChecks; i++, j-- ) {
if ( j == -1 )
j = CAPACITY - 1;
84
85
info.checks[ i ] = lastChecks[ j ];
}
numChecks : 5
lastChecks
info.checks
2
j
3
0
1
4
$50
$75
$100 $125 $175
0
1
2
i
3
$75
$50
$175
4
64
Checkbook.cpp (cont.)
81
82
83
for ( int i = 0, j = lastIndex; i < numChecks; i++, j-- ) {
if ( j == -1 )
j = CAPACITY - 1;
84
85
info.checks[ i ] = lastChecks[ j ];
}
numChecks : 5
lastChecks
info.checks
2
j
3
0
1
$50
$75
$100 $125 $175
0
1
i
3
$75
$50
2
4
4
$175 $125
65
Checkbook.cpp (cont.)
81
82
83
84
85
for ( int i = 0, j = lastIndex; i < numChecks; i++, j-- ) {
if ( j == -1 )
j = CAPACITY - 1;
info.checks[ i ] = lastChecks[ j ];
}
numChecks : 5
lastChecks
info.checks
2
j
3
0
1
$50
$75
$100 $125 $175
0
1
i
3
$75
$50
2
4
4
$175 $125
66
Checkbook.cpp (cont.)
81
82
83
84
85
for ( int i = 0, j = lastIndex; i < numChecks; i++, j-- ) {
if ( j == -1 )
j = CAPACITY - 1;
info.checks[ i ] = lastChecks[ j ];
}
numChecks : 5
lastChecks
info.checks
0
1
$50
$75
0
1
$75
$50
j
2
3
4
$100 $125 $175
2
3
i
4
$175 $125
67
Checkbook.cpp (cont.)
81
82
83
84
85
for ( int i = 0, j = lastIndex; i < numChecks; i++, j-- ) {
if ( j == -1 )
j = CAPACITY - 1;
info.checks[ i ] = lastChecks[ j ];
}
numChecks : 5
lastChecks
info.checks
0
1
$50
$75
0
1
$75
$50
j
2
3
4
$100 $125 $175
2
3
i
4
$175 $125
68
Checkbook.cpp (cont.)
81
for ( int i = 0, j = lastIndex; i < numChecks; i++, j-- ) {
82
if ( j == -1 )
83
84
85
j = CAPACITY - 1;
info.checks[ i ] = lastChecks[ j ];
}
numChecks : 5
lastChecks
info.checks
0
1
$50
$75
0
1
$75
$50
j
2
3
4
$100 $125 $175
2
3
i
4
$175 $125
69
Checkbook.cpp (cont.)
81
82
83
for ( int i = 0, j = lastIndex; i < numChecks; i++, j-- ) {
if ( j == -1 )
j = CAPACITY - 1;
84
85
info.checks[ i ] = lastChecks[ j ];
}
numChecks : 5
lastChecks
info.checks
0
1
$50
$75
0
1
$75
$50
j
2
3
4
$100 $125 $175
2
3
i
4
$175 $125
70
Checkbook.cpp (cont.)
81
82
83
for ( int i = 0, j = lastIndex; i < numChecks; i++, j-- ) {
if ( j == -1 )
j = CAPACITY - 1;
84
85
info.checks[ i ] = lastChecks[ j ];
}
numChecks : 5
lastChecks
info.checks
0
1
$50
$75
0
1
$75
$50
j
2
3
4
$100 $125 $175
2
3
i
4
$175 $125
71
Checkbook.cpp (cont.)
81
82
83
for ( int i = 0, j = lastIndex; i < numChecks; i++, j-- ) {
if ( j == -1 )
j = CAPACITY - 1;
84
85
info.checks[ i ] = lastChecks[ j ];
}
numChecks : 5
lastChecks
info.checks
0
1
$50
$75
0
1
$75
$50
j
2
3
4
$100 $125 $175
2
3
i
4
$175 $125 $100
72
Checkbook.cpp (cont.)
81
82
83
84
85
for ( int i = 0, j = lastIndex; i < numChecks; i++, j-- ) {
if ( j == -1 )
j = CAPACITY - 1;
info.checks[ i ] = lastChecks[ j ];
}
numChecks : 5
lastChecks
info.checks
0
1
$50
$75
0
1
$75
$50
j
2
3
4
$100 $125 $175
2
3
i
4
$175 $125 $100
73
Checkbook.cpp (cont.)
81
82
83
84
85
for ( int i = 0, j = lastIndex; i < numChecks; i++, j-- ) {
if ( j == -1 )
j = CAPACITY - 1;
info.checks[ i ] = lastChecks[ j ];
}
numChecks : 5
lastChecks
0
j
1
$50
$75
2
3
4
$100 $125 $175
i:5
info.checks
0
1
$75
$50
2
3
4
$175 $125 $100
74
Checkbook.cpp (cont.)
81
82
83
84
85
for ( int i = 0, j = lastIndex; i < numChecks; i++, j-- ) {
if ( j == -1 )
j = CAPACITY - 1;
info.checks[ i ] = lastChecks[ j ];
}
numChecks : 5
lastChecks
0
j
1
$50
$75
2
3
4
$100 $125 $175
i:5
info.checks
0
1
$75
$50
2
3
4
$175 $125 $100
75
Checkbook.cpp (cont.)
81
82
83
84
85
for ( int i = 0, j = lastIndex; i < numChecks; i++, j-- ) {
if ( j == -1 )
j = CAPACITY - 1;
info.checks[ i ] = lastChecks[ j ];
}
numChecks : 5
lastChecks
DONE!
info.checks
0
j
1
$50
$75
2
3
4
$100 $125 $175
i:5
0
1
$75
$50
2
3
4
$175 $125 $100
76
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
77
Client Program
89 #include <iostream>
90 #include <iomanip>
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 {
78
Client Program (cont.)
99 int main( )
100 {
101 float balance;
102 cout << "Enter the initial balance: $";
103 cin >> balance;
104 Checkbook<float> cb( balance );
105 float amount;
106 int choice;
107 bool checkAccepted = false;
108
109 cout << fixed << showpoint << setprecision( 2 );
110 choice = menu( );
79
Client Program (cont.)
111
112
113
114
115
116
117
118
119
120
121
122
123
124
while ( choice != QUIT ) {
if ( choice == CHECK ) {
cout << "Enter check amount: $";
cin >> amount;
if ( cb.writeCheck( amount ) ) {
cout << "Check accepted." << endl;
checkAccepted = true;
}
else
cout << "Your balance is not high " <<
"enough for that check." << endl;
}
.
. // other choices are written here
.
choice = menu( )
}
80
Client Program (cont.)
111
112
while ( choice != QUIT ) {
if ( choice == CHECK ) {
113
114
cout << "Enter check amount: $";
cin >> amount;
115
116
117
118
119
120
121
122
123
124
if ( cb.writeCheck( amount ) ) {
cout << "Check accepted." << endl;
checkAccepted = true;
}
else
cout << "Your balance is not high " <<
"enough for that check." << endl;
}
.
. // other choices are written here
.
choice = menu( )
}
81
Client Program (cont.)
111
112
while ( choice != QUIT ) {
if ( choice == CHECK ) {
113
114
115
116
117
118
cout << "Enter check amount: $";
cin >> amount;
if ( cb.writeCheck( amount ) ) {
cout << "Check accepted." << endl;
checkAccepted = true;
}
119
120
121
122
else
cout << "Your balance is not high " <<
"enough for that check." << endl;
}
123
124
.
. // other choices are written here
.
choice = menu( )
}
82
Client Program (cont.)
111
112
while ( choice != QUIT ) {
if ( choice == CHECK ) {
113
114
115
116
117
118
119
120
121
cout << "Enter check amount: $";
cin >> amount;
if ( cb.writeCheck( amount ) ) {
cout << "Check accepted." << endl;
checkAccepted = true;
}
else
cout << "Your balance is not high " <<
"enough for that check." << endl;
122
}
123
124
.
. // other choices are written here
.
choice = menu( )
}
83
Client Program (cont.)
125
126
127
128
129
130
131
132
133
134
135
.
.
.
if ( checkAccepted ) {
cout << "Your last check was: $"
<< cb.getLastCheck( ) << endl;
CheckInfo<float> ci;
ci = cb.getLastChecks( );
if ( ci.numChecks > 1 ) {
cout << "Your last checks are:" << endl;
for ( int i = 0; i < ci.numChecks; i++ )
cout << "$" << ci.checks[ i ] << endl;
}
}
84