We consider a variety of ways to implement a basic ADT list structure as a class. The list consists of objects of type CELL hooked together via pointers. So we can do this:
Here are details: First, a skeleton outline of the class headers that is heavily annotated:
We start with the parent class or core class. As usual, we set the type of the object with the use of a defined TYPE macro. Note
#define TYPE char
class CHAR_CELL_CORE{
friend class CHAR_LIST_CORE;
friend ostream& operator<<(ostream&,const CHAR_CELL_CORE&);
friend ostream& operator<<(ostream&,const CHAR_CELL_CORE*);
public:
void load_element(TYPE x){element = x;};
protected:
virtual ostream& print(ostream& out) const = 0;
TYPE element;
};
The derivation chain shown here is
and it is by no means the best way to do this: We immediately notice consequences of this design:
class CHAR_SL_CELL : public CHAR_CELL_CORE{
friend class CHAR_SLLIST;
public:
CHAR_SL_CELL(char *name_in);
~CHAR_SL_CELL();
...agents...here
protected:
char * name;
CHAR_SL_CELL *next;
ostream& print(ostream& out) const;
};
class CHAR_DL_CELL : public CHAR_SL_CELL{
friend class CHAR_DLList;
public:
CHAR_DL_CELL(char *name_in);
~CHAR_DL_CELL();
...agents here...
protected:
CHAR_DL_CELL *previous;
ostream& print(ostream& out) const;
};
The LIST class is then built using CELL objects:
class CHAR_LIST_CORE{
friend class CHAR_CELL_CORE;
friend istream& operator>>(istream&,CHAR_LIST_CORE&);
friend istream& operator>>(istream&,CHAR_LIST_CORE*);
friend ostream& operator<<(ostream&,const CHAR_LIST_CORE&);
friend ostream& operator<<(ostream&,const CHAR_LIST_CORE*);
public:
...agents...
virtual ofstream& getOutputFile() = 0;
virtual ifstream& getInputFile() = 0;
protected:
virtual istream& grab(istream& in) = 0;
virtual ostream& print(ostream& out) const = 0;
};
class CHAR_SLLIST : public CHAR_LIST_CORE{
friend class CHAR_SL_CELL;
public:
CHAR_SLLIST(char *name_in);
CHAR_SLLIST(CHAR_SLLIST&);
~CHAR_SLLIST();
CHAR_SLLIST& operator=(const CHAR_SLLIST&);
...agents...
ofstream& getOutputFile() {return(outputFile);};
ifstream& getInputFile() {return(inputFile);};
protected:
char *name;
CHAR_SL_CELL *head;
...agents...
istream& grab(istream& in);
ostream& print(ostream& out) const;
ifstream inputFile;
ofstream outputFile;
};
#endif