// This is the test program and question description for SYSC 2002 sample lab test #4 // 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. #include "stdstuff.h" #include "IntBag.h" // function to output contents of a bag 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 containing five 1s (i.e. all five values are 1); // b3 starts out containing four -2s (i.e. all 4 values are -2) // The new constructor is worth 3 marks. IntBag b1, b2(5,1), b3(4,-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; // uncommenting the following line of code should make your program quit, as // -2 copies of the value 4 makes no sense // (see samplt4constructorQuit.exe) //IntBag b6(-2,4); // put some items in b1 b1.add(2); b1.add(4); b1.add(2); b1.add(3); b1.add(5); // Let's check that... cout << "b1 contains: "; outputBag(b1); cout << endl; // Method twoOfEach returns an IntBag containing 2 copies of every element // in the bag that it's applied to. // Thus, b4 will contain two 2s, 4s, 3s, and 5s, and b5 will contain two 1s. // Note that this method **must** use startWalk and continueWalk and be const to get full marks. // This method is worth 5 marks. b4 = b1.twoOfEach(); b5 = b2.twoOfEach(); // Let's check that... cout << "b4 contains: "; outputBag(b4); cout << "b5 contains: "; outputBag(b5); cout << endl; // If there's not enough space for all the elements, your program should quit // Uncommenting the following two lines gives an illustration of this, as // b7 should contain two 0s and 10s as well as two 2s, 4s, 3s, and 5s. // (see samplt4twoOfEachQuit.exe) //IntBag b7, b8; //b8=b1; //b8.add(0); b8.add(10); //b7 = b8.twoOfEach(); // == returns true if two bags have the same number of elements (regardless // of whether the values are the same or not). Perhaps not a good choice // for this method, but go with it! // This method is worth 2 marks. if (b1==b2) cout << "CORRECT: b1 and b2 have the same number of elements.\n"; else cout << "INCORRECT: b1 and b2 do not have the same number of elements.\n"; if (b3==b5) cout << "INCORRECT: b3 and b5 have the same number of elements.\n"; else cout << "CORRECT: b3 and b5 do not have the same number of elements.\n"; pause (); return 0; }