Introduction to OOP: Chapter 9: Mechanisms for Software Reuse [next] [previous] [audio] [real] [text]

Using Composition

Everything must be redefined, but implementation can make use of the list data structure.

class Set {
public:
	void add(int);
	int  includes(int);
	void remove(int);
	int firstElement();
private:
	List data;
};

void Set::add (int x)
{
	if (! data.includes(x))
		data.add(x);
}

int includes (int x)
{
	return data.includes(x);
}

void remove (int x)
{
	data.remove(x);
}

int firstElement ()
{
	return data.firstElement();
}
Intro OOP, Chapter 9, Slide 9