0% found this document useful (0 votes)
21 views21 pages

Lecture 27 Templates

this is it

Uploaded by

aaggarwal5be21
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)
21 views21 pages

Lecture 27 Templates

this is it

Uploaded by

aaggarwal5be21
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/ 21

Generic Programming

Templates

By :
Dr. Rinkle Rani
Associate Professor, CSED
TIET, Patiala
1
Need of template
• Example Program- To find the greater of two numbers.

2
Need of template - motivation
• To reduce code duplication when supporting numerous data types
• Template make your function or class generalize as far as data type is
concerned.

• Template Definition- A template defines a general set of operations


that will be applied to various types of data.

3
We can write a generic code for We can write a generic code for a
a function e.g. add() for class, to manipulate group of
integers, double, float etc. member variables & functions e.g.
linked list of strings, integers etc.
4
Function Templates(Generics) -
definition
• We write a generic function that can be used for different data types. Syntax of an example
function -

template <class T>


T myMax(T x, T y)
{
if (x>y)
return x;
else
return y;}

• Other examples could be sort(), max(), min(), printArray(), show() etc.


5
Function template – an example
#include <iostream> int main(){
using namespace std; cout<<myMax(10,20)<<endl;
template <class T> cout<<myMax('a','z')<<endl;
T myMax(T x, T y) cout<<myMax(-2.5,7.7)<<endl;
{ }
if (x>y)
return x;
else
return y;
} 6
Function template – example 2
template<class X>
void show(X a, X b)
{
cout<<a<<endl;
cout<<b<<endl;
}
int main()
{
show(10,20);
show(30.2, 40.6);
show(‘a’, ‘c’);
} 7
Exercise – Make the generic
template of -

void bubbleSort(int a[], int n) {


for (int i = 0; i < n - 1; i++)
for (int j = n - 1; i < j; j--)
if (a[j] < a[j - 1])
swap(a[j], a[j - 1]);
}
8
Answer:
template <class T>
void bubbleSort(T a[], int n) {
for (int i = 0; i < n - 1; i++)
for (int j = n - 1; i < j; j--)
if (a[j] < a[j - 1])
swap(a[j], a[j - 1]);
}
9
Function templates with multiple
parameters
Exercise - Write a template for :
int main(){
show(100,"hello hello");
show('k',1500);
show(1.23,2987);
}

10
Answer:
template <class T1, class T2>
void show(T1 a, T2 b){
cout<<a<<“, “<<b<<endl;
}

11
Class Templates

12
Class templates
• Class templates are used when a class uses logic that can be generalized.
• The compiler will automatically generate the correct type of object, based
upon the type you specify when object is created.
• Syntax:
template <class Placeholder> class class-name
{
…….
}
• Once you created class template, object can be generated
using the following form:
Class-name<datatype> ob; 13
// Template class example
template <class T> int main(){
class Test { Test <int> t1(50,60);
T a; Test <double> t2(-10.20,
30.4);
T b;
Test <char> t3(‘a’, ‘c’)
public: t1.show();
Test (T x, T y) {a=x; b=y; } t2.show();
void show() t3.show();
{cout<<a<<endl; }
cout<<b<<endl;}
};
14
Defining function outside the template class
template <class T>
class Test {
T a;
T b;
public:
Test (T x, T y) {a=x; b=y; }
void show();
};
template <class T>
void Test<T> :: show()
{cout<<a<<endl;
cout<<b<<endl;}
} 15
Class template with multiple parameters
#include <iostream>
using namespace std; int main()
template <class X, class Y> {
class myclass
myclass<int, double> ob1(10, 0.23);
{
X i; myclass<char, int> ob2('X', 30);
Y j; ob1.show(); // show int, double
public: ob2.show(); // show char, int
myclass(X a, Y b) { i = a; j = b; }
return 0;
void show() { cout << i << ' ' << j << '\
n'; } }
}; 16
Template Specialization
in C++

17
Template Specialization
• What if we want a different code for a particular data type?

• Consider a big project that needs a function sort() for arrays of many
different data types.
• Let Quick Sort be used for all data types except char.
• In case of char, total possible values are 256 and counting sort may be
a better option.
• Is it possible to use different code only when sort() is called for char
data type?

18
• Solution: Template Specialization
• In C++, if you want to get a special behavior for a particular data type.
This is called template specialization.

19
Example
#include <iostream> int main()
using namespace std;
{
template <class T>
fun<char>('a');
void fun(T a)
{ fun<int>(10);
cout << "The main template fun(): " << a << endl; fun<float>(10.14);
} }
template<>
void fun(int x)
{
cout << "Specialized Template for int type: "
<< x << endl;
} 20
Output
• The main template fun(): a
• Specialized Template for int type: 10
• The main template fun(): 10.14

21

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