TEMPLATES IN C++ -Sanchit Karve born2c0de@hotmail.com INTRODUCTION Before I start this tutorial I assume that you have a Working Knowledge of 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 gr8 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 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< template T min(T a,T b) { return (a < b)?a:b; } void main() { int a=10,b=5; cout< 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 function..the overloaded function gets called...not the Templated version. What if we wanted a multi-parametered Templated Function? Here is the Syntax template void function(T item,Q data){} You now know enough of Function Templates.Now it's time to move onto Class Templates. OK.Now let us assume that we have a Stack class(Data Structure) that is capable of storing only int's. Now suppose you want it to store float's as well.You would normally write the whole class again by Copy/Pasting the Previous class and make the necessary Changes.But hey,There is a much better alternative.Using templates, you can manage just one class that handles different data-types.That way even if you find an error in your class,fixing it will make changes to all the data-types. Here is an example of a templated stack data structure. #include // For sleep() #include // For I/0 #include // FOR MessageBox() API #include #define MAX 10 // MAXIMUM STACK CONTENT template // Using Templates so that any type of data can be // stored in Stack without multiple defination of class class stack { protected: T arr[MAX]; // Contains all the Data public: T item,r; int top; //Contains location of Topmost Data pushed onto Stack stack() //Constructor { for(int i=0;ia; // Create object of class a with int Template int opt=1; while (opt!=3) { clrscr(); cout<<" MAX STACK CAPACITY="<<((MAX-a.top)-1)<<"\n\n\n\n"; cout<<"1) Push Item\n"; cout<<"2) Pop Item\n"; cout<<"3) Exit\n\n"; cout<<"Option?"; cin>>opt; switch(opt) { case 1: cout<<"Which Number should be pushed?"; cin>>a.item; a.push(a.item); break; case 2: a.r=a.pop(); cout<<"Item popped from Stack is:"< s1; means that the value T is substituted by int. User-Defined Data Types can also be used provided the required overloaded operator functions are present. In case if we define the class functions outside the templated class,the syntax changes a bit.For a Constructor: template stack ::stack() { //Code } Yes,we have to define that template thing everytime we write a function outside the class.But if a function returns an object of the same templated class the syntax would be like this: stack stack ::func(int data) { //code } Templates can also have pre-defined values like this: template class stack { private: T data[max]; //rest of the members... }; Templates can be inherited as well like this: class newstack:public stack { //members... }; That's all about templates.If you use Templates Frequently you will save a lot of typing and debugging time. Suggestions\Querys can be brought to my notice at born2c0de@hotmail.com HAVE FUN CODING!!! -Sanchit Karve born2c0de@hotmail.com