COM221 11 Templates
COM221 11 Templates
1
Templates
• A template establishes a pattern.
• A blueprint for creating a generic class or function.
• C++ enables the definition of a behaviour that applied to objects of varying
types.
• Templates can be declared for both functions and classes.
• Involves developing code that is independent of a particular type.
2
Templates
• Function Templates
• It is a definition of a function using templates.
• Declaration:
template_keyword <parameter list>
return_type function_name(parameters)
• The items inside the angular brackets are referred to as a type parameter list.
• A place holder for the actual data type used by the function.
• The template keyword tells the compiler that it this is template declaration or
definition
• Applies to both classes and functions.
3
Templates
• Function Templates
//Function declaration
//adds two variables together and prints out the result.
template<typename TYPE>
TYPE AddAndPrint(TYPE varA, TYPE varB);
//Function Definition
template<typename TYPE>
TYPE AddAndPrint(TYPE varA, TYPE varB)
{
TYPE result = varA + varB;
cout << "Result is: " << result << endl;
return result;
}
4
Templates
• Function Templates
• Note in the example the keyword typename always comes before a template
type parameter name i.e typename TYPE.
• Instead of typename, class keyword can also be used e.g.
template<class TYPE>
TYPE AddAndPrint(TYPE varA, TYPE varB);
5
Templates
• Function Templates
int main()
{
int iVarA = 2;
int iVarB = 5;
6
Templates
• Function Template
• Calling/Invoking a function template causes the compiler to create that
function.
• Replaces the template type with the actual data type used by the function.
• Templates help minimize the need to repeat or replicate code.
• Not necessary to add more versions of a function for required specific types.
• Virtually any data type can be plugged into the function template.
7
Templates
• Multiple template parameters
• The template parameter list can be expanded to declare multiple template
parameters.
• Separated by a comma.
8
Templates
• Multiple template parameters
//Function declaration
//Prints out the values in two variables of different type
template<typename TYPE_A, typename TYPE_B>
void Print(TYPE_A varA, TYPE_B varB);
//Function definition
template<typename TYPE_A, typename TYPE_B>
void Print(TYPE_A varA, TYPE_B varB)
{
cout << "Variable A is: " << varA << endl;
cout << "Variable B is: " << varB << endl;
}
9
Templates
• Multiple template parameters
int main()
{
//Printout a variable of type string and float
Print<string, float>("This is a string", 25.0f);
return 0;
}
10
Templates
• Class templates
• Templatized versions of C++ classes.
• Defines an abstract type
• Whose behaviour is generic and reusable.
11
Templates
template<typename T, typename T2> //Header file
class CTemplateClass
{
public:
CTemplateClass();
CTemplateClass(T& value1, T2& value2);
~CTemplateClass();
T GetData();
void SetData(T dataToSet);
void PrintData();
private:
T m_Data; //Member variable of template type T
T2 m_Data2; //Member variable of template type T2
};
12
Templates
//In the header file
template<typename T, typename T2>
CTemplateClass<T, T2>::CTemplateClass(T& value1, T2& value2)
{
m_Data = value1;
m_Data2 = value2;
}
template<typename T, typename T2>
CTemplateClass<T, T2>::~CTemplateClass()
{
}
13
Templates
template<typename T, typename T2> // Class Function definition in the header file
T CTemplateClass<T, T2>::GetData()
{
return m_Data;
}
template<typename T, typename T2>
void CTemplateClass<T, T2>::SetData(T dataToSet)
{
m_Data = dataToSet;
}
template<typename T, typename T2>
void CTemplateClass<T, T2>::PrintData()
{
cout << "ValueA is: " << m_Data << endl;
cout << "ValueB is: " << m_Data2 << endl;
}
14
Templates
#include "TemplateClass.h”//Note we include the header file here
int main()
{
CTemplateClass<int, float> tVar = CTemplateClass<int, float>();
tVar.SetData(25);
tVar.PrintData();
return 0;
}
15
Templates
• Template classes
• Declaration does not differ much from template functions.
• Can have multiple template parameters in parameter list.
• Class method implementations require the parameter list before the function
name.
• Instances of template classes are created by explicitly specifying the
template parameters.
• Only member functions called in the program are
instantiated/created by compiler.
16