Problem Solving with C++ (9th Edition)
Problem Solving with C++ (9th Edition)
9th Edition
ISBN: 9780133591743
Author: Walter Savitch
Publisher: PEARSON
Question
Book Icon
Chapter 17.1, Problem 2STE
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”.

Blurred answer
Students have asked these similar questions
Using C++ 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 each class member function…
Write a function template named maximum. The function takes two values of the same type as its arguments and returns the larger of the two arguments (or either value if they are equal). Give both the function declara-tion and the function definition for the template. You will use the operator< in your definition. Therefore, this function template will apply only to types for which < is defined. Write a comment for the function declaration that explains this restriction.
This is in c++. Create a class of function objects called StartsWith that satisfies the following specification: when initialized with character c, an object of this class behaves as a unary predicate that determines if its string argument starts with c. For example, StartsWith(’a’) is a function object that can be used as a unary predicate to determine if a string starts with an a. So, StartsWith(’a’)("alice") would return true but StartsWith(’a’)("bob") would return false. The function objects should return false when called on an empty string. Test your function objects by using one of them together with the STL algorithm count_if to count the number of strings that start with some letter in some vector of strings. So basically, I want a class that returns true or false based on if the name that is typed starts with the character that I put down.
Knowledge Booster
Background pattern image
Similar questions
SEE MORE QUESTIONS
Recommended textbooks for you
Text book image
C++ Programming: From Problem Analysis to Program...
Computer Science
ISBN:9781337102087
Author:D. S. Malik
Publisher:Cengage Learning