Problem Solving with C++ (10th Edition)
10th Edition
ISBN: 9780134448282
Author: Walter Savitch, Kenrick Mock
Publisher: PEARSON
expand_more
expand_more
format_list_bulleted
Question
Chapter 17.1, Problem 6STE
Program Plan Intro
Function template:
In C++, a function template is referred as a “generic” function, which can work with different data types.
- While writing a function template, a programmer should use a “type parameter” to denote a “generic” data type instead of using the actual parameter.
- The compiler generates the code, when it encounters a function call to a function template. This code will handle the particular data type which is used in the function call.
- The compiler identifies the argument type and generates the code to work with those types.
- The generated code is referred as “template function”.
Example:
For example consider the following function template for finding a cube of value:
template <class T>
T cube(T x)
{
return x * x * x ;
}
- A function template must begin with the keyword “template” and it is followed by a pair of angle brackets, which contains one or more “generic” data types.
- A “generic” type must start with the keyword “class”, followed by an argument name which stands for the data type.
- The statement “T cube(T x)” is referred as function header, where “T” is a “type parameter”, “cube” is the function name and the variable “x” is declared for the type “T”.
Expert Solution & Answer
Want to see the full answer?
Check out a sample textbook solutionStudents have asked these similar questions
Variadic templates can take any variable(zero or more) number of arguments. In C++,
templates can have a fixed number of parameters only that have to be specified at the
time of declaration. variadic templates help to overcome this issue.
Write a variadic template function printfq2 that takes a package of parameters; unpack
the parameter package; and print all parameters. Use either approach discussed in
classes.
READ THE QUESTION CAREFULLY AND DO AS IT SAYS IN QUESTION . MATCH OUTPUT AND INPUT AS IT IS.
------------------------------------------------
Template Specialization in c++ is used to write generic code. Template code is written once and use for any data type including user-defined data types.Example: sort() can be written and used to sort any data type items. Create a Template code that reads input from the user, user can enter any type of data like integer, double, float.sum() is the template method that can take any two types of data and add it and return the sum of the two numbers.Create a Template Specialization method to add two integer values only.If a specialized version is present, the compiler first checks with the specialized version and then the main template.
Sample Input and Output:Enter two integer Numbers: 55Enter two float Numbers: 3.53.1Enter two double Numbers: 3.68.6Sum of two integer Numbers: only integer10Sum of two float Numbers: 6.6Sum of two double…
use case template assignment
Chapter 17 Solutions
Problem Solving with C++ (10th Edition)
Ch. 17.1 - Write a function template named maximum. The...Ch. 17.1 - Prob. 2STECh. 17.1 - Define or characterize the template facility for...Ch. 17.1 - Prob. 4STECh. 17.1 - Display 7.10 shows a function called search, which...Ch. 17.1 - Prob. 6STECh. 17.2 - Give the definition for the member function...Ch. 17.2 - Give the definition for the constructor with zero...Ch. 17.2 - Give the definition of a template class called...Ch. 17.2 - Is the following true or false? Friends are used...
Ch. 17 - Write a function template for a function that has...Ch. 17 - Prob. 2PCh. 17 - Prob. 3PCh. 17 - Redo Programming Project 3 in Chapter 7, but this...Ch. 17 - Display 17.3 gives a template function for sorting...Ch. 17 - (This project requires that you know what a stack...Ch. 17 - Prob. 6PPCh. 17 - Prob. 7PPCh. 17 - This project requires that you complete...
Knowledge Booster
Similar questions
- What exactly is the connection between function templates and overloading?arrow_forward3. Inout-mode parameters can be implemented using either pass-by-value-result or pass-by-reference - show examples of both types of parameter passing. Show how collisions between actual parameters can cause aliasing.arrow_forwardUsing C++ Using your own creativity, make a set of function templates that have these features: This function must return a value. A function template with 1 template parameter, T. And, any other parameters you want. and then another function template but this time with 2 template parameters, T1 and T2. And, any other parameters you want.arrow_forward
- In C++, can I get a code example for a function that will return the intersection items for two sets. It will return/print out the shared (intersection) items for them. I am looking for an actual function preferably for a set class, comparing one instance of a set class with another, but definitely NOT a STL keyword. Thank you.arrow_forwardTemplate functions are considered to be an efficient alternative of function overloading. Justify this statement by comparing the pros and cons of both with real-world examples.arrow_forwardUsing C++ Using your own creativity, make a set of function templates that have these features: This function must return a value. A function template with 1 template parameter, T. And, any other parameters you want. A function template with 2 template parameters, T1 and T2. And, any other parameters you want. Using your own creativity, make a set of class templates that have these features: For this class template, put everything in one place--do not declare the member functions and have separate definition of the member functions elsewhere. Keep them in one place. Include a private variable. Include a constructor that loads the private variable when constructed. Include a destructor that clears the private variable to zero. Include set and get functions to set and get the private variable. For this class template, use declarations for variables and functions, like you do in header file (which you may use if you want). Then, separately put the full function definitions for…arrow_forward
- Discuss why you must learn both techniques if you can achieve the same result using a value-returning function vs. a void function and passing parameters by reference. Discuss security implications of using pass-by-reference.arrow_forwardWrite a C++ program that: • Implements a class with three data members {sum (int), count (int), avg (float)}.( Implement the following 3 member functions: • The first function is a default constructor which initializes sum and count to zero. (. • The second function is named add which receives an integer number as input parameter and add it to sum and increment count by 1. • The third function is named outmut which drvides the sum over count and save it to avg then print the avg value). • Then, write a program to read numbers from the user until entering zero. The program uses the previous class to calculate and print average of {positive, negative} numbers. (arrow_forwardCan you help with with the following questions, regarding population genetics and the Wright Fisher model in python? 1. Go through the given WFtrajectory function and give a short explanation of what each line of code does. Pay attention to the variables in the function and explain what they contain. 2. Make a new version of the WFtrajectory function which does exactly the same thing except that instead of appending to a list it initialises a numpy vector with zeroes and fills the vector with a for loop instead of a while loop. Make a plot of the output from a few example trajectories.arrow_forward
- I need help getting started with a writing assignment on ADTs, abstract data types. Suggested requirements are listed below : Your manual includes one section for each ADT. Each ADT section should include subsections as follows- an introduction that describes the ADT in plain English- a formal definition of the ADT (The operations, their parameters, and their results)- applications of the ADT, with discussion of which implementation is most desirable for each. One application not discussed in class (or in the book) should be provided for each ADT, with sources cited appropriately.- implementation descriptions of at least two distinct implementations (for instance, linked list and array). If more than two implementations routinely exist, inclusion of more is desirable.- a comparison of the implementations' strong and weak points (from an ADT user's perspective, i.e., performance tradeoffs). The introduction should include definitions of data type, abstract data type, and data…arrow_forwardWhat is the difference between template function and template classes elaborate with the help of an example?arrow_forwardThe following questions will provide you with a lambda expression and ask you to perform a single beta reduction using both the eager and lazy evaluation. They are intended to help you think through exactly what the order of operations for Lambda Calc is. For a reminder: 1) Lambda Calc is left-associative. This means that if you see something like A B C, read this as (A B) C, and evaluate the application A B before applying c to its result. It also means that if you see something like A B, you should fully reduce д before trying to complete the application, regardless of your reduction strategy. 2) Eager vs. Lazy: Eager says if you see something like A B, (first, you should fully reduce A before trying to complete the application, because of left-associativity), then you should fully reduce B, then perform the application. Lazy says if you see something like AB, (first, you should fully reduce д before trying to complete A the application, because of left-associativity), then perform…arrow_forward
arrow_back_ios
SEE MORE QUESTIONS
arrow_forward_ios
Recommended textbooks for you
- C++ Programming: From Problem Analysis to Program...Computer ScienceISBN:9781337102087Author:D. S. MalikPublisher:Cengage Learning
C++ Programming: From Problem Analysis to Program...
Computer Science
ISBN:9781337102087
Author:D. S. Malik
Publisher:Cengage Learning