CodesJava

Easy learning with example program codes

CPP Templates

CPP templates acts as a support for generic programming technique, where parameters in algorithms are of generic types and thus can work for a variety of data types. This feature in CPP is used to define the generic classes and generic functions.

 

Types of CPP Templates:

  1. Function Templates:

In function templates, templates for a function is defined.

Syntax:

template < class Ttype> ret_type func_name(parameter_list) 

{

 // code to be executed 

}

  • Ttype:

This parameter is used to represent a placeholder name, to be replaced by the compiler, for a data type, within the function definition.

  • class:

This keyword is used before Ttype to specify a generic type in a template declaration.

Syntax: Function Templates with Multiple Parameters:

template <class T1, class T2,…..>

return_type function_name (arguments of type T1, T2….)

{

 // code to be executed 

}

Important points:

  • Function template is used by generic functions to define a set of operations that can be applied to the various types of data.
  • The generic functions can be overloaded by overloading the template functions (can differ in the parameter list).
  • For all the versions of a function, a generic function executes the same operation for the same data type.

Example:

#include <iostream.h>  
using namespace std;  
template<class A> A mul(A &x,A &y,A &z)  
{  
A product = x*y*z;  
return product;  
}  
int main()  
{  
float n1 = 50;  
float n2 = 40;  
float n3 = 30;
cout<<"Product of " << n1 << ", " << n2 << " and " << n3 <<" is : "<< mul(n1,n2,n3);  
return 0;  
}

Output

Product of 50, 40 and 30 is : 60000
  1. Class Template:

In class templates, templates for a class is defined.

Syntax:

template <class Ttype>

class class_name 

{

 // code to be executed 

}

  • Ttype:

This parameter is used to represent a placeholder name, to be replaced by the compiler, for a data type, within the class definition.

Syntax: Creating an instance of a class

class_name <type> ob;

  • type:

This parameter is used to specify the type of the data that the class is operating on.

  • ob:

This parameter is used to specify the name of the object.

Syntax: Class Templates with Multiple Parameters:

template<class T1, class T2, ……>

class class_name

{

 // code to be executed 

}

Non Type Template Arguments

The non-type arguments such as strings, function names, constant expression and built-in types can also be used in addition to the type T argument.

Example:

#include <iostream.h>  
using namespace std;  
template<class X>  
class A   
{  
public:  
X n1 = 50;  
X n2 = 20;  
X n3 = 30;
void mul()  
{  
std::cout<<"Product of " << n1 << ", " << n2 << " and " << n3 <<" is : "<< n1*n2*n3<<std::endl;  
}  
};  
 
int main()  
{  
A<int> m;  
m.mul();  
return 0;  
}

Output

Product of 50, 20 and 30 is : 30000
Please follow and like us:
error
Posted in CPP   








Copyright © 2019 CodesJava DMCA.com Protection Status