The document discusses templates in C++. It explains that templates allow functions and classes to work with different data types using a single code definition. Template functions are called function templates, and template classes are called class templates. The document provides examples of defining class and function templates, and overloading template functions. It demonstrates how templates can be used to create generic functions that operate on multiple types of data.
2. Introduction
• A function that works for all C++ data types is
called as generic function.
• Templates help the programmer to declare group
of functions or classes.
• When used with functions they are called as
function templates.
• For example, We can create template for function
square(), it calculate square of int, float, double
and long.
• Templates associated with class are called as class
templates.
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
3. Need of template
• It is a technique that allows using single functions or
class to work with different data types.
• Using template we can create a single function
that can be process any type of data.
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
4. Definition Of Class Template
• Template declaration
template <class T>
class name_of_class
{
//class data member and function
• Line 1 tells the compiler that the following class
declaration can use the template data type.
• T is the variable of template type.
• < > (angle bracket) is used to declare variables of
template type, one or variables are declared
separated by comma.
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
}
5. Without Class Template
#include<iostream>
using namespace std;
class data
{
public:
data(char c)
{
cout << "c = " << c <<endl;
}
data(int c)
{
cout << "c = " << c <<endl;
}
data(double c)
{
cout << "c = " << c <<endl;
}
};
int main()
{
data h('A');
data i(100);
data j(68.2);
return 0;
}
6. Using Class Template
#include<iostream>
using namespace std;
template <class T>
class data
{
public:
data(T c)
{
cout << "c = " << c <<endl;
}
};
int main()
{
data <char> h('A');
data <int> i(100);
data <float> j(68.2);
return 0;
}
7. Class Template with more arguments
#include<iostream>
using namespace std;
template <class T1, class T2>
class data
{
public:
data(T1 a, T2 b)
{
cout << "a = " << a <<" || b = " << b <<endl;
}
};
int main()
{
data <char, int> h('A',20);
data <int, float> i(100, 23.3);
data <float, char> j(68.2, 'D');
return 0;
}
Output:
a = A || b = 20
a = 100 || b = 23.3
a = 68.2 || b = D
8. Definition Of Function Template
• Template declaration
template <class T>
name_of_function()
{
//code
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
}
9. Function Template
#include<iostream>
using namespace std;
template <class T>
void show (T x)
{
cout << "x = " << x << endl;
}
int main()
{
char c = 'A';
show(c);
int i = 65;
show(i);
return 0;
}
10. Function Template with more arguments
#include<iostream>
using namespace std;
template <class T>
void exchange (T &a, T &b)
{
T t = a;
a = b;
b = t;
}
int main()
{
int x = 5, y = 6;
cout << "Before exchanging: ";
cout << "x = " << x << " | " << "y = " << y <<endl;
exchange (x, y);
cout << "After exchanging: ";
cout << "x = " << x << " | " << "y = " << y <<endl;
return 0;
}
Output:
Before exchanging: x = 5 | y = 6
After exchanging: x = 6 | y = 5
11. Overloading of template functions
• It can be overloaded by normal function or
template function.
• The compiler follows the following rules:
– Searches for accurate match; if found it is
invoked.
– Searches for a template function through which
a function that can be invoked with accurate
match can be generated; if found it is invoked.
– Attempts normal overloading declaration for the
function.
– In case no match is found; an error will be
reported.
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
12. Overloading of template functions
#include<iostream>
using namespace std;
template <class T>
void show (T x)
{
cout << "Template variable x = " << x << endl;
}
void show (int f)
{
cout << "Integer variable f = " << f << endl;
}
int main()
{
show('C');
show(50);
show(50.25);
return 0;
}
Output:
Template variable x = C
Integer variable f = 50
Template variable x = 50.25
Editor's Notes
Be selective. You do not need to cover both research and education. It does not need to be a long list. You can put down just one opportunity that you are really excited about. Just identify what you think are the biggest opportunities for your department faculty. Strike a balance between “thinking big” and being realistic. One way to think would be to say that if you were the Dean, you would invest in these opportunities. Remember the goal is to have national level prominence and visibility where our peer group will recognize our activities and accomplishments. For example, the NSF ERC on Particle Science and Technology
As you go to the next slide, please bear in mind that there may well be very strong connections between this slide and the next on multi-disciplinary collaborations.
Be selective. You do not need to cover both research and education. It does not need to be a long list. You can put down just one opportunity that you are really excited about. Just identify what you think are the biggest opportunities for your department faculty. Strike a balance between “thinking big” and being realistic. One way to think would be to say that if you were the Dean, you would invest in these opportunities. Remember the goal is to have national level prominence and visibility where our peer group will recognize our activities and accomplishments. For example, the NSF ERC on Particle Science and Technology
As you go to the next slide, please bear in mind that there may well be very strong connections between this slide and the next on multi-disciplinary collaborations.
Be selective. You do not need to cover both research and education. It does not need to be a long list. You can put down just one opportunity that you are really excited about. Just identify what you think are the biggest opportunities for your department faculty. Strike a balance between “thinking big” and being realistic. One way to think would be to say that if you were the Dean, you would invest in these opportunities. Remember the goal is to have national level prominence and visibility where our peer group will recognize our activities and accomplishments. For example, the NSF ERC on Particle Science and Technology
As you go to the next slide, please bear in mind that there may well be very strong connections between this slide and the next on multi-disciplinary collaborations.
Be selective. You do not need to cover both research and education. It does not need to be a long list. You can put down just one opportunity that you are really excited about. Just identify what you think are the biggest opportunities for your department faculty. Strike a balance between “thinking big” and being realistic. One way to think would be to say that if you were the Dean, you would invest in these opportunities. Remember the goal is to have national level prominence and visibility where our peer group will recognize our activities and accomplishments. For example, the NSF ERC on Particle Science and Technology
As you go to the next slide, please bear in mind that there may well be very strong connections between this slide and the next on multi-disciplinary collaborations.
Be selective. You do not need to cover both research and education. It does not need to be a long list. You can put down just one opportunity that you are really excited about. Just identify what you think are the biggest opportunities for your department faculty. Strike a balance between “thinking big” and being realistic. One way to think would be to say that if you were the Dean, you would invest in these opportunities. Remember the goal is to have national level prominence and visibility where our peer group will recognize our activities and accomplishments. For example, the NSF ERC on Particle Science and Technology
As you go to the next slide, please bear in mind that there may well be very strong connections between this slide and the next on multi-disciplinary collaborations.
Be selective. You do not need to cover both research and education. It does not need to be a long list. You can put down just one opportunity that you are really excited about. Just identify what you think are the biggest opportunities for your department faculty. Strike a balance between “thinking big” and being realistic. One way to think would be to say that if you were the Dean, you would invest in these opportunities. Remember the goal is to have national level prominence and visibility where our peer group will recognize our activities and accomplishments. For example, the NSF ERC on Particle Science and Technology
As you go to the next slide, please bear in mind that there may well be very strong connections between this slide and the next on multi-disciplinary collaborations.
Be selective. You do not need to cover both research and education. It does not need to be a long list. You can put down just one opportunity that you are really excited about. Just identify what you think are the biggest opportunities for your department faculty. Strike a balance between “thinking big” and being realistic. One way to think would be to say that if you were the Dean, you would invest in these opportunities. Remember the goal is to have national level prominence and visibility where our peer group will recognize our activities and accomplishments. For example, the NSF ERC on Particle Science and Technology
As you go to the next slide, please bear in mind that there may well be very strong connections between this slide and the next on multi-disciplinary collaborations.
Be selective. You do not need to cover both research and education. It does not need to be a long list. You can put down just one opportunity that you are really excited about. Just identify what you think are the biggest opportunities for your department faculty. Strike a balance between “thinking big” and being realistic. One way to think would be to say that if you were the Dean, you would invest in these opportunities. Remember the goal is to have national level prominence and visibility where our peer group will recognize our activities and accomplishments. For example, the NSF ERC on Particle Science and Technology
As you go to the next slide, please bear in mind that there may well be very strong connections between this slide and the next on multi-disciplinary collaborations.
Be selective. You do not need to cover both research and education. It does not need to be a long list. You can put down just one opportunity that you are really excited about. Just identify what you think are the biggest opportunities for your department faculty. Strike a balance between “thinking big” and being realistic. One way to think would be to say that if you were the Dean, you would invest in these opportunities. Remember the goal is to have national level prominence and visibility where our peer group will recognize our activities and accomplishments. For example, the NSF ERC on Particle Science and Technology
As you go to the next slide, please bear in mind that there may well be very strong connections between this slide and the next on multi-disciplinary collaborations.
Be selective. You do not need to cover both research and education. It does not need to be a long list. You can put down just one opportunity that you are really excited about. Just identify what you think are the biggest opportunities for your department faculty. Strike a balance between “thinking big” and being realistic. One way to think would be to say that if you were the Dean, you would invest in these opportunities. Remember the goal is to have national level prominence and visibility where our peer group will recognize our activities and accomplishments. For example, the NSF ERC on Particle Science and Technology
As you go to the next slide, please bear in mind that there may well be very strong connections between this slide and the next on multi-disciplinary collaborations.
Be selective. You do not need to cover both research and education. It does not need to be a long list. You can put down just one opportunity that you are really excited about. Just identify what you think are the biggest opportunities for your department faculty. Strike a balance between “thinking big” and being realistic. One way to think would be to say that if you were the Dean, you would invest in these opportunities. Remember the goal is to have national level prominence and visibility where our peer group will recognize our activities and accomplishments. For example, the NSF ERC on Particle Science and Technology
As you go to the next slide, please bear in mind that there may well be very strong connections between this slide and the next on multi-disciplinary collaborations.
Be selective. You do not need to cover both research and education. It does not need to be a long list. You can put down just one opportunity that you are really excited about. Just identify what you think are the biggest opportunities for your department faculty. Strike a balance between “thinking big” and being realistic. One way to think would be to say that if you were the Dean, you would invest in these opportunities. Remember the goal is to have national level prominence and visibility where our peer group will recognize our activities and accomplishments. For example, the NSF ERC on Particle Science and Technology
As you go to the next slide, please bear in mind that there may well be very strong connections between this slide and the next on multi-disciplinary collaborations.