Problem Solving with C++ (9th Edition)
9th Edition
ISBN: 9780133591743
Author: Walter Savitch
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
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…
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…
Computer Science
Write the definition of the class template ArrayListType, and the derived class unordered ArrayListType. Provide implementations for the non abstract functions in the base template class, and implementations of the template functions in the derived template class. Write a test program to test the functions in the derived template class. Instantiate objects of type derived class in your test program using template parameters of type float, int.
Chapter 17 Solutions
Problem Solving with C++ (9th 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
- 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 each class…arrow_forwardSuppose that an array is passed as a parameter. How does this differ from the usual use of a value parameter? When an array is passed as a parameter, it is like passed by reference. A new array will be created in the called function and any changes to the new array will pass back to the original array. When an array is passed as a parameter, it is passed by value. So any changes to the parameter do not affect the actual argument. When an array is passed as a parameter, changes to the array affect the actual argument. This is because the parameter is treated as a pointer that points to the first component of the array. This is different from a value parameter (where changes to the parameter do not affect the actual argument). When an array is passed as a value, changes to the array affect the actual argument. This is because the parameter is treated as a pointer that points to the first component of the array. This is different from a parameter (where changes to the parameter do not…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
- Write 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_forwardIn java there must be at least two calls to the function with different arguments and the output must clearly show the task being performed. (ONLY ARRAYS or ARRAYLIST) Develop a function that accepts an array and returns true if the array contains any duplicate values or false if none of the values are repeated. Develop a function that returns true if the elements are in decreasing order and false otherwise. A “peak” is a value in an array that is preceded and followed by a strictly lower value. For example, in the array {2, 12, 9, 8, 5, 7, 3, 9} the values 12 and 7 are peaks. Develop a function that returns the number of peaks in an array of integers. Note that the first element does not have a preceding element and the last element is not followed by anything, so neither the first nor last elements can be peaks. Develop a function that finds the starting index of the longest subsequence of values that is strictly increasing. For example, given the array {12, 3, 7, 5, 9, 8,…arrow_forwardGiven the Class Definition for ClockType discussed extensively in class, write what would have to be added to the IMPLEMENTATION FILE for the Class ClockType to overload the “= =”, i.e., the comparison “equal-equal sign,” here: That is, write the FULL FUNCTION DEFINITION for THE FUNCTION associated with Class ClockType to overload the “= =” remembering the private members are: b) int hr; // that contains the hours int min; // that contains the minutes int sec; // that contains the secondsarrow_forward
- Revise the following Course class implementation in the following c++ code When adding a new student to the course, if the array capacity is exceeded, increase the array size by creating a new larger array and copying the contents of the current array to it. Implement the dropStudent function. Add a new function named clear() that removes all students from the course. Implement the destructor and copy constructor to perform a deep copy in the class. Write a test program that creates a course, adds three students, removes one, and displays the students in the course.arrow_forwardDescribe the parameters of the function addfirst.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 Within main (): Call your template functions and demonstrate how they work. Construct objects and show off your amazing programming skills!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