Loading....
Coupon Accepted Successfully!

 

Definition Of Class Templates

In order to declare a class of template type, the following syntax is used:
 

Template Declaration

template < class T>

class name_of_class

{

// class data member and function

}

 
The first statement template < class T> tells the compiler that the following class declaration can use the template data type. The T is a variable of template type that can be used in the class to define a variable of template type. Both template and class are keywords. The <> (angle bracket) is used to declare the variables of template type that can be used inside the class to define the variables of template type. One or more variables can be declared separated by a comma. Templates cannot be declared inside classes or functions. They should be global and should not be local.
T k;
 
where, k is the variable of template type. Most of the authors use T for defining a template; instead of T, we can use any alphabet.
 
17.1 Write a program to show values of different data types using overloaded constructor.
#include<iostream.h>
#include<conio.h>
class data
{
public:
data(char c)
{cout<<“\n”<< “ c=”<<c <<“ Size in bytes:”<<size of(c);}
data(int c)
{cout<<“\n”<< “ c=”<<c <<“ Size in bytes:”<<size of(c);}
data(double c)
{cout<<“\n”<< “ c=”<<c <<“ Size in bytes:”<<size of(c);}
};
int main()
{
clrscr();
data h(‘A’); // passes character type data
data i(100); // passes integer type data
data j(68.2); // passes double type data
return 0;
}
OUTPUT
c = A Size in bytes :1
c = 100 Size in bytes :2
c = 68.2 Size in bytes :8

 

Explanation: In the above program, the class data contains three overloaded one-argument constructors. The constructor is overloaded for char, int, and double type. In function main(), three objects h, i, and j are created, and the values passed are of different types. The values passed are char, int, and double type. The compiler invokes different constructors for various data types. Here, in order to manipulate different data types, we require to overload the constructor; that is, defining a separate function for each non-compatible data type. This approach has the following disadvantages:
  1. Re-defining the functions separately for each data type increases the source code and requires more time.
  2. The program size is increased. Hence, more disk space is occupied.
  3. If the function contains a bug, it should be corrected in every function.
  4. Figure shows you the working of a program.

127446.png

Fig: Working of non-template function
 
From the above program, it is clear that for each data type we need to define a separate constructor function. According to data, type of argument passed respective constructor is invoked. C++ provides templates to overcome such a problem and helps the programmer develop a generic program. The same program is illustrated with the template as follows:
 
17.2 Write a program to show values of different data types using constructor and template.
#include<iostream.h>
#include<conio.h>
template<class T>
class data
{
public:
data (T c)
{
cout<<“\n”<< “ c=”<<c <<“ Size in bytes:”<<size of(c);
}
};
int main()
{
clrscr();
data <char> h(‘A’);
data <int> i(100);
data <float> j(3.12);
return 0;
}
OUTPUT
c = A Size in bytes :1
c = 100 Size in bytes :2
c = 3.12 Size in bytes :4

 

Explanation: In the above program, the constructor contains a variable of template T. The template class variable can hold values of any data type. While declaring an object, the data type name is given before the object. The variable of template type can accept the values of any data type. Thus, the constructor displays the actual values passed. The template variable c can hold the values of any data type. The value and space in bytes required by these variables are displayed at the output. The size of data type changes according to the data types used in the program. Figure shows the working of the program.
 
127823.png
Fig: Working of template function
 
In the above program, different values are passed using constructors, but for all data types, the same template function is used.




Test Your Skills Now!
Take a Quiz now
Reviewer Name