Monday 28 December 2020

Function Templates (Object Oriented Programming Using C++).

 

Function Templates 


Objectives:  

To familiarize students with function templates and its usage      

Tools:

Turbo C++ IDE 

Procedure:

  Writing C++ language program to perform the following tasks
       Template used for an absolute value function
       Template used for function that finds number in an array

Templates: 

 can be used for both classes and functions and a feature, blueprint or formula of C++ programming language that allows functions and classes to operate with generic types. Generic programming is a style of computer programming in which algorithms are written in terms of types tobe-specified-later that are then instantiated when needed for specific types provided as parameters. 
A function template behaves like a function except that the template can have arguments of many different types. In other words, a function template represents a family of functions. Suppose you want to write a function that returns the absolute value of two numbers. As absolute value of a number is its value without regard to its sign: The absolute value of 3 is 3, and the absolute value of –3 is also 3. Ordinarily this function would be written for a particular data type such as:

int abs(int n)          //absolute value of ints
   {
        return (n<0) ? -n : n;     //if n is negative, return -n
    }


In above the function takes one argument and return value of same type but we will have to write a separate function in order to return absolute value of type long such as:

long abs(long n)               //absolute value of longs
    {
         return (n<0) ? -n : n;
     }


The body of the function is written the same way in each case, but they are completely different functions because they return values of different types. So to in order to save our time and to avoid code to rewrite we use function template. The general syntax of function template is as follow:

template <class type> 
     {
                  // Body of function
     }

The template keyword informs compiler that we are using a template function. The keyword class, within the angle brackets, is a type. The “type” is a placeholder name for a data type used by the function. This name can be used within the function definition and is called as template argument.



Step 1: Create New C++ Program File and save it as lab13.cpp Step 2:Write the following code as shown in figures below

function template program

Figure 35(a): Absolute value template function

function template program

Figure 35(b): Absolute value template function

Step 3: Save file again, compile and run it for required output as shown in figure below
function template program output

Figure 35(c): Absolute value template function
Step 4: Override above code with the following as shown in figures below
function template program

Figure 36(a): Number in an array template function

function template program

Figure 36(b): Number in an array template function
Step 5: Save file again, compile and run it for required output as shown in figure below
output

Figure 36(c): Number in an array template function

Try these Task(s): Write C++ language program to perform following tasks

       Define a function template for finding the minimum value contained in an array. Write a main() function to find the minimum value of integer array and minimum value of floating point numbers in an array. 

       Write a template function that returns the average of all the element of an array. The argument to the function should be the array name and the size of the array (type int). In main(), exercise the function with arrays of type int, long, double and char.

       Create a function called swaps() that interchanges the values of the two arguments sent to it. Make the function into a template, so it can be used with all numerical data types (char, int, float, and so on). Write a main() program to exercise the function with several types.


No comments:

Post a Comment