100% found this document useful (1 vote)
20 views17 pages

Lect 15 Templates

This gives overall knowledge about Templates in OOP C++
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
20 views17 pages

Lect 15 Templates

This gives overall knowledge about Templates in OOP C++
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 17

OBJECT ORIENTED PROGRAMMING

INSTRUCTOR: SANNEYA AZIZ


TEMPLATE

o A template is a simple and yet very powerful tool in C++.


o The simple idea is to pass data type as a parameter so that we don’t
need to write the same code for different data types.
 Template specialization
o Template specialization allows us to have different code for a particular
data type. See Template Specialization for more details.
EXAMPLE

A software company may need sort() for different data types. Rather than
writing and maintaining the multiple codes, we can write one sort() and
pass data type as a parameter.
Keyword:
 Template
 Class
C++ adds two new keywords to support
templates: ‘template’ and ‘typename’. The second keyword can always be
replaced by keyword ‘class’.
HOW TEMPLATES WORK

 Templates are expanded at compiler time.


 The difference is, compiler does type checking before template
expansion.
 The idea is simple, source code contains only function/class, but compiled
code may contain multiple copies of same function/class.
HOW TEMPLATES WORK (FUNCTION TEMPLATE)
FUNCTION TEMPLATES

 We write a generic function that can be used for different data types.
 Examples of function templates are sort(), max(), min(), printArray().
output :
7
7
EXAMPLE g
#include <iostream> int main()
using namespace std; {
cout << myMax<int>(3, 7) <<
// One function works for all data types. This would work endl; // Call myMax for int
even for user defined types if operator '>' is overloaded
cout << myMax<double>(3.0, 7.0)
template <typename T> << endl; // call myMax for double
T myMax(T x, T y) cout << myMax<char>('g', 'e') <<
{ endl; // call myMax for char
return (x > y)? x: y; return 0;
} }
CLASS TEMPLATES

 Like function templates, class templates are useful when a class defines
something that is independent of the data type.
 Can be useful for classes like LinkedList, BinaryTree, Stack, Queue, Array,
etc.
EXAMPLE
#include <iostream> template <typename T>
using namespace std; Array<T>::Array(T arr[], int s) {
template <typename T> ptr = new T[s];
class Array { size = s;
private: for(int i = 0; i < size; i++)
T *ptr; ptr[i] = arr[i]; }
int size;
public:
Array(T arr[], int s);
void print();
};
EXAMPLE
template <typename T> int main()
void Array<T>::print() {
{ int arr[5] = {1, 2, 3, 4, 5};
for (int i = 0; i < size; i++) Array<int> a(arr, 5);
cout<<" "<<*(ptr + i); a.print();
cout<<endl; return 0;
} }

output :
12345
THINK

 Can there be more than one arguments to templates?


 Can we specify default value for template arguments?
 What happens when there is static member in a template class/function?
 What is the difference between function overloading and templates?
ANSWER

 Can there be more than one arguments to templates?

Yes, like normal parameters, we can pass more than one data types as
arguments to templates. The following example demonstrates the same.
output :
Constructor called
EXAMPLE Constructor called

#include<iostream> cout<<"Constructor
using namespace std; Called"<<endl;
}
template<class T, class U> };
class A
{ int main()
T x; {
U y; A<char, char> a;
public: A<int, double> b;
A() return 0;
{ }
ANSWER

 Can we specify default value for template arguments?

Yes, like normal parameters, we can specify default arguments to templates.


The following example demonstrates the same.
EXAMPLE

#include<iostream> Called"<<endl; }
using namespace std; };

template<class T, class U = char> int main() {


class A { A<char> a; // This will call A<char,
public: char>
T x; return 0;
U y; }
A() { cout<<"Constructor output :
Constructor called
ANSWER

 What happens when there is static member in a template


class/function?

Each instance of a template contains its own static variable. See Templates
and Static variables for more details.
ANSWER

 What is the difference between function overloading and


templates?
Both function overloading and templates are examples of polymorphism
feature of OOP. Function overloading is used when multiple functions do
similar operations, templates are used when multiple functions do identical
operations.

You might also like