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

Templates

Templates are essential in generic programming, allowing functions and classes to operate with any data type, enhancing code reusability and flexibility. There are two main types of templates: Function Templates and Class Templates, each with specific syntax and usage examples. While templates offer advantages like data type-free coding and powerful libraries, they also have drawbacks such as slower compile times and increased code generation.
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)
3 views16 pages

Templates

Templates are essential in generic programming, allowing functions and classes to operate with any data type, enhancing code reusability and flexibility. There are two main types of templates: Function Templates and Class Templates, each with specific syntax and usage examples. While templates offer advantages like data type-free coding and powerful libraries, they also have drawbacks such as slower compile times and increased code generation.
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/ 16

Templat

es
ClO-3, PLO2, C3
Templates

Templates are the fundamental parts of generic programming.


In generic programing the code is written without taking care of
the data types. It acts like a blueprint for creating a function or a
class. The same function or class will be able to deal with any of
the basic data types. Templates are used for the code
reusability and flexibility of the programs.

Templates are of two types:

• Function Templates
• Class Templates
Templates

Function
Templates
Function Templates

template <class type>


return_type function_name (list of arguments)
{
function_body ..
}
template: keyword return type: if
required
<class …. > keyword list of arguments if
required
or
<typename …> keyword
Function Templates
Output

x=5
template <class cse>
y = 10
void sqr(cse a)
The Square of 5
{
cse result; is 25
result= a * a; The Square of 10
cout << "\n The Square of " <<a is 100
<< " is " << result ; The Square of 0.2
} is 0.04
int main() The Square of 0.3
{
is 0.09
int x = 5, y = 10;
float m = 0.2, n = 0.3; void sqr(int a)
{
cout << "\nx = " << x << int result;
"\ny = " << y; result= a * a;
sqr(x); cout << "\n The Square of " <<a << " is
" << result ; }
sqr(y);
sqr(m);
sqr(n); }
Function Templates

template <class cse>


void display(cse a)
{
cout<<"\nThe function received: Output
"<<a;
}
The function received:
int main()
{ 335
int x = 50, y = 335; The function received:
float m = 0.2, n = 0.3;
0.3
char asad = 'R', n2 = 'T';
display(y); display(n);display(asad); The function received:
R
}
Function Templates
template <class cse>

void display (cse a, cse b)


{
cout<<"\nThe function received: "<<a << "
and " << b;
}
int main()
{
int x = 50, y = 335;
float m = 0.2, n = 0.3;
char asad = 'R', n2 = 'T';

display ( x , y );
display ( x , m );
display ( n , asad );
}
Function Templates

template <class cse, class cse2>

void display(cse a, cse2 b)


{
cout<<"\nThe function received: "<<a << Output
" and " << b;
}
int main() The function received: 50
{
int x = 50, y = 335; and 0.2
float m = 0.2, n = 0.3; The function received: 0.3
char asad = 'R', n2 = 'T';
and R
display( x , m );
display( n , asad );
}
Class Templates

template <class type>


class class_name
{
private:

…..

public:

… ..
};
Class Templates
template <class A>

class item { Output


private:
A weight;
A price;
public:
indata() { cin>>weight; cin>>price; } 11
showdata(){cout<<"weight " <<weight; cout<< " 22
price= \n" << price; }
}; weight 11 price= 22
main( ) { 33.3
item<int> book; 44.4
item<float> pen; weight 33.3 price=
book.indata(); 44.4
book.showdata();
pen.indata();
pen.showdata();
Class Templates
template <class A>

class item {
private:
A weight;
A price; Output
public:
indata() { cin>>weight; cin>>price; }
showdata(){cout<<"weight " <<weight; cout<< "
334.55
price= \n" << price; }
};
weight 334 price=0
main( ) {
item<int> book; weight 4.2039e-045 price=0
item<float> pen;
book.indata();
book.showdata();
pen.indata();
pen.showdata();
Class Templates
template <class a, class b>
class item
{ private:
a id;
b price;
public: Output
indata(a one, b two) { id= one; price=two; }
showdata(){cout<<"\n id = " <<id; cout<< "
price= " << price; }
}; id = 4 price= 44
id = 8.5 price=
main( )
{ 77.9
item <int, int>book; item <float, float>pen;

book.indata(4,44); book.showdata();

pen.indata(8.5,77.9); pen.showdata();

}
Class Templates
template <class a, class b>
class item
{ private:
a id;
b price;
public:
indata(a one, b two) { id= one; price=two; }
showdata(){cout<<"\n id = " <<id; cout<< " price=
" << price; }
}; Output
main( ) {
item <int, int>book; item <float, float>pen;
item <float, int>pencil; id = 4 price= 44
book.indata(4,44);
book.showdata(); id = 8.5 price= 77.9
pen.indata(8.5,77.9);
pen.showdata();
pencil.indata(1.11, 22); id = 1.11 price= 22
pencil.showdata();
}
Class Templates
template <class a, class b>
class item
{ private:
a id;
b price;
public: Output
indata(a one, b two) { id= one; price=two; }
showdata(){cout<<"\n id = " <<id; cout<< "
price= " << price; }
}; id = 4 price= 44

main( ) { id = 8.5 price= 77.9


item <int, int>book; item <float, float>pen;
item <float, int>pencil; id = 1.11 price= 22
book.indata(4,44); book.showdata();
pen.indata(8.5,77.9); pen.showdata();
pencil.indata(1.11, 22); pencil.showdata();
id = 83 price= 0
pencil.indata('S', 0.03);
pencil.showdata();
}
Templates

Analysis of Templates
- Comparison with function Overloading
- Comparison with Polymorphism

Advantages:
 It provides a data type free coding technique.
 Useful in generic programming.
 Good for code reuse.
 Useful to build powerful libraries.
 Greater modifiability
Templates

Disadvantages :

Comparatively slower compile time


Generates more code
 Support problem in few compilers reducing
portability
 Difficult to debug

You might also like