case study: con - web.cs.dal.castephane/3132/chap15.pdf · case study: con tainer classes in c++ 3...
TRANSCRIPT
Case Study: Container Classes in C++ 1
Introduction to
Object Oriented Programming 2E
Timothy A. Budd
Chapter 15
Case Study: Container Classes
Introduction To Object Oriented Programming 2E c Timothy A. Budd 1996 Chapter 15
Case Study: Container Classes in C++ 2
Strong Typing versus Containers
Strong typing is useful in programming languages because it helps in the rapid discovery
of programming errors.
But strong typing would seem to get in the way of developing useful, reusable collection
(or container) classes.
� To be reusable, container classes must be independent of any application.
� Values should not lose their type when placed into a container.
� The programmer creating the reusable container classes will have no idea what type
of object it will hold.
Introduction To Object Oriented Programming 2E c Timothy A. Budd 1996 Chapter 15
Case Study: Container Classes in C++ 3
Conventional Solution
Conventional containers tie too closely the container and the element type.
typeLink = Record
value : integer;
nextELement : ^ Link;
end;
Introduction To Object Oriented Programming 2E c Timothy A. Budd 1996 Chapter 15
Case Study: Container Classes in C++ 4
Containers in Dynamic Languages
Containers in dynamic languages are relatively easy, since values just hold their own
type (or can be cast into the appropriate type, as in Java).
Containers hold values of type Object.
Containers in Strongly Types Languages are much more di�cult.
Introduction To Object Oriented Programming 2E c Timothy A. Budd 1996 Chapter 15
Case Study: Container Classes in C++ 5
Three Solutions
We will examine three di�erent solutions to this problem:
� Containers maintain elements derived from a common parent class.
� Containers maintain pointers, which can be cast to appropriate type.
� Templates
Introduction To Object Oriented Programming 2E c Timothy A. Budd 1996 Chapter 15
Case Study: Container Classes in C++ 6
Common Base Link Type
In one approach, the programmer creating the linked list abstraction creates a generic
\link" class, independent of any application.
The programmer who uses the list abstraction then subclasses from this generic class, in
order to hold values speci�c to a particular problem.
Introduction To Object Oriented Programming 2E c Timothy A. Budd 1996 Chapter 15
Case Study: Container Classes in C++ 7
The Generic List Class
class List f
public:// constructor
List() : �rstElement(0) f g
// operations
void Add(Link � n)
f n!next = �rstElement; �rstElement = n; g
int isEmpty()
f return �rstElement == 0; g
Link � First();
f return �rstElement; g
void RemoveFirst();
f Link � p = �rstElement;
�rstElement = �rstElement! next;
delete p; g
int Includes(Link � n)
f for (Link � p = �rstElement; p; p = p!next)
if (�p == �n) return 1;
return 0; g
Introduction To Object Oriented Programming 2E c Timothy A. Budd 1996 Chapter 15
Case Study: Container Classes in C++ 8
private:// data �eld
Link � �rstElement;
g;class Link f
public:// constructors
Link () : next(0) f g
Link (Link � n) : next(n) f g
// operations
virtual int operator == (Link �) = 0;
// data �eld
Link � next;
g;Introduction To Object Oriented Programming 2E c Timothy A. Budd 1996 Chapter 15
Case Study: Container Classes in C++ 9
A Link for Double Precision Numbers
class DoubleLink : public Link f
public:// data area
double theValue;
// constructor
DoubleLink (double v) : theValue(v) fg
// operations
int operator == (Link � d)
f return thisValue == ((DoubleLink �) d)!theValue; g
g;Introduction To Object Oriented Programming 2E c Timothy A. Budd 1996 Chapter 15
Case Study: Container Classes in C++ 10
Using The List Class
Using the list class involves explicitly creating links.
List aList;
aList.Add(new DoubleLink(2.71828));
aList.Add(new DoubleLink(1.61803));
aList.Add(new DoubleLink(3.14159));
DoubleLink � pi;
pi = (DoubleLink �) aList.First();
aList.RemoveFirst();
Introduction To Object Oriented Programming 2E c Timothy A. Budd 1996 Chapter 15
Case Study: Container Classes in C++ 11
Hiding the Links
We can avoid the explicit manipulation of links by subclassing the List class, as well as
the Link class.
class DoubleList : public List f
public:void Add(double v)
f List::add(new DoubleLink(v)); g
double First()
f return �rstElement!value; g
int Includes(double v)
f DoubleLink v1(v);
return List::includes(v1); g
g;Introduction To Object Oriented Programming 2E c Timothy A. Budd 1996 Chapter 15
Case Study: Container Classes in C++ 12
Containers that Hold Only Pointers
In many applications, containers will be used only to hold pointers, not values
themselves.
This is often due in part to the fact that only pointers or references can be true
polymorphic variables in C++.
In these situations we can create a generic link class that maintains a void pointer, and
get by with only subclassing the list class.
Introduction To Object Oriented Programming 2E c Timothy A. Budd 1996 Chapter 15
Case Study: Container Classes in C++ 13
List and Link
class List f
public:... // same as before
void Add(void � v)
f �rstElement = new Link(v, �rstElement); g
void � First()
f return �rstElement!value; g
... // same as before
g;class Link f
// constructors
Link (void � v) : next(0), value(v) fg
Link (void � v, Link � n) : next(n), value(v) fg
// data areas
Link � next;
void � value;
g;Introduction To Object Oriented Programming 2E c Timothy A. Budd 1996 Chapter 15
Case Study: Container Classes in C++ 14
Example, a list of Windows
class WindowList : public List f
public:void Add(Window � w)
f List::add(w); g
Window � First()
f return (Window �) List::�rst(); g
g;Introduction To Object Oriented Programming 2E c Timothy A. Budd 1996 Chapter 15
Case Study: Container Classes in C++ 15
Best Alternative, Templates
The third alternative is the best.
A template allows a class description to be parameterized, much as a function is
parameterized.
Parameter values are types, and can be used as type names within the class description.
Introduction To Object Oriented Programming 2E c Timothy A. Budd 1996 Chapter 15
Case Study: Container Classes in C++ 16
A Template List Class
template <class T>
class List f
// constructors
List();// operations
void Add(T v)
f �rstElement = new Link<T>(v, �rstElement); g
int isEmpty()
f return �rstElement == 0; g
T First()
f return �rstElement!value; g
void RemoveFirst()
f Link<T> � p = �rstElement;
�rstElement = p!next;
Introduction To Object Oriented Programming 2E c Timothy A. Budd 1996 Chapter 15
Case Study: Container Classes in C++ 17
delete p; g
int Includes(T v)
f for (Link<T> � p = �rstElement; p; p = p!next)
if (v == p!value)
return 1;
return 0; g
g;template <class T>
class Link f
// constructor
Link (T v, Link � n) : value(v), next(n) f g
// data �elds
Link � next;
T value;
g;Introduction To Object Oriented Programming 2E c Timothy A. Budd 1996 Chapter 15
Case Study: Container Classes in C++ 18
Using Template Classes
List<double> aList;
aList.Add(2.71828);
aList.Add(1.61803);
aList.Add(3.14159);
double pi;
pi = aList.First();
aList.removeFirst();
Introduction To Object Oriented Programming 2E c Timothy A. Budd 1996 Chapter 15
Case Study: Container Classes in C++ 19
Advantages of Templates
� Does not require any new subclasses to be created for use.
� General facility, now part of C++ standard de�nition.
Introduction To Object Oriented Programming 2E c Timothy A. Budd 1996 Chapter 15
Case Study: Container Classes in C++ 20
Iteration and Iterators
Want a mechanism that allows iteration over containers of values without exposing inner
working.
Can be performed using an iterator. In C++ can be made to look like a pointer.
list<int>::iterator start = aList.begin();
list<int>::iterator end = aList.end();
for ( ; start != end; ++start)
cout � (�start) � endl;
Introduction To Object Oriented Programming 2E c Timothy A. Budd 1996 Chapter 15
Case Study: Container Classes in C++ 21
Iterator Class
template <class T> class listIterator f
ListIterator (Link<T> & sl) : currentLink(sl) f g
void operator ++ () f currentLink = currentLink!nextLink; g
T operator � () f return currentLink!value; g
bool operator == (ListIterator<T> & right) f return currentLink ==
right.currentLink; g
private:
Link<T> � currentLink;
g;Introduction To Object Oriented Programming 2E c Timothy A. Budd 1996 Chapter 15