Starting from:

$34.99

COP3331 Lab 10 Solution

Submission Instructions:
1. Create a folder named Lab10 LastName FirstInitial (e.g. Lab10 Neal T).
2. In your folder, place a PDF file containing your answers to questions with a ♦.
4. Ensure that all programs have block comments at the very beginning (starting at the first line) in the file containing main() with your name and the program’s description. The block comment’s format should be identical to what’s provided in Figure 2-1.
5. Use single-line comments to describe your code’s functionality as needed.
6. Do not submit anything for questions with a ♣.
7. Zip the folder and submit it via Canvas.
♦ = 5 points each, ♠ = 15 points each

1. ♣ Read Chapter 18: How to work with templates.
2. ♦ A function template allows a function
a. to accept a variable number of parameters
b. to accept parameters with different data types
c. to return different data types
d. to be generated at runtime
3. ♦ A class template can define one or more type parameters that can then be used a. anywhere in the class
b. only in the private code for the class
c. only in the public code for the class
d. only in the member functions of the class
4. ♦ Why is the code for a class template typically stored in its own header file?
a. Because it’s required by some compilers
b. Because it makes it easier to include function templates in the class
c. Because class templates are often designed to be used by multiple programs
d. Because it makes the class template more flexible
1
5. ♠ Lab10 Q12: Multiset (Bag)
Code a custom container for a multiset (also known as a bag) that stores data of any type in no particular order and allows duplicates. Then, write some code that tests this custom container. Save your file(s) in folder lab10 q5. Console
Multiset/Bag Test
ELEMENTS
John
Joel
Samantha John
isEmpty() returns false isFull() returns false contains() for John returns true contains() for Bob returns false
Calling remove()
ELEMENTS
John
Joel
Samantha
Specifications
• Create a class template named ArrayBag that uses a private built-in array to store the underlying data for this container.
• Provide a no-argument constructor to create the array on the heap, set its capacity to 1000, and set its size to 0.
• Provide a destructor that returns the memory used by the array to the heap.
• Implement the following member functions:
– getSize() – return the current number of items in the bag.
– isEmpty() – return a value that indicates whether the bag is empty.
– isFull() – return a value that indicates whether the bag is full.
– add() – add the specified item to the bag.
– remove() – remove one item from the bag.
– contains() – return a value that indicates whether the bag contains the specified item.
– makeEmpty() – clear the bag of all items.
– printElements() – print all items in the bag to standard output.
• If the user tries to add to a full bag, or remove from an empty bag, return false and don’t perform the operation. Otherwise, perform the operation and return true.
TIP: You can use the boolalpha stream manipulator to convert the 0 and 1 values returned by the isEmpty(), isFull(), and contains() functions to “true” and “false”. For more information about this stream manipulator, you can search the internet.
2

More products