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 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”.
Expert Solution & Answer
Want to see the full answer?
Check out a sample textbook solutionStudents have asked these similar questions
Write a simple function template for predicate function isEqualTo that compares its two arguments of the same type with the equality operator (==) and returns true if they are equal and false otherwise. Use this function template in a program that calls isEqualTo only with a variety of fundamental types. Now write a separate version of the program that calls isEqualTo with a user-defined class type, but does not overload the equality operator. What happens when
you attempt to run this program? Now overload the equality operator (with the operator function) operator==. Now what happens when you attempt to run this program?
In C++, define a “Conflict” function that accepts an array of Course object pointers. It will return two numbers:- the count of courses with conflicting days. If there is more than one course scheduled in the same day, it is considered a conflict. It will return if there is no conflict.- which day of the week has the most conflict. It will return 0 if there is none.
Show how this function is being called and returning proper values.
you may want to define a local integer array containing the count for each day of the week with the initial value of 0.Whenever you have a Course object with a specific day, you can increment that count for that corresponding index in the schedule array.
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.
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
- 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…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_forwardimplement pass-by-value parameter passing method.USE C LANGUAGEarrow_forward
- 4. Which function calls would provide the most helpful test of this function? Remember: With tests, you are attempting to figure out all the possible ways the function could be broken. function findMin(num1, num2){ if(num1 < num2){ return num1; } else { return num2; } } function findMin (num1 num2 if( numl < num2 ) return numl } else ( return num2 O A. findMin(-1, 0) findMin(2,4) findMin(5,10) Ов. findMin(5,3) findMin(7,2) findMin(5,1) O C. findMin(1,1) findMin(-2,2) findMin(0,3) O D. findMin(-1,1) findMin(1,-1) findMin(1,1)arrow_forwardUsing 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_forwardMoving Between Rooms - Navigation In this assignment, you will be working with a given "rooms" dictionary and associated constants to create a simple text-based game. Your main task is to develop a function that allows the player to navigate through the rooms based on the given specifications. You need to implement the function found in the starter code to the right The function should take into account the following conditions: If the direction leads to an exit, set the next room to the exit and the message to "Goodbye". If the direction is invalid, set the next room to the current room and the message to "No such direction". If the direction is valid, but you cannot go that way, set the next room to the current room and the message to "You bumped into a wall". If the direction is valid and you can go that way, set the next room to the room in that direction and the message to "Empty". To help you understand how the function will be integrated into the gameplay loop, the following…arrow_forward
- using c++ Develop a class called Point with two data members: x and y. Here you are dealing with a point in planar (2D) Cartesian coordinates with two integer values (x and y). Here are what you need to do: • Define a print function that returns the coordinates of a Point object. • Define a function to find the distance between two points. • Define a function to move a point to the specified location (x’, y’). • Define a function to translate a point by (dx, dy): the new location will be (x+dx, y+dy). • Define functions to tell the user if a point is on the left side, right side, above, or below another point (it can be the combination of some of these or exactly on the same spot). You also need to implement proper constructors (default and parameter constructors), a copy constructor, a destructor, getters, and setters for the data members. Implement the program with separate files: an interface file, an implementation file, and an application file (driver program). Your driver program…arrow_forwardFor all WWPD questions, type Function if you believe the answer is , Error if it errors, and Nothing if nothing is displayed. As a reminder, the following two lines of code will not display anything in the Python interpreter when executed: >>> x = None >>> X >>> lambda x: x #A lambda expression with one parameter x >>> a = lambda x: x # Assigning the lambda function to the name a >>> a (5) >>> (lambda: 3) () # Using a lambda expression as an operator in a call exp. >>> b = lambda x: lambda: x # Lambdas can return other lambdas! >>> c= b (88) >>> C >>> c() >>> d= Lambda f: f(4) # They can have functions as arguments as well. >>> def square(x): return x * X >>> d (square) >>> Z = 3 >>> e = Lambda x: lambda y: lambda: x >>>e (0) (1) () >>> f = Lambda z: x + Z >>> f (3) >>> higher_order_lambda = Lambda f: Lambda x: f(x) >>> g = Lambda x: x* x >>> higher_order_lambda (2) (g) # Which argument belongs to which function call? >>> higher_order_lambda (g) (2) + Z >>> call_thrice = lambda f:…arrow_forwardWrite a function, removeAll, that takes three parameters: an array of integers, the number of elements in the array, and an integer (say, removeItem). The function should find and delete all of the occurrences of removeItem in the array. If the value does not exist or the array is empty, output an appropriate message. (Note that after deleting the element, the number of elements in the array is reduced.) Assume that the array is unsorted.arrow_forward
- A Fast Food Shop has menu of items being sold on point. Each item has serial number, category, and price. You are supposed to do the following with this same. So 16. Define a appropriate structure to manage this information. SO 17. Define functions to input & output values in one object/variable of this struct type SO 18. Define an array of struct variables and input/output with the help of your won-defined functions in main. SO 19. Define pointer variable of struct type, assign the address of any single element of defined array then print the information of that object with this pointer. 5O 20. Define function out of the struct with following prototype to sum the prices all items enlisted in menu. double sumPrices(/*struct type array with size*/)arrow_forwardGiven the Class Definition for ClockType discussed extensively in class, write what would have to be added to the FUNCTION DEFINITION for the Class ClockType to overload the “= =”, i.e., the comparison “equal-equal sign,” here: NOTE: In other words, WHAT WOULD BE ADDED TO THE CLASS DEFINITION ONLY? Note: ThePrivate Members are: int hr; // contains the hours int min; // contains the minutes int sec; // contains the secondsarrow_forwardSo form code should be established in a functional way and outcome also.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