Introduction to OOP: Chapter 15 : Case Study: Container Classes
[next]
[previous]
[audio]
[real]
[text]
The Generic List Class
class List {
public:
// constructor
List() : firstElement(0) { }
// operations
void Add(Link * n)
{ n->next = firstElement; firstElement = n; }
int isEmpty()
{ return firstElement == 0; }
Link * First();
{ return firstElement; }
void RemoveFirst();
{ Link * p = firstElement;
firstElement = firstElement-> next;
delete p; }
int Includes(Link * n)
{ for (Link * p = firstElement; p; p = p->next)
if (*p == *n) return 1;
return 0; }
private:
// data field
Link * firstElement;
};
class Link {
public:
// constructors
Link () : next(0) { }
Link (Link * n) : next(n) { }
// operations
virtual int operator == (Link *) = 0;
// data field
Link * next;
};