• Main Menu
  • C++ Templates


    A template is one of the features added to C++ recently. It is a new concept which enables the programmer to define generic classes and functions, thus provides support for generic programming. Generic programming is an approach where generic types are used as parameters in algorithms so that they work for a variety of suitable data types and data structures.

    A template can be used to create a family of classes or functions. For example, a class template for an array class would enable us to create arrays of various data types such as int array and float array. Similarly, we can define a template for a function, say multiply(), that would help us create various versions of multiply() for multiplying int, float and double type values.

    A template can be considered a kind of macro. When an object of a specific type is defined for actual use, the template definition for that class is substituted with the required data type. Since a template is defined with a parameter that would be replaced by a specified data type at the time of actual use of the class or function. The templates are sometimes called parameterized classes or functions.

    Syntax:

    template <class T> return-type 
    T function-name( arguments of type T) 
    { 
        // Body of function with type T 
    }

    Consider a vector class defined as follows:

    class vector 
    { 
        private: 
            int *v; 
            int size; 
        public: 
            vector(int m) 
            { 
                v = new int[ size = m ]; 
                for(int i=0; i<size; i++) 
                    v[i] = 0; 
            } 
            vector( int *a, int size ) 
            { 
                for(int i=0; i<size; i++) 
                    v[i] = a[i]; 
            } 
            int operator*(vector &y) 
            { 
                int sum = 0; 
                for(int i=0; i<size; i++) 
                    sum += this->v[i] * y->v[i]; 
                return sum; 
            } 
    }

    Now suppose we want to define a vector that can store an array of float values. We can do this by simply replacing the appropriate int declarations with float in the vector class. This means that we have to redefine the entire class all over again.

    Assume that, we want to define a vector class with the data type as a parameter and then use this class to create a vector of any data type instead of defining a new class every time. The template mechanism enables us to achieve this goal:

    Template definition of vector class is as shown below:

    template<class T> 
    class vector 
    { 
        private: 
            T *v; 
            int size; 
        public: 
            vector(int m) 
            { 
                v = new T [ size=m ]; 
                for(int i=0; i<size; i++) 
                    v[i] = 0; 
            } 
            vector(T *a, int size) 
            { 
                for(int i=0; i<size; i++) 
                    v[I] = a[i]; 
            } 
            T operator*(vector &y) 
            { 
                T sum = 0; 
                for(int i=0; i<size; i++) 
                    sum += this->v[i] * y->v[i]; 
                return sum; 
            } 
    }

    The class template definition is very similar to an ordinary class definition except the prefix template <class T> and the use of type T. This prefix tells the compiler that we are going to declare a template and use T as a type name in the declaration. Thus, vector has become a parameterized class with the type T as its parameter. T may be substituted by any other data type including the user-defined types. Now, we can create vectors for holding different data types.

    A class created from a class template is called a template class. The syntax for defining an object of a template class is as follows:

    classname<type> 
    object-name( argument-list );

    This process of creating a specific class from a class template is called instantiation. The compiler will perform the error analysis only when an instantiation takes place. It is, therefore, advisable to create and debug an ordinary class before converting it into a template.

    Example: Program which illustrates the working of templates.

    #include<iostream.h> 
    template<class T> 
    T sum( T a, T b ) 
    { 
        T c = a + b ; 
        return c ; 
    } 
    void main() 
    { 
        int x = 10, y = 5 ; 
        int z = sum(x, y); 
        cout << "n The sum of two int numbers is: "<< z ; 
        float f1 = 3.5, f2 = 7.5 ; 
        float f = sum(f1, f2); 
        cout << "n The sum of two float numbers is: " << f ; 
        char c1 = 'a', c2 = 'b'; 
        char c = sum(c1, c2); 
        cout << "n The sum of two char variables is: " << c ; 
    }

    Output:

    The sum of two int numbers is: 15
    The sum of two float numbers is: 11
    The sum of two char variables is: Ã

    Got Something To Say:

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

    C
    } 238 queries in 0.508 seconds.