This repository has been archived by the owner on Oct 4, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Checkbook.h
67 lines (62 loc) · 1.54 KB
/
Checkbook.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
// Lab 4b, Introduction To Dynamic Array Doubling And Deallocation
// Programmer: Minos Park
// Editor(s) used: Sublime Text 2
// Compiler(s) used: G++
#ifndef Checkbook_h
#define Checkbook_h
template <class DataType>
class Checkbook
{
DataType* data;
int* track;
DataType bal;
int size = 2, track_num = 0;
void doubleArray();
public:
Checkbook(const DataType&);
DataType* getChecks() const;
DataType getBalance() const {return bal;}
void writeChecks(const DataType&);
int getNumCheckWritten() const {return track_num;}
void makeDeposit(const DataType& deposit){bal += deposit;}
};
template <class DataType>
Checkbook<DataType>::Checkbook(const DataType& initialBal)
{
data = new DataType[size];
track = new int[size]();
bal = initialBal;
}
template <class DataType>
DataType* Checkbook<DataType>::getChecks() const
{
DataType* dumb = new DataType[size];
for (int i = 0; i < size; i++)
dumb [i] = data [i];
return dumb; // client must free the returned array after use
}
template <class DataType>
void Checkbook<DataType>::writeChecks(const DataType& input)
{
if(track[size - 1]) doubleArray();
bal -= input;
data[track_num] = input;
track[track_num] = 1;
track_num++;
}
template <class DataType>
void Checkbook<DataType>::doubleArray()
{
size *= 2;
DataType* temp = new DataType[size];
for (int i = 0; i < size/2; i++)
temp[i] = data[i];
delete [] data;
data = temp;
int* temp2 = new int[size]();
for (int i = 0; i < size/2; i++)
temp2[i] = track[i];
delete [] track;
track = temp2;
}
#endif