article

Templates Made Easy!

Email
Submitted on: 1/3/2015 1:45:00 AM
By: Andrew Hull (from psc cd)  
Level: Intermediate
User Rating: By 5 Users
Compatibility: C++ (general), Microsoft Visual C++, Borland C++, UNIX C++
Views: 268
 
     This is an introduction to templates and their uses. Easy to learn! You should be familiar with functions and classes.

 
				

Templates Made Easy

 

So, what exactly is a template? You've probably heard the word before, perhaps you've had experience with the STL, or Standard Template Library. So what makes templates so important and useful? Let's consider a simple swap function to exchange 2 values:

void Swap(int &A, int &B)
{
int Temp = A;
A = B;
B = Temp;
}

This functions takes the address of 2 int's and swaps their values. Now, what happens if you want to swap 2 chars? Easy, you can overload the Swap() function:

void Swap(char &A, char &B)
{
char Temp = A;
A = B;
B = Temp;
}

You can keep overloading for every primitive data type there is (int, char, float, double, bool, etc). However, what if you want to swap 2 objects? What if your program has 10 different types of objects, all of which need to be swapped? It seems like a lot of work to keep writing overloaded Swap() functions for every type you want to swap. Isn't there some way to create an "outline", or template to describe a generic Swap() function that will work for ANY data type? The syntax for a templated function is:

template <class type>
return_type function_name (parameters)
{
// function body
}

"T" is the most often used class name for template. (T = type). So, here is the Swap() function, this time with a template:

template <class T>
void Swap(T &A, T &B)
{
T Temp = A;
A = B;
B = Temp;
}

template defines T to be a generic data type- it will be filled in later when the function is called. The parameters A and B are of type T, meaning their type will be filled in later. This is the syntax for calling our Swap() function, as well as any templated function:

int a = 1, b = 2;
Swap<int>(a, b);

The compiler translates this to:

void Swap(int &A, int &B)
{
int Temp = A;
A = B;
B = Temp;
}

Here is the syntax for calling with char values:

char a = 'a', b = 'b';
Swap<int>(a, b);

This becomes:

void Swap(char &A, char &B)
{
char Temp = A;
A = B;
B = Temp;
}

See the pattern? The value given to the function in the <> brackets replaces T in the function definition. Now, here's where the real usefulness of templates comes in: classes. Say you make a simple list function that you want to hold any type of data: primitive types or class objects. You'll be distributing the code and you don't know what objects will be put into your list. You can apply the template keyword to your class and its member functions to create objects that can hold any data type! Take this example of the list class:

template <class T>
class List
{
public:
void AddItem(T item);
void DeleteItem(T item);
void ShowItems();
private:
T* list;
};

template <class T>
void List::AddItem(T item)
{
// ...
}

template <class T>
void List::DeleteItem(T item)
{
// ...
}

template <class T>
void List::ShowItems()
{
// ...
}

And so on...

You can see that T will be replaced by whatever data type is given to the object. the template keyword needs to be put before the class declaration as well as each function definition. Here is the syntax creating a List object:

List<int> myList;

This creates a List of int's. All T's in the class and functions are replaced by int. Now, let's say you have a simple struct like this:

struct data
{
int a;
double b;
char c;
};

And you want to make a list of data's. With a template, it's easy!

List<data> myList;

And you have a List of data's!

-----------------------------------------------------------------------------------------------

I hope you've learned something from this! Please leave any comments/feedback that come to mind, everything is appreciated! If you need anything cleared up, feel free to email me at kavutitan26@yahoo.com. Enjoy!

-> Andrew <-


Other 2 submission(s) by this author

 


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this article (in the Intermediate category)?
(The article with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments


 There are no comments on this submission.
 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular article, please click here instead.)
 

To post feedback, first please login.