Website Design United States, Website Design California, Website Designing United States, Website Designing California

Using Templates in C++

-Sanchit Karve
born2c0de@hotmail.com
INTRODUCTION

Before I start this tutorial I assume that you have a working knowledge of Object Oriented Program (OOP) in C++. Without this, the tutorial will be of no use to you.

This tutorial shows you how to reuse code, save typing time, prevent time spent on debugging and saving source-code space.

OK. Let me take a common example. The great min() function returns the value which is lowest from the given two arguments.I shall show two examples now. One without Templates and one with Templates. Here is the first program:
#include <iostream.h>
int min(int a,int b)
{
return (a < b)?a:b ;
}

float min(float a,float b)
{
return (a < b)?a:b ;
}

char min(char a,char b)
{
return (a < b)?a:b ;
}

void main()
{
int a=10,b=5;
cout<<min(a,b)<<'\n';
char p='A',q='Z';
cout<<min(p,q)<<'\n';
float z=1.91,x=3.98;
cout<<min(z,x)<<'\n';
}

OUTPUT:
5
A
1.91

Function Overloading doesn't help much as far as typing time and source space is concerned. Also if you find an error in one such function, you have to correct it in all the other functions as well. With Templates all the work becomes a lot easier. Let's see the Templated version now:
#include <iostream.h> template <class T>

T min(T a,T b)
{
return (a < b)?a:b ;
}

void main()
{
int a=10,b=5;
cout<<min(a,b)<<'\n';
char p='A',q='Z';
cout<<min(p,q)<<'\n';
float z=1.91,x=3.98;
cout<<min(z,x)<<'\n';
}

Same output. Nearly the same executable size. But what we have achieved is that the time we take to type the code is reduced. The code looks more readable and incase there is an error just correcting it here will make the change for all data types accessing this function.

Let me explain what happens at compile time.When the function is first called with this:
cout<<min(a,b); //a and b are int's

It goes to the function min(). Now T is the value that will be substituted with the data type of the parameter.So here T will be int. So dynamically the function
declaration becomes like this:
int min(int a,int b)

In the second case T takes the form of a char and so on...The compiler on every call still has to generate seperate functions for each data type but what we are saving are typing time and debugging ease.

Some of you might wonder "what if we want all data-types to access a templated function except one?" Like for example in a abs() function we would like all data types to access it except a char data-type.What do we do then? Well in such a case we override the function call like this:

template <class T>

int abs(T no)
{
return (no<0)?(no*-1):no;
}

void abs(char a){}

This way even if accidentaly a char data type is used as a parameter to the abs functio, the overloaded function gets called. Not the Templated version.

What if we wanted a multi-parametered Templated Function? Here is the syntax:

template <class T,class Q>

void function(T item,Q data){}

You now know enough of Function Templates. Now it's time to move onto Class Templates.


Author Information:

Sanchit Karve

http://www.sanchitkarve.com

born2c0de@dreamincode.net

Comments:

Add your comments here.

Name

Comment

You can also send feedback to feedback@programmers-corner.com

Jason - January 6, 2005 4:14 PM

The tutorials quite nice for starters, but your coding style is awfull

sayed Israr - March 9, 2005 2:42 AM

I really Like this tutorial ...
This is very good for freshers...
I Req. U pleae give the ad of yr site in popular sites.. so candidate shoulde take the advantage of thiss...
Thanks a lot.

Osama Mirza - June 3, 2005 6:04 PM

Very nice, easy language, comprehensive

Bobby - December 26, 2005 4:09 AM

This is Excellent for fresher who start C++ few days back

Ch.Ram Krishna - January 19, 2006 4:32 AM

could u plz forward some source code of real time program

Nidhi - February 3, 2006 2:14 AM

Before reading this tutorial , I was afraid of this name "template" but now I am not :)..Thanks

 















 


© 2008-2009 dotnet4all.com