View Feed
Coffee Room
Discuss anything here - everything that you wish to discuss with fellow engineers.
12921 Members
Join this group to post and comment.
salaikumar • Mar 22, 2008

regarding template in OOP

hi friends, i am not clear with template in oops concepts .will anyone explain its basic things in this topic.
friendster7 • Mar 22, 2008


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.


using namespace std;

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;

int main()
 int a=10,b=5;
 cout<Function Overloading doesn't help much as far as typing time and source space isconcerned.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...


using namespace std;

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

int main()
 int a=10,b=5;
 cout<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 us first called
with this:
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...

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

void abs(char a){}
This way even if accidentally 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

void function(T item,Q data){}
You now know enough of Function Templates.Now it's time to move onto Class

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

using namespace std;

#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


  T arr[MAX];       // Contains all the Data

    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)
 cout<<" MAX STACK CAPACITY="<<((<<"\n\n\n\n";
 cout<<"1) Push Item\n";
 cout<<"2) Pop Item\n";
 cout<<"3) Exit\n\n";
  case 1:
  cout<<"Which Number should be pushed?";

  case 2:
  cout<<"Item popped from Stack is:"< Everything looks similiar except an object declaration
stack 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

In case if we define the class functions outside the templated class,the
syntax changes a bit.For a Constructor:
        stack ::stack()
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)
Templates can also have pre-defined values like this:
        class stack
          T data[max];
          //rest of the members...
Templates can be inherited as well like this:
         class newstack:public stack
That's all about templates.If you use Templates Frequently you will save
a lot of typing and debugging time.
salaikumar • Apr 12, 2008
hi my dear friend it is nice and useful for me.thank a lot.hate's of to u .

Share this content on your social channels -