Object Oriented Programming (CS1143) : Department of Computer Science Capital University of Science and Technology (CUST)
Object Oriented Programming (CS1143) : Department of Computer Science Capital University of Science and Technology (CUST)
(CS1143)
Week 12
Function Templates
Class Templates
2
Template Programming
3
Outline
Function Templates
Class Templates
4
Function Template
5
Using a family of functions
6
7
8
Using Function Template
9
Example
10
Description
We have only one generic type, but the type is used three times:
twice as parameters and once as the return type.
In this program the type of the parameters and the returned value
are the same.
We have saved code by writing only one template function instead of
three overloaded functions.
11
Another Example
12
13
template <typename T>
T smaller (T first, T second)
{
...
}
Before Compilation
14
Overloading Function Templates
15
16
17
Outline
Function Templates
Class Templates
18
Class Template
19
Example
20
21
22
This is all for Week 12
23