Place all declarations and definitions needed for the tasks below in one file, i.e. main.cpp file. In your main() function, include code that shows testing
Place all declarations and definitions needed for the tasks below in one file, i.e. main.cpp file.
In your main() function, include code that shows testing the functions and classes you created as thoroughly as possible.
Part 1 (2 points)
Create a function template named randomPick() with three parameters of the same type and a return value of the same type as the parameters. When called, it should randomly pick one of the three parameters, and return that value.
Part 2 (2 points)
Create a function template named getChoice(), that has:
- Two string parameters
- Two other parameters, with the same type as each other.
- A return value with the same type as the two parameters.
When it runs, it presents the user with a prompt using the two strings. Using a loop, it does input validation to continue the prompt until the user picks one of the items. Once they have picked a valid item, it returns the corresponding value from the other two parameters.
For instance, if I were to call
getChoice("seven", "eleven", 7, 11)
the user should see something like:
Please pick seven or eleven:
If they (eventually) enter "seven", the function would return the integer value 7.
Part 3 (2 points)
Answer the following two questions at the top of the main.cpp file.
Question 1: Why normally the entire template class, including both the declaration and definition (i.e. implementation), are placed in the .h file? If we choose to let the definition of the class be in the .cpp file, what should be the way to include it?
Question 2: The getQuotient() function takes two integers or doubles as input and returns the quotient. Taking the getQuotient() as example, what may go wrong when using this template?
// answers to the questions at the top of the main.cpp /// Question 1: Why normally the entire template class, including both the /// declaration and implementation, are placed in the .h file? If we choose to let /// the definition of the class be in the .cpp file, what should be the way to /// include it? /// Question 2: Taking the getQuotient() function as example, what may go /// wrong when using this template? /// The following is the minimal set of testing code. int main() { cout << "Part 1" << endl; cout << randomPick('1','2','3') << endl; cout << randomPick (11,22,33) << endl; cout << randomPick ("Mac","PC","Others") << endl << endl; cout << "Part 2" << endl; cout << getChoice("x","y", 1, 2) << endl << endl; /// User enters z -> system fails to get a choice /// User enters y -> system returns 2 return 0; }
Step by Step Solution
There are 3 Steps involved in it
Step: 1

Get step-by-step solutions from verified subject matter experts
100% Satisfaction Guaranteed-or Get a Refund!
Step: 2Unlock detailed examples and clear explanations to master concepts

Step: 3Unlock to practice, ask and learn with real-world examples

See step-by-step solutions with expert insights and AI powered tools for academic success
-
Access 30 Million+ textbook solutions.
-
Ask unlimited questions from AI Tutors.
-
Order free textbooks.
-
100% Satisfaction Guaranteed-or Get a Refund!
Claim Your Hoodie Now!

Study Smart with AI Flashcards
Access a vast library of flashcards, create your own, and experience a game-changing transformation in how you learn and retain knowledge
Explore Flashcards