You are on page 1of 13

Templates

Template: is a variable that takes different data types either built in or user
defined.
Same logic is used but on different data types so instead of rewriting the same
logic for each data type we define Template ‫ قالب‬only once that uses a parameter .
when you want to use the template function or the template class you will define
the data type to be used instead of this parameter where the compiler rewrites
the template according to the defined data type (Specialization).
1.Function Template
2.Class Template
3.Special Features

1.Function Template
o Declaration:
template<class T>
or you can use typename
T abs( T n )
{

return (n < 0) ? -n : n; T abs( T n )


} Parameter List
Return type

1|Page Techniques of programming team CMP 2024


Case more than one parameter in<> add ( , ) between them but you must
REWRITE “class”/or/”typename” before each parameter.
template<class T , class N>
N functionname(T a…..)
{…….}

NB:Templates may specify types of:


1.Return type
template<typename T>
T functionname(int n …..)
{
T obj;
………
return obj;
}

2.Arguments in the parameter list:


template<class T>
int functionname( T b)
{ b=…..
return 1;
}

2|Page Techniques of programming team CMP 2024


3.Local variables used within the function
Template<class a>
Int abs(int k)
{
a varaiblename;
…….
}

o Declaring specialization of Class Template: //in other words


calling template function

//main //example on T abs(T n) defined in page(1)


int main()
{
int int1 = 5;
abs(int1); //int abs(int)
/*the compiler here replaces each T with int */
float flo1 = - 700.345 ;
abs(flo1); //float abs(float)
long lon1 = 70000L;
abs(lon1); //long abs(long)
}

3|Page Techniques of programming team CMP 2024


NB: when the data defined for the template class is a user defined data type you
must make sure that any operator used in the template function is overloaded in
the class of the user defined data type.
For example:
Class Date
int operator>(Date D){…….}
{……}
template<class T>
int greater(T a,T b)
{
if(a > b)
ostream& operator<<(ostream&output,const Date d){….}
…………
}
Void PrintINfo()
{….} //we overload function operator in order
to make the global function which the
class’s object invokes do it’s logic correctly

o Overloading Function Template:


template< class T >
void printArray( const T *array, const int count)
May be overload by:
a.Other function templates with SAME NAME but different
parameters
➢ template< class T >
void printArray( const T *array, const int count, int start_index)

4|Page Techniques of programming team CMP 2024


b.Non-template functions with SAME NAME
➢ void printArray( char *array)

➢ Compiler performs matching process:


1. It tries to find precise match of function name and argument
types.
2. It generates function templates with the data type with which
the function is called.
If it fails and gives Error if:
➢ no matching can be found (failed in finding 1. Then 2.)
➢ if there are multiple matches

2.Class Templates

o Declaration:
template<class T>
class mypair
{
int Compare(T a,T b);
};

5|Page Techniques of programming team CMP 2024


o Defining function out scope of the class :
template<class T> >>> MUST be written before each definition of member
functions of the class OUTSIDE ITS SCOPE //Example Compare(T,T);

template<class T>

int mypair <T>::Compare(T a,T b);

{……….//Defination}

Class name Formal Type Parameter

int mypair <T>::Compare(T a,T b);

Return Type

BUT in case that the definition is inside the scope of the class(As we used to
define functions before )

template<class T>
class mypair
{
Int Compare(T a,T b)
{……….//Defination}
};

6|Page Techniques of programming team CMP 2024


o Declaring specialization of Class Template:
//as we used to declare int variable int x; now we need to declare an object of
mypair class but with int version (the compiler will rewrite the class definition by
replacing each T with int ).

int main() {
Mypair <int> myintegerpair;
Return 0; }
Object name

3.Special Features
i.Non-Types Parameters

o Definition:
A template non-type parameter is a special type of parameter that does
not substitute for a type, but is instead replaced by a value.

Non-type parameter

template<class T , int N >


class A
{T arra[N]; ….….};

7|Page Techniques of programming team CMP 2024


When declaring an object of class A
int main()
{
A<int,5>A_object;
}
Each T(normal paramter) in the class Definition is replaced by int
But each N is replaced by 5 >>>>>>>>as if it is a constant
i.e(Nonetype Parameters are treated as constants).

N.B:Non-type parameters allows us to enter size of the array during


run time instead of using new to create dynamic arrays
i.e(This eliminates the run time overhead of using new to create the
space dynamically!)

8|Page Techniques of programming team CMP 2024


ii.Default value of type parameters
They are parameters with default values

Template<class T=int> // default value for type parameter


class B
{
T mysequence[10]; …..
};
int main()
{
mysequence<float>integerobject; //float
mysequence<>defaultobject; // integer(default value given to T)
}

ii.Default value of non-type parameters


They are non-parameters with default values

Template<class T=int, int size=5> // default value for type parameter


// default value for non-type parameter
int main()
{ mysequence<double,10>iobject; //double,10 data user defined
mysequence<double>defaultobject // double, 5(as a default value)
}

9|Page Techniques of programming team CMP 2024


iii.Overriding class template
Simply it is defining a specialization of this template

For Example:
template<class T>
class mycontainer
{string name;};

➢ If you want in case that T is int to treat the class in a different


way ,then we will declare an int specialization

// Overriding class template


template<>
class myconatiner <int>
{long serial;
Public:
Int getserial();
};

//main
Int main()
{
Myconatiner<float> floatcontainer;//
10 | P a g e Techniques of programming team CMP 2024
Myconainter<int>intconaiter;
Intcointer.getserial();
Floatcontainer.getserial(); //Synatx Error(since getserial() isn’t a
member function of class my container)
Return 0;}

Templates and inheritance

Case (1) Base is class template specialization:


Derived:
1)class template
template <class T>
Class Der<T>: public Base < int >

2)template class specialization


template <class T>
Class Der < float >: public Base < float>

3)Non template class


Class Der: public Base < int >

11 | P a g e Techniques of programming team CMP 2024


Case(2) base is non template class:
Derived:
1)template class
template <class T>
Class Der<T>: public Base

Case(3) Base class can be template

Templates and friends

*Class Template may declare may declare: (Either template or non-template)

• Global Function
void greater(int n)
{…..}
Template<class T>
class numbers
{
friend void greater(int n); // greater() has access to all data members //of class
numbers
}

NB: the global function maybe Template

• Member function of another class:


class A
{ void func1(int);}

12 | P a g e Techniques of programming team CMP 2024


template<class T>
class B
{friend void A::func1(int);} //func1 has access to Data members of //class A

NB: class A maybe Template

• Another class:
class A
{…}

template<class T>
class B
{friend class A;} //all member function of class A has access to data
members of class B

Templates and static members


Each specialization of class template has its own copy of static data members.

for example:
the integer version of template share the same static member and another
different copy of this static member is shared by the (float,double,char,etc…)
version and so on.

GOOD LUCK

13 | P a g e Techniques of programming team CMP 2024

You might also like