Unit V Template
Unit V Template
Unit V Template
22-10-2024 1
Contents:
❑Template
UNIT-III Polymorphism
06-10-2021 2
Department Of Computer Engineering
7.Template
UNIT-V Template
06-10-2021 3
Department Of Computer Engineering
7.1 Introduction to Generic Programming
7.1 Introduction to Generic Programming.
• It is type of computer programming . In this type of programming we are
designing the algorithm , where in the type of data is specified at run time.
• We are writing the generalized code and compiler decides for what type of data it
should work.
UNIT-V Template
06-10-2021 4
Department Of Computer Engineering
7.1 Introduction to Generic Programming
Example: Write a program to handle addition of two number using
Template.
#include<iostream>
using namespace std;
int main()
{
cout<<"\n Addition of two Number";
int a=10, b=20;
int c;
c=a+b;
cout<<"\n Addition of two number is:"<<c;
c=a-b;
cout<<"\n Substraction of two number is:"<<c;
return 0;
}
UNIT-V Template
06-10-2021 5
Department Of Computer Engineering
7.1 Introduction to Generic Programming
Output:
Addition of two Number
Addition of two number is: 30
Substraction of two number is:-10
UNIT-V Template
06-10-2021 6
Department Of Computer Engineering
7.1 Introduction to Generic Programming
Example: (Floating point version)
#include<iostream>
using namespace std;
int main()
{
cout<<"\n Addition of two Number";
float a=10.5, b=20.3;
float c;
c=a+b;
cout<<"\n Addition of two number is:"<<c;
c=a-b;
cout<<"\n Substraction of two number is:"<<c;
return 0;
}
UNIT-V Template
06-10-2021 7
Department Of Computer Engineering
7.1 Introduction to Generic Programming
Output:
Addition of two Number
Addition of two number is: 30.8
Substraction of two number is:-9.8
UNIT-V Template
06-10-2021 8
Department Of Computer Engineering
7.1 Introduction to Generic Programming
Example: Using Template.
#include<iostream>
using namespace std;
template<typename T>
void add(T a, T b)
{
cout<<"\n Addition of two number is:"<<a+b;
}
template<typename T>
void sub(T a, T b)
{ cout<<"\n Substraction of two number is:"<<a-b;
}
int main()
{ cout<<"\n Example of generic program";
//integer operations
add<int>(10,20);
sub<int>(50,20);
UNIT-V Template
06-10-2021 9
Department Of Computer Engineering
7.1 Introduction to Generic Programming
//floating point operations
add<float>(10.4,30.6);
sub<float>(30.4,60.6);
return 0;
}
Output:
Addition of two number is: 30
Substraction of two number is: 30
Addition of two number is: 41
Substraction of two number is:-30.2
UNIT-V Template
06-10-2021 10
Department Of Computer Engineering
7.2 Power of Template
UNIT-V Template
06-10-2021 11
Department Of Computer Engineering
7.2 Power of Template
Example: Write a function template for finding the minimum value contained in an array.
#include<iostream>
using namespace std;
template<typename T>
void MAX(T a[], int n)
{
T m;
m=a[0];
for (int i=0;i<n;i++)
if(m<a[i])
m=a[i];
cout<<"\n Largest number is"<<m;
}
int main()
{
int n,i;
int a[10];
cout<<"\n Finding Largest number from int";
cout<<"\n How many elements want to enter";
cin>>n;
cout<<"\n Enter int elements:";
for (i=0;i<n;i++)
cin>>a[i];
MAX<int>(a,n);
UNIT-V Template
06-10-2021 12
Department Of Computer Engineering
7.2 Power of Template
float b[10];
cout<<"\n Finding Largest number from float";
cout<<"\n How many elements want to enter";
cin>>n;
cout<<"\n Enter float elements:";
for (i=0;i<n;i++)
cin>>b[i];
MAX<float>(b,n);
return 0;
}
Output:
Finding Largest number from int:
How many elements want to enter: 5
Enter int elements: 5 4 3 2 1
Largest number is 5
UNIT-V Template
06-10-2021 14
Department Of Computer Engineering
7.3 Function Template
• We can pass more than one argument to function template also.
• Templates cannot take varying numbers of arguments .But function Overloading
can take varying number of arguments.
• The following example declares a swap() function template that will swap two
values of a given type of data.
template<class T>
void swap(T &x, T&y)
{
T temp=x;
x=y;
y=temp;
}
UNIT-V Template
06-10-2021 15
Department Of Computer Engineering
7.3 Function Template
Example: Write a function template for finding the minimum value contained in an array.
#include<iostream>
using namespace std;
template<typename T>
void MAX(T a, T b)
{
if(a>b)
cout<<"\n First number is greater ";
else
cout<<"\n Second number is greater";
}
int main()
{
cout<<"\n Example of function template:";
cout<<"\n integer comparision";
MAX<int>(110,20);
cout<<"\n Example of function template:";
cout<<"\n Floating point comparision";
MAX<float>(-55.5,50.5);
return 0;
}
UNIT-V Template
06-10-2021 16
Department Of Computer Engineering
7.3 Function Template
Output:
UNIT-V Template
06-10-2021 17
Department Of Computer Engineering
7.4 Overloading Function Template
• Overloading a function template either by a non template function or by another template function
.
• if you can call the name of an overloaded function template the compiler will try to reduce its
template arguments and check its explicitly declared template arguments.
• If successful it will instantiate a function template specialization then add this specialization
to the set of candidate function used in overload resolution.
UNIT-V Template
06-10-2021 18
Department Of Computer Engineering
7.4 Overloading Function Template
Example:
#include<iostream>
using namespace std;
template <class T>
void f (T x, T y)
{
cout<<“Template:”<<x<<y<<endl;
}
void f (int w, int z)
{
cout<<“ Non-Template:”<<w<<z<<endl;
}
int main()
{
f(1,2);
f(‘a’,’b’);
f(1,7);
return 0;
} 06-10-2021 UNIT-V Template
19
Department Of Computer Engineering
7.4 Overloading Function Template
Output:
Non-Template
Template.
Non-Template
UNIT-V Template
06-10-2021 20
Department Of Computer Engineering
7.5 Overloading Class Template
• A class Template defines a family of classes. The syntax for the class template is given as follows.
Syntax:
• Parameter-List: non empty comma –separated list of the template parameter , each of which either a
non type parameter.
UNIT-V Template
06-10-2021 21
Department Of Computer Engineering
7.6 Class Template & Non-Type Parameter
Type template parameters and non-type template parameters.
• We can use also other arguments such as strings function names, constant expressions and built in
types. Consider the following example.
UNIT-V Template
06-10-2021 22
Department Of Computer Engineering
7.6 Class Template & Non-Type Parameter
• This template the size of the array as an argument. This implies the size of the array is known to
the compiler at the compile time itself.
• Example:
#include<iostream>
using namespace std;
template <class T, int MAX>
class example
{
T arr[MAX];
public:
void accept( )
{ 06-10-2021 UNIT-V Template
23
Department Of Computer Engineering
7.6 Class Template & Non-Type Parameter
cout<<“\n Enter “<<MAX<<“elements:”;
for (int i=0;i<MAX;i++)
cin>>arr[i];
}
void display()
{
cout<<“\n Content of array are”;
for (int i=0;i<MAX;i++)
cout<<“\t”<<arr[i];
}
};
int main()
{
example<float ,5> f;
f.accept();
f.display();
return 0;
}
UNIT-V Template
06-10-2021 24
Department Of Computer Engineering
7.6 Class Template & Non-Type Parameter
Output:
Enter 5 Elements:
10.5
20.6
30.7
40.8
50.9
Contents of array are: 10.5 20.6 30.7 40.8 50.9
UNIT-V Template
06-10-2021 25
Department Of Computer Engineering
7.7 Template & Friend Generic Functions
• When writing c++, there are some common situations where you may want to create a friend
function such as implementing operator<<.
Function Templates:
We can define a template for a function. For example, if we have an add() function, we
can create versions of the add function for adding the int, float or double type values.
UNIT-V Template
06-10-2021 26
Department Of Computer Engineering
7.7 Template & Friend Generic Functions
Class Template:
We can define a template for a class. For example, a class template can be created for the
array class that can accept the array of various types such as int array, float array or
double array.
Function Template
• Generic functions use the concept of a function template. Generic functions define a set
of operations that can be applied to the various types of data.
• The type of the data that the function will operate on depends on the type of the data
passed as a parameter.
UNIT-V Template
06-10-2021 27
Department Of Computer Engineering
7.7 Template & Friend Generic Functions
Syntax of Function Template
template < class Ttype> ret_type func_name(parameter_list)
{
// body of function.
}
Example:
#include <iostream>
using namespace std;
template<class T> T add(T &a,T &b)
{
T result = a+b;
return result;
}
int main()
{
UNIT-V Template
int06-10-2021
i =2; Department Of Computer Engineering
28
7.7 Template & Friend Generic Functions
int j =3;
float m = 2.3;
float n = 1.2;
cout<<"Addition of i and j is :"<<add(i,j);
cout<<'\n';
cout<<"Addition of m and n is :"<<add(m,n);
return 0;
}
Output:
Addition of i and j is :5
Addition of m and n is :3.5
UNIT-V Template
06-10-2021 29
Department Of Computer Engineering
7.7 Template & Friend Generic Functions
Class Template:
can also be defined similarly to the Function Template. When a class uses the concept of
Template, then the class is known as generic class.
Syntax
template<class Ttype>
class class_name
{
. .
}
UNIT-V Template
06-10-2021 30
Department Of Computer Engineering
7.7 Template & Friend Generic Functions
#include <iostream>
using namespace std;
template<class T>
class A
{ public:
T num1 = 5;
T num2 = 6;
void add()
{
std::cout << "Addition of num1 and num2 : " << num1+num2<<std::endl;
}
};
UNIT-V Template
06-10-2021 31
Department Of Computer Engineering
7.7 Template & Friend Generic Functions
int main()
{
A<int> d;
d.add();
return 0;
}
Output:
Addition of num1 and num2 :
UNIT-V Template
06-10-2021 32
Department Of Computer Engineering
7.7 The Typename and Export Keywords.
• In a template declaration, typename can be used as an alternative to class to declare type
template parameters.
Example:
template<typename T>
Const T&max( const T&x,const T&y)
{
if (y<x)
Return x;
Return y;
}; 06-10-2021
UNIT-V Template
33
Department Of Computer Engineering