Advertisement
3_2004-2005 Miscellaneous #146320

Template Tutorial

Explains function and class templates.

AI

Podsumowanie AI: This codebase represents a historical implementation of the logic described in the metadata. Our preservation engine analyzes the structure to provide context for modern developers.

Kod źródłowy
original-source
<HTML>
<FONT FACE="FIXEDSYS" COLOR="#BD0000">
<CENTER><B> Template Tutorial </center></b><br><br>
"Independent concepts, should be independently represented and should only be combined when needed" - Bjarne Stroustrup <br><br> Templates are a important part of C++ programming and allow the creation of flexible type indenpedent containers. Templates provide support for generic programming, or that is programming using types as parameters. The standard library includes many templates like vector and map. There are 2 types of templates, function templates, and class templates, lets go over them both.
<br><br>
<b> Function Templates </b><br><br>
Lets go over a simple function<br>
<pre>
void Swap(int& x, int& y)
{
int temp;
temp = x;
x = y;
y = temp;
}
void Swap(char& x, char& y)
{
char temp;
temp = x;
x = y;
y = temp;
}
</pre>
Now we could go and and overload every possible data type<br> for the Swap function, but that would be tedious <br>and time consuming. Using a template we could make a generic function<br> using a template that would allow us to<br> pass a data type as a parameter<br>. Heres the layout of what a function template should look like: <br><br>
<pre>
template&lt;class type>
return_type function_name(parameters)
{
}
</pre>
now lets look at a are Swap function as a template<br><br>
<pre>
template&lt;class type>
void Swap(type& x, type& y)
{
type temp;
temp = x;
x = y;
y = temp;
}
</pre>
Now that we have written are template we can<br> call it simply like this with multiple data types 
<pre>
char a = 'a';
char b = 'b';
Swap&lt;char>(a,b);
int x = 100, y = 200;
Swap&lt;int>(x,y);
</pre>
As you can see in the example above, we only wrote one <br>template function, but used it on multpile data types.<br><br>
<b> Classes </b><br><br>
We can also uses classes, to create type independent containers and data structures.<br>This is were it gets really fun. Heres a layout of <br>what a class template should look like<br><br>
<pre>
template&lt;class type>
class Object {
type array[100];
Object();
};
template&lt;class type>
Object&lt;type>::Object()
{
}
</pre>
To Make a instance of this template we would simply go<br>
<pre>
Object&lt;int> object;
</pre>
To get a better understanding of this concept lets go over a simple program using a class template, creating a vector.<br><br>
<pre>
template &lt;class type>
class Vector
{
public:
	Vector();
	Vector(int size);
	~Vector();
	type operator[](int pos);
	void Resize(int size);
	void Add(type t);
	void Asn(int pos, type t);
 	type Get(int pos);
	unsigned long Length();
private:
	type* array;
	unsigned long size;
	int i;
};
template&lt;class type>
Vector&lt;type>::Vector()
{
	array = 0;
	size = 0;
}
template &lt;class type>
Vector&lt;type>::Vector(int sizex)
{
	size = sizex;
	array = new type[ size ];
}
template &lt;class type>
Vector&lt;type>::~Vector()
{
	if( array != 0 )
	{
		delete [] array;
		array = 0;
	}
}
template &lt;class type>
type Vector&lt;type>::operator [](int pos)
{
	if( pos > size || pos < 0 && array != 0 ) { return 0; }
	return array[pos];
}
template &lt;class type>
void Vector&lt;type>::Resize(int sizex)
{
	type* temp = new type[ size ];
	for(i = 0; i < size; i++)
	{
		temp[i] = array[i];
	}
	
	if(array != 0)
	{
	delete [] array;
	array = 0;
	}
	array = new type [ sizex ];
	for(i = 0; i < size; i++)
	{
		if(i < sizex) 
		{
		array[i] = temp[i];
		}
	}
	delete [] temp;
	temp = 0;
	size = sizex;
}
template &lt;class type>
void Vector&lt;type>::Add(type t)
{
	int size2;
	size2 = size + 1;
	Resize(size2);
	array[size2-1] = t;
}
template&lt;class type>
unsigned long Vector&lt;type>::Length()
{
	return size;
}
template&lt;class type>
void Vector&lt;type>::Asn(int pos, type t)
{
	if(pos < size && pos >= 0) 
	{
		array[pos] = t;
	}
}
template&lt;class type>
type Vector&lt;type>::Get(int pos)
{
	return this[pos];
}
#include&lt;iostream>
using namespace std;
int main()
{
	Vector&lt;char> vect;
	vect.Add('m');
	vect.Add('a');
	vect.Add('s');
	vect.Add('t');
	vect.Add('e');
	vect.Add('r');
	vect.Add('!');
	for(int i = 0; i < vect.Length(); i++)
	{
		cout << vect[i];
	}
	cout << "\n example driver by Jared Bruni" << endl;
	return (system("pause"));
}
</pre>
<br><br>
There we go, simple template tutorial :)
</FONT>
</HTML>
Oryginalne komentarze (3)
Odzyskane z Wayback Machine