Академический Документы
Профессиональный Документы
Культура Документы
Programming
C++ Basic Examples
Bounded Stack Example
Linked List Stack Example
UNIX File ADT Example
Specication for a String ADT
String Class ADT Example
Circular Queue Class Example
Matrix Class Example
1
e.g.,
/* The stack consists of ELEMENT TYPEs. */
typedef int ELEMENT TYPE;
class Stack f
private:
/* Bottom and maximum size of the stack. */
enum fBOTTOM = 0, SIZE = 100g;
/* Keeps track of the current top of stack. */
int stack top;
/* Holds the stack's contents. */
ELEMENT TYPE stack[SIZE];
2
g;
// File stack.h
typedef int ELEMENT TYPE;
class Stack f
public:
/* First 9 member functions are the same */
Stack (void): head (0) fg
Stack (const Stack &s);
Stack &operator= (const Stack &s);
~Stack (void);
void push (ELEMENT TYPE new item);
ELEMENT TYPE pop (void);
ELEMENT TYPE top (void);
int is empty (void) f return this->head == 0; g
int is full (void) f return 0; g
/* New function, for memory management */
static void release free list (void);
:::
:::
g;
g;
Node *head;
void while
Node::free all nodes (void) f
(Node::free list != 0) f
g
void Node::free
Stack::release free list (void) f
all nodes ();
g
10
11
Goals:
1. Improve type-security
2. Improve consistency and ease-of-use
Open
{ Read/write a single
from/to the le
{ Control functions (
etc.)
character
e.g.
or a single
line
Implementation Details:
{ Note: we'll cheat and use the Standard ANSI
C library routines for our implementation
class File
f
public:
13
:::
{
{
{
{
iob
inode
is used to keep
14
? strlen (buf) : 0;
16
// File main.C
#include "le.h"
int copy les (char *read le, char *write le) f
File in (read le, "r");
File out (write le, "w");
int c;
17
str1 = str2;
2. Strings are NUL-terminated
{
{
{
{
{
{
{
str
Hello
len
size
22
/* output operator */
ostream &operator << (ostream &, const String&);
/* Indexing operator */
char &operator[] (int);
int length (void) const;
/* Dynamically allocates a C/C++ string copy */
operator char *() const;
23
g;
26
#endif
g
27
// number to move);
29
30
32
/* Output operator */
ostream &operator << (ostream &stream,
const String &str) f
for (int i = 0; i < str.len; i++)
stream << str[i]; // actually str.operator[] (i);
return stream;
g
/* Min operator */
int String::min (int a, int b) f a > b ? b : a; g
/* Index operator. */
char &String::operator[] (int index) const f
#if dened (EXCEPTIONS)
if (index < 0 jj index >= this->len)
throw (RANGE ERROR);
#else
assert (index >= 0 && index < this->len);
#endif
return this->str[index];
g
34
35
i++)
;
return i < end index ? i : ,1;
g
else f
delta = -delta;
this->copy up (index + s.len, delta);
memcpy (&this->str[index], t.str, t.len);
this->len += delta;
g
return index;
g
#endif /* CHALLENGE */
37
// File queue.h
template <class TYPE> class Queue f
private:
int count, size;
int front, rear;
TYPE *array;
int next (int index);
public:
Queue (int sz = 100): size (sz),
front (0), rear (0), count (0)
array (new TYPE[sz]) fg
~Queue (void) f delete this->array; g
int enqueue (TYPE item);
int dequeue (TYPE &item);
int is empty (void);
int is full (void);
g;
38
39
40
q.dequeue (i);
cout << "dequeueing " << i << "\n";
g
// ~Queue () destructor called here!
41
class Index f
public:
Index (void): i (0) f g
Index (int j): i (j) f g
operator int &() f return i; g
Double Index operator, (Index j) f
return Double Index (this->i, j.i);
g
const Index &operator= (int j) f
this->i = j; return *this;
g
const Index &operator++ (void) f
++this->i; return *this;
g
private:
int i;
g;
42
Two D Matrix m;
43
1
11
21
31
41
51
61
71
81
91
101
111
121
131
141
151
161
171
181
191
2
12
22
32
42
52
62
72
82
92
102
112
122
132
142
152
162
172
182
192
3
13
23
33
43
53
63
73
83
93
103
113
123
133
143
153
163
173
183
193
4
14
24
34
44
54
64
74
84
94
104
114
124
134
144
154
164
174
184
194
5
15
25
35
45
55
65
75
85
95
105
115
125
135
145
155
165
175
185
195
6
16
26
36
46
56
66
76
86
96
106
116
126
136
146
156
166
176
186
196
7
17
27
37
47
57
67
77
87
97
107
117
127
137
147
157
167
177
187
197
8
18
28
38
48
58
68
78
88
98
108
118
128
138
148
158
168
178
188
198
9
19
29
39
49
59
69
79
89
99
109
119
129
139
149
159
169
179
189
199
44