Share on facebook
Share on twitter
Share on pinterest

In this C++ tutorial, let us see about Templates, its types with an example program in C++

Introduction of Templates

Templates define the structure of a function or a class irrespective of the data types used. So, while using templates, the same function can be used with different data types. The template may be defined as the blueprint for functions & classes. Templates don’t define the data types during function or class declaration.
The data types for the function are specified only at the time of function call whereas the data types for the class are specified at the time of installation of class. So, templates enable the programmer to do generic programming.

Function Templates

Function templates provide support for generic functions. Generic functions define the code for the function but don’t specify the datatypes. The datatypes for the variables used in the function are specified when a call is made to the function. Templates are used only when the body of the function is exactly the same for different data types.

Syntax 

template <class T>
T func (T arg)
{
}

When the compiler encounters the “template” keyboard, it doesn’t generate any code. it doesn’t generate the machine code as it doesn’t know the data type. It keeps a note of the template so that it can be used later.
When a call is made to the function & the data types used in the function are specified, the compiler actually generates the code. Compiler derives the data type from the data type of the argument passed to the function & generates the code for the specific datatypes i.e. if int value is passed, T would be replaced by int in the function template. This is known as an instantiation of the template. Each instance is known as the template function.

Benefits 

Using the function template, the source code file is comparitively small as only a single version of the function is required in the source code for different data types. If ever a change is required in the function, the changes have to be made at one place & would be effective for all the data types used in various function calls.

C++ Program

The following C++ program is to find the sum of two numbers using function template.

#include<iostream.h>
#include<conio,h>
template <class T>
T sum(T a, T b)
{
return (a + b);
}
void main()
{
clrscr();
cout<<"\nSum Of integers : "<<sum(4,9);
cout<<"\nSum Of long : "<<sum(234334,34539);
cout<<"\nSum Of floats : "<<sum(4.5,9.9);
cout<<"\nSum Of doubles : "<<sum(344544.54,92423.45);
cout<<"\nSum Of chars : "<<sum('A','K');
}
Output
sum of integers : 13
sum of long : 268873
sum of floats : 14.4
sum of doubles : 436967.99
sum of chars : 1/4

Class Template

A template can also be defined for classes.

Sample C++ program for class templates

The following code defines a cylinder class that has radius & height variables as members of the class. The class has two member function, area and circumference.

#include<iostream.h>
#include<conio.h>
template <class T>
class cyinder
{
T radius;
T height;
public:
cylinder(T r, T h)
{
radius = r;
height = h;
}
void area()
{
cout<<"\nArea of cylinder = "<<3.14f* radius * radius * height <<endl;
}
void circumference()
{
cout<<"\nCircumference = "<<2 * 3.14f * radius * height <<endl;
}
};
void main()
{
clrscr();
cylinder <int> obj1(12,34);
cylinder <float> obj2(23.4,45.6);
obj1.area();
obj1.circumference();
obj2.area();
obj2.circumference();
}
Output
Area of cylinder = 15373.44043
Circumference = 2562.23999
Area of cylinder = 78401.828125
Circumference = 6701.01123
-

Subscribe to our Newsletter

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Ut elit tellus, luctus nec ullamcorper mattis, pulvinar dapibus leo.

Share this post with your friends

Share on facebook
Share on google
Share on twitter
Share on linkedin

Leave a Reply

Your email address will not be published. Required fields are marked *