c109sp02f - CSc 109 Final Examination Sunday 12 May 2002...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
CSc 109 Final Examination Sunday 12 May 2002 >>>>>>>>>>>>>>>>>>>>SUGGESTED ANSWERS<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< 1. (25 pts) I am interested in a class, CheckList, whose instances keep track of whether instances of some class have been added or removed from the CheckList. By default, the CheckList can hold up to 100 items, but an instances of CheckList of other sizes can be created. One can add an item to a CheckList with the += operator (which does nothing if the item has been previously added, one can remove an item with the -= operator (which does nothing if the item is not in the CheckList), and one can ask whether an item is in the CheckList with the inList() function. Below is some code using the class. Write a template for class CheckList. int main() { CheckList<int> a(200),b; CheckList<double> d; a+=3; a+=2000; a+=2000; if(a.inList(2000)) cout<<"2000 is in the list\n";//should print b+=5; a-=2000; if(!a.inList(2000)) cout<<"2000 is not in the list\n";//should print return 0; } ========================== #include <fstream.h> template <class T> class CheckList {public: CheckList(int j=100); ~CheckList(); CheckList & operator+=(const T &j); CheckList& operator-=(const T &j); bool inList(const T &j)const; private: T **table; int size,count; void err(bool b, char *mess); }; template <class T> CheckList<T>::CheckList(int j) {err(j<1,"Bad size"); size=j; count=0; table=new T*[size]; err(table==NULL,"Heap overflow"); for(int j=0;j<size;j++) table[j]=NULL; } template <class T> CheckList<T>::~CheckList() {for(int j=0;j<count;j++) delete table[j]; delete [] table; }
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
template <class T> {int loc=0; loc++; if(loc<count) return *this; err(count>=size,"CheckList overflow"); table[loc]=new T(j); err(table[loc]==NULL,"Heap overflow"); count++; return *this; } template <class T> {int loc=0; loc++; if(loc>=count) return *this; delete table[loc]; for(int j=loc;j<count-1;j++) table[j]=table[j+1]; table[count-1]=NULL; count--; return *this; } template <class T> {for(int j=0;j<count;j++) if(*table[j]==t) return true; return false; } template <class T> void CheckList<T>::err(bool b, char *mess) {if(b) {cerr<<"ERROR: "<<mess<<endl; exit(1); } } int main() { CheckList<int> a(200),b(0); a+=3; a+=2000; a+=2000; if(a.inList(2000)) cout<<"2000 is in the list\n";//should print
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 03/09/2008 for the course CSE 109 taught by Professor Kay during the Spring '08 term at Lehigh University .

Page1 / 7

c109sp02f - CSc 109 Final Examination Sunday 12 May 2002...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online