Академический Документы
Профессиональный Документы
Культура Документы
3 4
Object-Oriented Class Library
Architecture
Two general approaches are tree vs forest Object-Oriented Class Library
(dier in their use of inheritance): Architecture (cont'd)
Tree: create a single rooted tree of classes de-
rived from a common base class, e.g., object
{ e.g., standard Smalltalk libraries or NIHCL Object
Parameterized Types
Parameterized list class
Object-Oriented Class Library
template <class T>
Architecture (cont'd) class List f
public:
List (void): head (0) fg
void prepend (T &item) f
Node<T> *temp =
Hash new Node<T> (item, this->head );
Table this->head = temp;
Splay Tree g
/* */
private:
:::
Vector
template <class T>
Binary class Node f
Srch Tree Queue private:
T value ;
Node<T> *next ;
Stack Bag public:
Node (T &v, Node<T> *n)
: value (v), next (n) fg
g;
Node<T> *head ;
Forest-based class library g;
protected:
(1) Ugly ;-) <T>SLListNode* last ;
(2) Code bloat g;
(3) Not integrated with compiler
11 12
void Pointer Method void Pointer Example
General approach: One example application is a generic ADT
List container class. It contains four basic
{ void * pointers are the actual container ele- operations:
ments
1. Insertion
{ Subclasses are constructed by coercing void *
elements into pointers to elements of interest { add item to either front or back
2. Membership
Advantages: { determine if an item is in the list
1. Code sharing, less code redundancy
3. Removal
2. Builds on existing C++ features (e.g., inheri-
tance) { remove an item from the list
4. Iteration
Disadvantages: { allow examination of each item in the list
1. Somewhat awkward to design correctly (without revealing implementation details)
2. Inecient in terms of time and space (requires
dynamic allocation) The generic list stores pointers to elements,
along with pointers to links
3. Reclamation of released container storage is
dicult (need some form of garbage collec- { This allows it to hold arbitrary objects (but
tion) watch out for type-safety!!)
13 14
21 22
Card List.h
#include "Card.h"
class Card List : public List f
public:
void add (Card *a card) f
List::add to end (a card); Card List.C
g
// Virtual method
Card *current (void) f bool Card List::match (void *x, void *y) f
return (Card *) List::current (); Card &xr = *(Card *) x;
g Card &yr = *(Card *) y;
// Calls Card::operator ==
int includes (Card *a card) f return xp == yp;
return List::includes (a card); g
g
void Card List::print (ostream &str) f
void remove (Card *a card) f for (this->reset (); this->current (); this->next ())
List::remove (a card); this->current ()->print (str);
g g
Main problem:
{ Must dynamically allocate objects to store into
generic list!
Handling memory deallocation is dicult with-
out garbage collection or other tricks :::
25