Cyberithub

2 Popular Types: C++ Template Class and C++ Template Function

In this tutorial, i will take you through C++ Template Concepts and its usage . C++ Template can be used to create a family of classes or function. For ex. A C++ template Class for an array class would enable us to create arrays of various data types say int array, float array etc.

Similarly template for function can be defined. Ex. multiply() ,it would help us multiplying different types of data by creating different version of multiply() function say int, float etc.

What is Template

Templates is one of the C++ feature that provides support for generic programming.It enable us to define generic classes and functions.

 

2 Popular Types: C++ Template Class and C++ Template Function 1

C++ Template Concepts 

Also Read: Learn 3 Types of Constructors in C++ with Best Examples

C++ Template Class

As mentioned earlier, C++ templates allow us to define generic classes. It can be Parameterized with one or more types. Container classes, which are used to manage elements of a certain type, are a typical example of this feature. By using class templates, you can implement such container classes while the element type is still open.

Syntax:

template<class T>
class classname
{
..........
..........
..........
};

Example:

#include <iostream>
using namespace std;

template <class T>
class Array {
private:
T *ptr;
int size;
public:
Array(T arr[], int s)
{
ptr = new T[s];
size = s;
for(int i = 0; i < size; i++)
ptr[i] = arr[i];
}
void display()
{
for (int i = 0; i < size; i++)
cout<<" "<<*(ptr + i);
cout<<endl;
}
};

int main() {
int arr[5] = {1, 3, 5, 7, 9};
Array<int> b(arr, 5);
b.display();
return 0;
}

C++ Template Class with Multiple Parameters

It is possible to use more than one generic data type in a class template. It can be declared as a comma separated list within the template specification.

Syntax:

template<class T1, class T2, ...>
class classname{
..........
..........
..........
};

Example:

#include<iostream>
using namespace std;
template<class T1, class T2>
class Test
{
T1 x;
T2 y;
public:
Test(T1 a, T2 b)
{
x = a;
y = b;
}
void show()
{
cout<<x<<"\t"<<"and"<<"\t"<<y<<"\n";
}
};

int main()
{
cout<<"Data type float & int passed for class template test1:"<<"\n";
Test<float, int>test1(1.23, 123);
test1.show();
cout<<"Data type int & char passed for class template test2:"<<"\n";
Test<int, char>test2(3, 'W');
test2.show();
return 0;
}

C++ Template Function

We could also define function templates that can be used to create a family of functions with different argument types. Function templates provide a functional behavior that can be called for different types. In other words, a function template represents a family of functions. The representation looks a lot like an ordinary function, except that some elements of the function are left undetermined: These elements are parameterized.

Syntax :

template<class T>
returntype functionname (arguments of type T)
{
......
......
......
}

Below program shows how a template function is defined and implemented.

#include <iostream>
#include <string>

using namespace std;

template <class T>
T Max (T &a, T &b) {
return a < b ? b:a;
}

int main () {
int x = 39;
int y = 20;
cout << "Max(x, y): " << Max(x, y) << endl;

float f1 = 13.5;
float f2 = 20.7;
cout << "Max(f1, f2): " << Max(f1, f2) << endl;

string s1 = "Hi";
string s2 = "There!!";
cout << "Max(s1, s2): " << Max(s1, s2) << endl;

return 0;
}

C++ Template Function with multiple parameters

We also can use more than one generic data type in the C++ template statement.

Syntax :

template<class T1, class T2, ...>
returntype functionname (arguments of types T1, T2, ...)
{
......
......
......
}

Below program shows how a C++ template function with multiple parameters is defined and implemented.

#include <iostream>
#include <string>

using namespace std;

template <class T1, class T2>
void display (T1 a, T2 b) {
cout<<a<<"\t"<<b<<"\n";
}

int main () {
cout<<"Call function template with int and character string type parameter"<<"\n";
display(1999, "Hi There!!");
cout<<"Call function template with float and int type parameter"<<"\n";
display(12.34, 100);

return 0;
}

 

Also Read: An Idiotic Guide to C++ Templates

Leave a Comment