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

Info icon This 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; }
Image of page 1

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

View Full Document Right Arrow Icon
template <class T> CheckList<T> & CheckList<T>::operator+=(const T &j) {int loc=0; while(loc<count && *table[loc]!=j) 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> CheckList<T> & CheckList<T>::operator-=(const T &j) {int loc=0; while(loc<count && *table[loc]!=j) 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>
Image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern