Modules -- a functional interface
ADT
typedef int element;
struct list;
extern list* nil();
extern list* cons(element e, list* l);
extern element head(list* l);
extern list* tail(list* l);
extern bool equal(list* l, list* m);
slide: Modules -- a functional interface
For convenience, the list has been restricted to
contain integer elements only.
However, at the expense of additional notation,
we could also easily define a generic list
by employing template functions as provided
by C++.
This is left as an exercise for the reader.
The interface of the abstract class list
given in slide [obj-face] has been defined
generically by employing templates.
Objects -- a method interface
OOP
template< class E >
class list {
public:
list() { }
virtual ~list() { }
virtual bool empty() = 0;
virtual E head() = 0;
virtual list<E>* tail() = 0;
virtual bool operator==(list<E>* m) = 0;
};
slide: Objects -- a method interface
Note that the equal function in the ADT
interface takes two arguments,
whereas the operator== function in the OOP
interface takes only one,
since the other is implicitly provided by the
object itself.