Code About Us Tutorial


    An Introduction to C++
    Statements & Expression
    Basic Types
    Arrays
    Functions
    Pointers and References
    Basic Classes
    Inheritance
    Templates


   
 

Home > Templates

November 27, 2009 9:38 pm

   

Templates

See Also:
Template Functions
Multiple Template Parameters
Template Specialization
Class Templates
Linked List
Linked List Template
Binary Tree
Today we are going to learn about Templates and how they help us. Templates are very useful because they can be used to produce functions and classes. You could have several different colors of dough and only one cutter shaped like a star. The cutter will still create star shaped pieces of dough no matter what color the dough is. It does not have to have a different cutter for each different color of dough. This is a sort of way to visual a template. You have two integers you wanted to swap. We would make a function to swap these two numbers like so:

 

 
void swap(int &rx, int &ry)
{
	int temp;

temp = rx; rx = ry; ry = temp; cout <<"Swap. After swap." << "n"; cout <<"&rx: " << rx << "n"; cout <<"&ry: " << ry << endl; }

But if you wanted to swap two floats or chars or longs then you would have to create a whole different function. We can skip this by creating a template function. Please Rate this Code:


    Comments for: Templates
  There are NO user contributed comments for Templates.



Add Comment:
Name:
Email:


 «2» THINKQUEST TEAM C0111571 © 2001. All Rights Reserved.