0% found this document useful (0 votes)
5 views19 pages

18 Templates

The document explains the concepts of function and class templates in programming, highlighting their role in achieving genericity. It covers function overloading, the use of template parameters, and how templates can be defined for both functions and classes. Additionally, it illustrates examples of function templates with multiple arguments and class templates, emphasizing the flexibility and reusability they provide in code design.

Uploaded by

komal komal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views19 pages

18 Templates

The document explains the concepts of function and class templates in programming, highlighting their role in achieving genericity. It covers function overloading, the use of template parameters, and how templates can be defined for both functions and classes. Additionally, it illustrates examples of function templates with multiple arguments and class templates, emphasizing the flexibility and reusability they provide in code design.

Uploaded by

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

TEMPLATES

Templates
Function Templates
Class Templates

Templates - Genericity
Function Genericity
Function Overloading
Function Templates

Function Overloading

The name of a function can be overloaded provided no two


definitions of the function have the same signature.
int square(int x);
double square(double x);
Function Genericity
Return type or the void specifier is not a part of function signature,
so two functions with the same parameter types but with different
return types cannot be overloaded.

long square(int x);


int square(int x);

Return type of overloaded functions can be different but this


should not be the ONLY difference.
Function Genericity
Function Templates
void add(int first, int second) void add(double first, double second)
{ {
int result = first+second; double result = first+second;
cout<<“Sum :”<<result; cout<<“Sum :”<<result;
} }

void add( first, second)


{
_____ result = first+second;

cout<<“Sum :”<<result;

}
Function Genericity
Templates make it possible for functions to receive not only
data values via parameters but also to receive the type of data
via a parameter.

template <typename Item>


void add( Item first,Item second)
{
Item result=first+second;
cout<<“Sum : “<<result;

}
Function Genericity
template <typename Item>
Item abs(Item number)
{
if (number < 0)
return –number;
else
return number;

}
Function Templates with Multiple
Arguments
template <class atype>
int find(atype array[], atype value, int size)
{
for (int j=0;j<size;j++)
if(array[j]==value)
return j;
return -1;
}
Function Templates with Multiple
Arguments
int intArr[] = {1,2,3,4,5};
int intSearch = 5;

double doubleArr[] = {1.0,2.0,3.5,4.6,5.0};


double dbSearch = 5.5;

void main()
{
cout << find(intArr,intSearch,5);
cout << find(doubleArr,dbSearch,5);

}
Template Arguments must Match
int intArray[]={1,2,3,4,5};
float f1 = 5.0;
int value = find(intArray,f1,4);

Compiler expects all instances of atype to be


of the same type:

find(int [], int, int);

But it can not generate:

find(int [], float, int);


More Than One Template
Argument
template <class atype, class btype>

atype funct(atype *array, btype value, int


size)
{

}
Review – Function Templates
• Written by programmer once
• Defines a whole family of overloaded functions
• Begins with the template keyword
• Contains a template parameter list of formal type
and the parameters for the function template are
enclosed in angle brackets (<>)
• Formal type parameters
– Preceded by keyword typename or keyword class
– Placeholders for fundamental or user-defined types
Function Template – Example
Forms
• template<typename T> f() {
}

• template<class ElementType>
g(){
}
Class Genericity
• Class-template definitions are preceded by a
header
– Such as template< typename T >
• Type parameter T can be used as a data type
in member functions and data members
• Additional type parameters can be specified
using a comma-separated list – e.g.,
– template< typename T1, typename T2 >
Class Genericity
const int CAPACITY = 128;

template <typename _______ >


class Test
{
public:

private:
array[CAPACITY];
};
Class Genericity
const int CAPACITY = 128;
template <typename Element>
class Test
{
public:
private:
Element array[CAPACITY];
};
Class Genericity
Without
void Test::display() Template
{
for(int i=0;i<CAPACITY;i++)
cout<<array[i];
With
}
Template
template <typename Element>
void Test<Element> :: display()
{
for(int i=0;i<CAPACITY;i++)
cout<<array[i];
}
Class Genericity
template <typename Element>
void Test<Element>::assign(Element val)
{

for(int i=0;i<CAPACITY;i++)
array[i] = val;

}
Class Genericity
void main(void)
{
Test<int> s1;
Test<float> s2;
s1.assign(5);
s2.assign(2.5);
………
………
}

You might also like