0% found this document useful (0 votes)
33 views34 pages

CS - 1004 - Week-13 - Introduction To Templates - Hassan

The document discusses templates in C++. Templates allow functions and classes to operate on generic types rather than specific types. This allows code to be reused for different data types. The document provides examples of template functions that swap values and template classes like stacks and linked lists that work for any data type.
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)
33 views34 pages

CS - 1004 - Week-13 - Introduction To Templates - Hassan

The document discusses templates in C++. Templates allow functions and classes to operate on generic types rather than specific types. This allows code to be reused for different data types. The document provides examples of template functions that swap values and template classes like stacks and linked lists that work for any data type.
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/ 34

CS 1004 – Object Oriented

Programming

Dr. Hassan Sartaj


Introduction to Templates
Functions
Main reason for using functions: make pieces of code
reusable by encapsulating them within a function.
Example: Interchange the values of two int variables x and y.
Instead of inline code:
int temp = x;
x = y;
y = temp;
write a function:

void Swap(int & first, int & second)


{
int temp = first;
first = second;
second = temp;
}
3
General Solution
This function gives a general solution to the interchange
problem for ints (to exchange the values of any two
integer variables):
Swap(x, y);
...
Swap(w, z);
...
Swap(a, b);

NOTE: Inline code would have to be rewritten for each pair of


integer variables to swap.

4
But not for doubles…
To interchange the values of two double variables:
Cannot use the preceding function; it swaps ints not doubles.

However, overloading allows us to define multiple versions of the same


function:
/* Function to swap two double variables */
void Swap(double & first, double & second)
{
double temp = first;
first = second;
second = temp;
}

The two different Swap functions are distinguished by


the compiler according to each function's signature
(name, number, type, and order of parameters).
5
Signature
And for strings…
To interchange the values of two string variables:
Again, overload function Swap():

/* Function to swap two string variables*/


void Swap(string & first, string & second)
{
string temp = first;
first = second;
second = temp;
}

6
What about User Defined Types?
And so on ... for other types of variables.

We would have to overload Swap() for each user-defined type:


/* Function to swap two Time variables */
void Swap(Time & first, Time & second)
{
Time temp = first;
first = second;
second = temp;
}

7
Observations:
• The logic in each function is exactly the same.

• The only difference is in the type of the values


being exchanged.

• If we could pass the type as an argument,


we could write a general solution that could be
used to exchange the values of any two
variables.

8
Template Mechanism
Declare a type parameter (type placeholder) and use it in
the function instead of a specific type. This requires a different
kind of parameter list:

template <typename DataType > // type parameter

void Swap(________
DataType & first, ________
DataType & second)

{
________
DataType temp = first;
first = second;
second = temp;
}

9
Template Mechanism Comments

•The word template is a C++ keyword specifying that


what follows is a pattern for a function not a function
definition.

•Originally, the keyword class was used instead of


typename in a type-parameter list.

10
How is a Template Used?
<typename DataType> names DataType as a type parameter
— value will be determined by the compiler from the type of the
arguments passed when Swap() is called.
Example:
#include "Time.h"
#include "Swap.h" //ONE function template definition
int main()
{
int i1, i2;
double d1, d2;
string s1, s2;
Time t1, t2;
... // Compiler generates definitions
// of Swap() with DataType replaced
Swap(i1, i2); // by int
Swap(d1, d2); // by double
Swap(s1, s2); // by string
Swap(t1, t2); // by Time 11
}
General Form of Template
template <typename TypeParam>
FunctionDefinition
or
template <class TypeParam>
FunctionDefinition
where:
TypeParam is a type-parameter (placeholder) naming
the "generic" type of value(s) on which the function operates
FunctionDefinition is the definition of the function, using type
TypeParam.

12
Another Example

13
14
Template Instantiation
• A function template is only a pattern that describes how
individual functions can be built from given actual types.
• This process of constructing a function is called
instantiation.

• We instantiated Swap() four times — with types int,


double, string, and Time.

• In each instantiation, the type parameter is said to be


bound to the actual type passed.

• A template thus serves as a pattern for the definition of


an unlimited number of instances.
15
Class Templates
• The template concept can be extended to
classes.

16
Examples - Stack
• Stack: a LIFO (last in, first out) data structure
• insertions and deletions take place at one end
called Top

• Examples:

17
Integer Stack …

18
Long Stack …

19
20
21
• If the member functions are defined externally
(outside of the class specification), we need a
new syntax.

• The expression template<class Type> must


precede not only the class definition, but each
externally defined member function as well.

22
23
General form of class template declaration
template <typename TypeParam > or
template <class TypeParam>
class SomeClass
{
// ... members of SomeClass ...
};

More than one type parameter may be specified:


template <typename TypeParam1,..., Can also be
typename TypeParamn> set to a
class SomeClass default value
{
// ... members of SomeClass ...
};

24
Instantiating class templates
To use a class template in a program/function/library:
Instantiate it by using a declaration of the form
ClassName<Type> object;
to pass Type as an argument to the class template definition.
Examples:
Stack<int> intSt;
Stack<string> stringSt;
Compiler will generate two distinct definitions of Stack
— two instances — one for ints and one for strings.

25
A Linked List Class Using Templates

26
27
28
29
30
31
32
33
Thank you!

You might also like