0% found this document useful (0 votes)
15 views

Template

§ Templates allow functions and classes to work with different data types without code duplication. § A template defines a generic class or function that can accept multiple data types as parameters. When an object of a specific type is defined, the template code is substituted with the actual type. § Template parameters specify placeholder types that will be replaced by specific types when template code is compiled.

Uploaded by

d4n67jnkr5
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views

Template

§ Templates allow functions and classes to work with different data types without code duplication. § A template defines a generic class or function that can accept multiple data types as parameters. When an object of a specific type is defined, the template code is substituted with the actual type. § Template parameters specify placeholder types that will be replaced by specific types when template code is compiled.

Uploaded by

d4n67jnkr5
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 27

Template

Need of Templates
int add(int x, int y) float add(float x, float y)
{ {
return x+y; return x+y;
} }

I like C++ so
char add(char x,char y) double add(double x, double y)
{ {

much
return x+y; return x+y;
} }

I like Rupesh sir


We need a single function that will work for int, float, double
etc…
Templates
§ Templates concept enables us to define generic classes and
functions.
§ This allows a function or class to work on many different data
types without being rewritten for each one.
I like C++ so
muchTemplates
I like Rupesh sir
Function Class
Template Template
EXAMPLES

I like C++ so
much
I like Rupesh sir
EXAMPLE FUNCTION TEMPLATE

I like C++ so
much
I like Rupesh sir
EXAMPLE

I like C++ so
much
I like Rupesh sir
EXAMPLE

I like C++ so
much
I like Rupesh sir
EXAMPLE CLASS TEMPLATE

I like C++ so
much
I like Rupesh sir
EXAMPLE CLASS TEMPLATE

I like C++ so
much
I like Rupesh sir
TASK
§ WAP to find the largest number from two int and float number
using class template.

I like C++ so
much
I like Rupesh sir
Function Template
Syntax:
template<class Ttype>

Keyword Placeholder name for a


ISpecifies
likegeneric
C++ so
type in
datatype

much a Template

Itemplate<typename
like Rupesh sir
Ttype>
Templates
§ C++ templates are a powerful mechanism for code reuse, as
they enable the programmer to write code that behaves the
same for any data type.

§
I like C++ so
§ By template we can define generic classes and functions.
In simple terms, you can create a single function or a class to
much
work with different data types using templates.
§
I like Rupesh sir
It can be considered as a kind of macro. When an object of a
specific type is defined for actual use, the template definition
for that class is substituted with the required data type.
Function Template
§ Suppose you write a function printData:
void printData(int value){
cout<<"The value is "<<value;
}
§ Now if you want to print double values or string values, then
I like C++ so
you have to overload the function:
void printData(float value){

}
much
cout<<"The value is "<<value;

I likevalue
Rupesh
void printData(char *value) {
cout<<"The sir
is "<<*value;
}
§ To perform same operation with different data type, we have
to write same code multiple time.
Function Template (Cont…)
§ C++ provides templates to reduce this type of duplication of code.
template<typename T>
void printData(T value){
cout<<"The value is "<<value;
}

I like C++ so
§ We can now use printData for any data type. Here T is a
template parameter that identifies a type.
much
§ Then, anywhere in the function where T appears, it is
replaced with whatever type the function is instantiated.
int i=3;
I
float d=4.75;
like Rupesh sir
char *s="hello";
printData(i); // T is int
printData(d); // T is float
printData(s); // T is string
#include <iostream>
§ T is a template argument
using namespace std;
that accepts different data
template <typename T>
T Large(T n1, T n2) types
{ § typename is a keyword
return (n1 > n2) ? n1 : n2; § You can also use
} keyword class instead of
int main(){ typename
int i1, i2; float f1, f2; char c1, c2;
cout << "Enter two integers:\n";
cin >> i1 >> i2;
cout << Large(i1, i2) <<" is larger." << endl;
cout << "\nEnter two floating-point numbers:\n";
cin >> f1 >> f2;
cout << Large(f1, f2) <<" is larger." << endl;
cout << "\nEnter two characters:\n";
cin >> c1 >> c2;
cout << Large(c1, c2) << " has larger ASCII value.";
}
Class Template
§ Sometimes, you need a class implementation that is same for
all classes, only the data types used are different.
§ Normally, you would need to create a different class for each
data type OR create different member variables and functions
I like C++ so
within a single class.

much
I like Rupesh sir
Class Template
Syntax:
template<class Ttype>

Keyword Placeholder
ISpecifies
like generic
C++typeso
in
name for a
datatype

much a Template

I like Rupesh sir


Object of template class
The object of template class are created as follows
class name <data type> object name;

template<class Ttype> int main()


class sample
{ I like C++ so {
sample <int>s1;

much
Ttype a,b; sample <float>s2;
public: s1.getdata();
void getdata()
{ I like Rupesh sir
cin>>a>>b;
s1.sum();
s2.getdata();
s2.sum();
}
}
void sum();
};
template<class T1, class T2>
class Sample Class Template
{
T1 a; T2 b; Example
public:
Sample(T1 x,T2 y){
a=x;
b=y;
}
void disp(){
cout<<a<<b;
} § To create a class
}; template object,
int main(){ define the data type
Sample <int,float> S1(12,23.3); inside a < > at the time
Sample <char,int> S2('N',12); of object creation.
S1.disp(); § className<int> classObj;
S2.disp(); className<float>
} classObj;
TASK
1. Write a function template for finding the minimum value
contained in an array.
2. Write program to swap Number using Function Template.

I like C++ so
much
I like Rupesh sir
STL – Standard Template
Library

Stack Queue Vector

map
STL- Standard Template Library
§ The C++ STL (Standard Template Library) is a powerful set of
C++ template classes to provides general-purpose
templatized classes and functions that implement many
popular and commonly used algorithms and data structures
like vectors, lists, queues, and stacks.
§
I like C++ so
There are three core components of STL as follows:
much
1. Containers (an object to store data)
2. Algorithms (procedure to process data)
I like Rupesh sir
3. Iterators (pointer object to point elements in container)
STL- Containers
§ A container is an object the actually stores data.
§ The STL containers can be implemented by class templates
to hold different data types.

I like C++ soContainers

much e
Sequence
Associativ
Derived
• vector • set • stack
• deque
• list
I like Rupesh sir


multiset
map
• queue
• Priority-
• multimap queue

Store elements in Direct access to Derived from


linear sequence elements using keys sequence containers
STL Algorithms
§ It is a procedure that is used to process data contained in
containers.
§ It includes algorithms that are used for initializing, searching,
copying, sorting and merging.
I like C++ so
§ Mutating Sequence Algorithms

§
much
like copy(), remove(), replace(), fill(), swap(), etc.,
Non Modifying sequence Algorithms
I like
like find(), Rupesh
count(),search(), sir and equal()
mismatch(),
§ Numerical Algorithms
accumulate(), partial_sum(), inner_product(), and
adjacent_difference()
STL- Algorithms
§ STL provide number of algorithms that can be used of any
container, irrespective of their type. Algorithms library contains
built in functions that performs complex algorithms on the data
structures.

I like C++ so
§ For example: one can reverse a range with reverse() function,
sort a range with sort() function, search in a range with
much
binary_search() and so on.
Algorithm library provides abstraction, i.e you don't
§
I like
necessarily need Rupesh
to know sir works.
how the the algorithm
STL- Iterations
§ Iterators behave like pointers.
§ Iterators are used to access container elements.
§ They are used to traverse from one element to another.

I like C++ so
much
I like Rupesh sir
STL components
§ STL provides numerous containers and algorithms which
are very useful in completive programming , for example you
can very easily define a linked list in a single statement by
using list container of container library in STL , saving your
time and effort.
§
I like C++ so
STL is a generic library , i.e a same container or algorithm
much
can be operated on any data types , you don’t have to define
the same algorithm for different type of elements.
§ For exampleI, sort
like Rupesh
algorithm will sort thesir
elements in the given
range irrespective of their data type , we don’t have to
implement different sort algorithm for different datatypes.

You might also like