Chris Pollett > Old Classes > PIC10B Lec 1&2 ( Print View ) Lecture Notes-PDF. Enrollment info. Course Info:
Practice Exams: PIC: |
PIC 10b Fall 2000 HW #2Due date: Oct. 18. Lecture 1's due time is: 10:30pm ========= Lecture 2's due time is: 5:30pm. Reading Assignment: Read Sedgewick Ch1 to Ch3 Sec 3. =================== Files to submit: pic10bhw2file1.h -contains Set interface ================= pic10bhw2file2.cpp -contains Set class implementation pic10bhw2file3.cpp -contains driver program i.e., main() HW2 Purpose: Write a dynamic storage class called Set. ============ Practice with separate compilation of files. Gain experience with operator overloading and dynamic memory allocation. Learn a little bit about analyzing amortized run time of an algorithm. Specification: ============== The Set class has three private variables: int *array; // stores user data int curSize; // storage space used int maxSize; // current total storage space. Below are the public member functions for and friends of Set: Set(); // constructor. array set to be a new array of ints of size 10 // curSize is set to 0; maxSize set to 10 Set(int s); //constructor. array set to be a new array of ints of size s // curSize is set to 0; maxSize is set to s. Set(const Set& s); //constructor. copies s into current object ~Set(); //destructor should free up memory in *array Set& operator=(Set& v); //should use this pointer to check if //v is current object. If it is //return current object. Otherwise, //set up current object as in v and //return current object. //member function not friend! friend Set operator+(const Set& s, int m); //returns the Set that results from //adding m to s. friend Set operator+(int m, const Set& s); //returns the Set that results from //adding m to s (does same as //previous function) //For the previous two functions if s's curSize < maxSize then //the Set returned by + has the same maxSize as s but curSize is incremented. //On the other hand, if curSize = maxSize then the Set returned //has curSize one larger, but maxSize twice as large as s. //When allocating memory for array in the new Set make it of size this //new maxSize. bool element(int i); //checks if the integer i has been stored in the Set. // returns true if it has; false otherwise. friend ostream& operator << (ostream& out, const Set& s); //prints each element to ostream& //separated by a return friend istream& operator >> (istream& in, Set& s); //reads one integer element //and stores it into set s using //+ operator. You should also write a driver program as below: The driver should first should create two sets: Set *s1, *s2; s1 = new Set(); s2 = new Set(1); It should then set *s1 = *s2 +1; *s1 = *s1; *s2 = 2 + *s1; It should check if 2 is an element of s1 and print out result using cout. It should check if 2 is an element of s2 and print out result using cout. It should read two more elements into s2 using cin >> *s2 >> *s2; Finally it should print out the elements of s2 using cout << *s2 and then delete s1 and s2 and return 0. Point Breakdown for HW2 ======================= If the submit program didn't collect your file because you misnamed it or you did not follow the required formatting instructions in the syllabus you will get a 0 without an opportunity for a regrade. If your driver program doesn't test something as described above, then you won't receive credit for it. How well your code is commented.........1pt. use #ifndef in header file..............1pt. class def in header has member and friend functions as specified above.1pt. constructors and destructors work correctly................1pt. Set + int and int + Set work correctly................1pt. = is overloaded and uses the "this" pointer correctly...............1pt. element(i) method works correctly.......1pt. << and >> overloaded and work correctly............................1pt. ============================================ Total...................................8pts Bonus...................................1pt.(after curving on final grade) Bonus: Implement the following two additional member function and friend ====== function and write additional driver routine to test them: Set& operator+=(int elt); // adds elt to current Set using same // algorithm for resizing array as stated above //returns reference of current set. //Only does copying when need to resize. friend bool operator==(const Set& s1, const Set& s2); // tests if two sets are the same. // They are the same if have same elements. // It doesn't matter if they have same number of // copies of a given element. i.e., {1,2} and {1,2,1} // are same. |