Unit V Template

Download as pdf or txt
Download as pdf or txt
You are on page 1of 33

Sinhgad College of Engineering

Vadgaon Bk. Pune


Approved by AICTE New Delhi Recognized by Govt. of Maharashtra
Affiliated to Savitribai Phule Pune University
Accredited by NAAC with A+ Grade

Subject:- 210243: Object Oriented Programming.


Unit-V Template.
Mr. AJIT M. KARANJKAR
Assistant. Professor.
Department of Computer Engineering,
Sinhgad College Of Engineering Vadgaon Bk, Pune-041
Email:- amkaranjkar.scoe@sinhgad.edu

22-10-2024 1
Contents:
❑Template

Templates: The power of templates, Function Template, Overloading function


templates, and class template, and nontype parameters, Template and friends
generic function, The type name and export keywords.

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.

• Template is best example of generic programming in c++. In generic


programming we are writing the algorithms which are applicable to variety of
data types.
• In traditional programming language We need to use different algorithms for
different data types.

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

• Template are newly added features in C++.

• It allows us to define generic classes and functions.

• Templates are useful in achieving dynamic polymorphism.

• Template is way of creating generalize functions and classes which are


applicable for all data types without changing the definition of the program.

• They are used for crating family of functions and classes.

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

Finding Largest number from float:


How many elements want to enter: 5
Enter int elements: 5.5 4.5 3.5 2.5 1.2
06-10-2021
UNIT-V Template
13
Department Of Computer Engineering
Largest number is 5.5
7.3 Function Template
• General syntax for creating function template is as follow.
template < typename T>
return_typefunction_name(T type argument)
{
// ………
// body of function
//with Type T
//wherever appropriate
//……..
}
The function template syntax is similar to that of the class template except that we
are defining functions instead of classes

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:

Example of function template:


integer comparison.
First number is greater

Example of function template:


Floating point comparison.
Second number is greater

UNIT-V Template
06-10-2021 17
Department Of Computer Engineering
7.4 Overloading Function Template

• Template Function can be overloaded.

• 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:

Template <parameter-List>class declaration

• Class Declaration : The class name declared becomes a template name.

• 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.

• Template can have many arguments.

• It is possible to use nontype arguments also.

• 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
{
. .
}

Now, we create an instance of a class


class_name<type> ob;

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.

• Inside a declaration or definition of a template , typename can be used to declare that


dependent qualified name is a type.

• Inside a requirements for type requirements.

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

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy