Code
Templates in C++ - by hackervalley - Sunday 1st FebruaryTemplates in C++
Describe use of templates in C++
Inheritance , surdefinition and polymorphisme allow you to reuse object code, but that doesn’t solve all your reuse needs. Templates allow you to reuse source code by providing the compiler with a way to substitute type names in the body of a class or function.
What are Templates ?
Templates are mechanisms for generating functions and classes based on type parameters. They provide generic way to develop reusable code. There are two basic types of templates: function templates and class templates.
By using templates, you can design a single class that operates on data of many types, instead of having to create a separate class for each type.
So Templates are one of the best ways to reuse code itself and reach generic programming goal.
Working with templates.
A - Function Templates
void SwapInt(int &ia , int &ib)
{
int ic;
ic = ia;
ia = ib;
ib = ic;
}
So this function works for all intergers. Now, suppose, you are required to use a function for swapping two float numbers. So, we write this down as:
void SwapFloat(float &fa, float &fb)
{
float fc;
fc = fa;
fa = fb;
fb = fc;
}
Now, suppose, you are required to use a function for swapping two double numbers. Have we again write a new function ? No, we use the template solution for this.
template<typename T>
void SwapT(T &ta, T &tb)
{
T tc;
tc = ta;
ta = tb;
tb = tc;
}
Remark we can write : template
Now, we can instantiate function template. For example :
cout << " i " << i << " j " << j << endl;
SwapT(i,j);
cout << " i " << i << " j " << j << endl;
double f = 5.55, g = 6.66;
cout << " f " << f << " g " << g << endl;
SwapT(f,g);
cout << " f " << f << " g " << g << endl;
B - Class Templates
Suppose we want a vertex class which is able to generate code for whichever legal data type it is instantiated. So, our first attempt is as follow:
{
T x, y, z;
public:
CVertex (T inx = 0, T iny = 0, T inz = 0)
{
x = inx;
y = iny;
z = inz;
}
void vSetCVertex (T inx, T iny, T inz);
void vDisplayCVertex ();
};
template < typename T > void CVertex < T >::vSetCVertex (T inx, T iny, T inz)
{
x = inx;
y = iny;
z = inz;
}
template < typename T > void CVertex < T >::vDisplayCVertex ()
{
cout << "x = " << x << " y = " << y << " z = " << z << endl;
}
Now, we can instantiate class template. For example :
ivertexa.vDisplayCVertex ();
ivertexa.vSetCVertex (1, 2, 3);
ivertexa.vDisplayCVertex ();
CVertex < float >ivertexb;
ivertexb.vDisplayCVertex ();
ivertexb.vSetCVertex (1.111, 2.222, 3.333);
ivertexb.vDisplayCVertex ();
Conclusion
Templates remove code duplication and do generic programming.
Author : hackervalley@free.fr http://hackervalley.free.fr
Copyright (c) 2003 hackervalley Permission is granted to copy, distribute and/or modify this document under the terms of the GNU Free Documentation License, Version 1.2 or any later version published by the Free Software Foundation; with no Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts. A copy of the license is included in the section entitled "GNU Free Documentation License".
CppTemplates.tar.bz2
BZip - 12.9 kb