Question: 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

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!