// This is the test program and question description for SYSC 2002 lab test #4 v1 // 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 five copies of 5 (i.e. all five values are 5); // b3 starts out with four copies of 4 (i.e. all four values are 4) // 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(5), b3(4), 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 maxTwice returns an IntBag containing at most two copies of every // different element in the bag that it's applied to. // If the bag contains one copy, the new bag will also have one copy. // If the bag has two or more copies, the new bag will have two copies. // Thus, b4 will contain two 2s, two 4s and one 3, // and b5 will contain two 5s. // 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.maxTwice(); b5 = b2.maxTwice(); // Let's check that... cout << "b4 contains: "; outputBag(b4); cout << "b5 contains: "; outputBag(b5); cout << endl; // "<" (i.e. less than) returns true if the first bag has fewer elements // than the second (regardless of whether any of the values are the same or not). // This method is worth 2 marks. if (b1