Homework 7, Templates.
Short Description:
Create a templated class that selects items at random from a list of items.
This assignment is worth 5 points.
Goals
When you finish this homework, you should have:
- Created a templated class.
- Created a templated function.
Formal Description
- Create a templated class RandomPickT.
This class should have the following methods:
- A constructor that takes an initializer list of the template type and stores it in a vector.
- AddChoice, takes an argument of the template type and appends it to the list
- Choices: returns the number of choices available.
- Pick: returns a random element of the list.
- Create a templated function called DemoPick that takes a RandomPicker and performs 10 random picks using the picker. It should print out the picks one per line.
Your code should work with the following program:
#include <iostream>
#include "RandPickT.h"
using namespace std;
int main() {
RandomPickT names{"Bob","Alice", "Sue", "Tim"};
RandomPickT letters{'a','b','c','d'};
RandomPickT numbers{1, 2, 3, 4, 5};
DemoPick(names);
cout << endl;
DemoPick(letters);
cout << endl;
DemoPick(numbers);
cout << endl;
return 0;
}
My output was
Tim
Sue
Alice
Tim
Alice
Tim
Sue
Bob
Alice
Alice
c
d
c
d
d
c
a
c
a
a
2
4
3
5
3
1
3
4
3
1
Required Files
A single .h file containing your template.
Submission
Email your file as an attachment to dbennett@edinboro.edu. Make sure that the title indicates that this is homework 7 and that your name and section are included in the body of the message.