C++ Class Templates

 

C++ Class Templates

 

Class templates are one application of templates. Class templates could be used according to the requirements of the programmer. Templates are nothing but shortened generic declarations of similar entities. They are expanded at the compile time. 

 

Examples of some of the in-built class templates are,

Vector, Set, Linked List etc.

 

Class templates are helpful for classes that are independent of the data type. And like any other function, there could be more than one parameter to a template. On the basis of the number of parameters of a template, they could be single parameters as well as multiple parameters. 

 

A. Single parameter

Syntax for declaring a single parametrized template is,

#include <iostream>
using namespace std;
 
template <class T>
class nameOfClass
{
    //body
};
 
int main()
{
    //body of main
}

 

The example of the vector easily demonstrates how templates are used with a single parameter.

#include <iostream>
using namespace std;
 
template <class T>
class vector
{
    T *arr;
    int size;
};
 
int main()
{
    vector<int> v1();
    vector<float> v2();
}

 

B. Multiple Parameter

Syntax for declaring a multiple parametrized template is,

#include <iostream>
using namespace std;
 
template <class T1, class T2>
class nameOfClass
{
    //body
};
 
int main()
{
    //body of main
}

 

The difference lies only in the number of parameters we declare inside the template. Consider an example that demonstrates the use of multiple parameters in a class template.


#include <iostream>
using namespace std;
 
template <class T1, class T2>
 
class myClass
{
public:
    T1 data1;
    T2 data2;
    myClass(T1 a, T2 b)
    {
        data1 = a;
        data2 = b;
    }
    void display()
    {
        cout << this->data1 << " " << this->data2;
    }
};
 
int main()
{
    myClass<char, int> obj('C', 1);
    obj.display();
}


Output

C 1

 

Now, we defined an object obj in myClass to hold a character and an integer data. It could be anything else since both the parameters have been generalized using the template.

 

C. Default Parameter

C++ Templates have this additional ability to have default parameters. Its ability to have default specifications about the data type, when it receives no arguments from the main is a powerful attribute.

 

The syntax for making a parameter have a default datatype,

template <class T1 = datatype_1, class T2 = datatype_2>

Now, even if the programmer does not specify the type of the data, objects created using this template will automatically make it, of the form <datatype_1, datatype_2>.

 

Consider an example demonstrating the application of the default parameters,

#include <iostream>
using namespace std;
 
template <class T1 = int, class T2 = float, class T3 = char>
class myClass
{
public:
    T1 data1;
    T2 data2;
    T3 data3;
 
    myClass(T1 a, T2 b, T3 c)
    {
        data1 = a;
        data2 = b;
        data3 = c;
    }
    void display()
    {
        cout << "The value of a is " << data1 << endl;
        cout << "The value of b is " << data2 << endl;
        cout << "The value of c is " << data3 << endl;
    }
};
 
int main()
{
    myClass<> obj1(1, 4.3, 'C');
    obj1.display();
}


Output

The value of a is 1
The value of b is 4.3
The value of c is C

 

The template has default defined its parameters as an integer, a float, and a character. Now, if the programmer wishes to change the data types, it must be exclusively mentioned and the object must be defined as it was done in earlier examples.