// This is the test program and question description for SYSC 2002 lab test #4 v2 // You are to update and submit files "IntBag.h" and "IntBag.cpp" by adding three new // methods. Your new methods may use existing methods in the IntBag class. // The following code and the sample executables provided should make it clear as to // what methods are required and what they do. // Note that the Chapter 9 version of the IntBag class (IntBag.h and IntBag.cpp) // is provided on the G: drive. The Chapter 8 version 3 of the Date class (Date.h // and Date.cpp) is also provided on the G: drive for reference. #include "stdstuff.h" #include "IntBag.h" // function to output contents of a bag // (Note that this function is provided for you. You do not have to // write it or change it.) void outputBag(IntBag &bag) { int value; bool stillWalking; // use startWalk and continueWalk to print contents of bag for (stillWalking=bag.startWalk(value); stillWalking; stillWalking=bag.continueWalk(value)) { cout << value << " "; } cout << endl; } int main () { // b1, b4, and b5 start out empty; // b2 starts out with four 5s and four 6s (i.e. four copies of the value // provided [5] and four of value+1 [6]) // b3 starts out with two -2s and two -1s (i.e. two copies of the value // provided [-2] and two of value+1 [-1]) // (i.e. six values are -2) // The new constructor is worth 3 marks. // Hint: Inside a constructor, the only methods you can apply to "*this" are // other constructors. Do not attempt to apply other methods (e.g. add, etc.) // to "*this" inside constructors. // Note that the test for the constructor quitting has been moved to the end // of the main program, so that only one sample exe is needed. IntBag b1, b2(4,5), b3(2,-2), b4, b5; // Let's check that... cout << "b1 contains: "; outputBag(b1); cout << "b2 contains: "; outputBag(b2); cout << "b3 contains: "; outputBag(b3); cout << "b4 contains: "; outputBag(b4); cout << "b5 contains: "; outputBag(b5); cout << endl; // put some items in b1 b1.add(2); b1.add(4); b1.add(2); b1.add(3); b1.add(2); b1.add(4); // Let's check that... cout << "b1 contains: "; outputBag(b1); cout << endl; // Method removeOneCopy returns an IntBag containing one less occurrence // of every different element in the bag that it's applied to. // In other words, if there is just one occurrence, it is removed, // and if there is more than one occurrence, just one occurrence is removed. // Thus, b4 will contain two 2s, and one 4, and b5 will contain three 5s and // three 6s. // This method is worth 5 marks. // Note that to get full marks, your method must be const, and it must *not* // directly access the data array (e.g. data[i], etc. is forbidden) of *any* // bags. If you choose to write a non-const method and/or to access data // directly, you will get part marks. b4 = b1.removeOneCopy(); b5 = b2.removeOneCopy(); // Let's check that... cout << "b4 contains: "; outputBag(b4); cout << "b5 contains: "; outputBag(b5); cout << endl; // ">" (i.e. greater than) returns true if the first bag has more elements // than the second (regardless of whether any of the values are the same or not). // This method is worth 2 marks. if (b1>b2) cout << "INCORRECT: b1 has more elements than b2.\n"; else cout << "CORRECT: b1 does not have more elements than b2.\n"; if (b5>b3) cout << "CORRECT: b5 has more elements than b3.\n\n"; else cout << "INCORRECT: b5 does not have more elements than b3.\n\n"; // Here is a test to see if the new constructor quits. // The following line of code should make your program quit, as // six 4s and six 5s won't fit in the bag. IntBag b6(6,4); pause (); return 0; }