0% found this document useful (0 votes)
2 views16 pages

COM221 11 Templates

The document provides an overview of templates in C++, including function and class templates. It explains how templates allow for the creation of generic functions and classes that can operate with various data types, minimizing code duplication. Additionally, it covers the syntax for declaring and defining templates, as well as examples of their usage in code.

Uploaded by

bed-com-46-22
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views16 pages

COM221 11 Templates

The document provides an overview of templates in C++, including function and class templates. It explains how templates allow for the creation of generic functions and classes that can operate with various data types, minimizing code duplication. Additionally, it covers the syntax for declaring and defining templates, as well as examples of their usage in code.

Uploaded by

bed-com-46-22
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 16

COM221

Advanced Computer Programming

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;

//This is how you call/invoke a template function


int iResult = AddAndPrint<int>(iVarA, iVarB);
return 0;
}

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

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy