Object-Oriented Design and Programming C++ Basic Examples Bounded Stack Example Linked List Stack Example UNIX File ADT Exam...
Object-Oriented Design and Programming
C++ Basic Examples
Bounded Stack Example
Linked List Stack Example
UNIX File ADT Example
Speci cation for a String ADT
String Class ADT Example
Circular Queue Class Example
Matrix Class Example,
Bounded Stack Example
The following program implements a bounded
stack abstraction
{ This is the solution to the rst programming
assignment
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
Bounded Stack Example (cont'd)
/* The public section. */
public:
/* Initialize a new stack so that it is empty. */
Stack (void);
/* Copy constructor */
Stack (const Stack &s);
/* Assignment operator */
Stack &operator= (const Stack &s);
/* Perform actions when stack is destroyed. */
~Stack (void);
/* Place a new item on top of the stack
Does not check if the stack is full. */
void push (ELEMENT TYPE new item);
/* Remove and return the top stack item
Does not check if stack is empty. */
ELEMENT TYPE pop (void);
Does not check if stack is empty. */
ELEMENT TYPE top (void);
/* Returns 1 if the stack is empty,
otherwise returns 0. */
int is empty (void);
/* Returns 1 if stack full, else returns 0. */
int is full (void);
g;
String Class ADT Example
/* Check whether String S is a substring
in this String. Return ô€€€1 if it is not, oth-
erwise return the index position where the
substring begins. */
int String:: nd (const String &s) const f
char rstc = s[0]; // s.operator[] (0);
int end index = this->len ô€€€ s.len + 1;
for (int i = 0;
i < end index
&& ((*this)[i] != rstc jj
memcmp (&this->str[i] + 1, s.str + 1, s.len ô€€€ 1) !
i++)
;
return i < end index ? i : ô€€€1;
g
/* Extract a substring from this String of
size count starting at index pos. */
String String::substr (int index pos, int count) const f
if (index pos < 0 jj index pos + count > this->len)
return "";
else f
String tmp (count);
for (int i = 0; i < count; i++)
tmp[i] = (*this)[index pos++];
/* tmp.operator[] (i) =
this->operator[] (index pos++); */
return tmp;
g
g
No comments