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

Dept of EEE Object Oriented Programming

TEMPLATES

int maximum(int a, int b)


Here’s a small function that you might write to find the {
▰ maximum of if (a > b)
two integers. return a;
else
return b;
}

int maximum(Hoo a, Hoo b)


{
if (a > b)
int maximum(Hoo a, Hoo b) int maximum(double a, double b)
{
int maximum(Noo returna, a; Noo ifb)(a > b) {
{ else
if (a > b) return b;
int maximum(Hoo
return a; a, Hoo b) two double numbers if (a > b)
else {
} if (a > b)
else
return a; return b;
return a;
return a;
}
return b; else
int maximum(Koo a, Koo b) else
} { return b;
} if (a > b) int maximum(Poo a, Poo b) return b;
return a; {
int maximum(Poo a, Poo b) else if (a > b) }
{ int maximum(Looreturn b; a, Looreturnb) a;
if (a > b) } else
{

else
return a;
int maximum(Poo
if (a > b)
a, Poo
return }
intb)maximum(Loo
a;
return b;
a, Loo b) 10s and 100s of data types
int maximum(Knafn
return b;{ a, Knafnelseb) {
{} if (a > b) return b; if (a > b)
if (a > b) return} a; return a;
return a; else else
else return b; return b;
return b; } }
} 2

S.Muralidharan 1
Dept of EEE Object Oriented Programming

C++ TEMPLATE
• For creating generic programming.
• Are used for the creation of set of functions and classes can be used to access all
kinds of data type.
• When an object of specific type is defined the template definition is replaced with
the required data type.
FUNCTION
TEMPLATE CLASS
TEMPLATE
Are used to create Are used to create
family of functions with family of objects with
different data types different data types of
data members.
C++
TEMPLATE

FUNCTION
TEMPLATE

S.Muralidharan 2
Dept of EEE Object Oriented Programming

A Template Function
Class means
classification not CLASS
Called “Template
Syn::
Syn Prefix”
Template<class TDT
TDT>>
returntype functionname
functionname((TDT arg1,
arg1,TDT
TDT arg2)
{
fn body;
}

▰ TEMPLATE FUNCTION FOR FINDING MAXIMUM


template <class T>
T maximum(T a, T b) THIS WORKS OUT FOR
{
if (a > b) cout << maximum(1,2);
return a; cout << maximum(1.3, 0.9);
else
return b; ...
5
}

Function template to print the given input

#include <iostream>

using namespace std;


template<class T>
void print(T x)
{
cout<<x<<endl;
}

int main()
{
print(10);
print(3.14f);
print('Y');
print("welcome");
return 0;
}
6

S.Muralidharan 3
Dept of EEE Object Oriented Programming

Another example

Difference between Template Function and Normal Function

S.Muralidharan 4
Dept of EEE Object Oriented Programming

Function template with MULTIPLE parameters

10

T2 Not Used

Missing
Template prefix

11

S.Muralidharan 5
Dept of EEE Object Oriented Programming

Function template to print MULTIPLE data types

#include <iostream>

using namespace std;


template<class T1, class T2 >
void print(T1 x,T2 y )
{
cout<<x<<y<<endl;
}

int main()
{
print(10,3.14);
print(3.14,55);
print('Y‘,100);
return 0;
}

12

Try by yourself..

▰ Write a function template to sort the given number of


i)Integer type ii)Float type

13

S.Muralidharan 6
Dept of EEE Object Oriented Programming

CLASS TEMPLATE

15

▰ Class templates
▻ Allow type-specific versions of generic classes

Format:
template< class T >
class ClassName
{
definition
}

To create an object of the class, type


ClassName< type > myObject;

Example: Stack< double > doubleStack;


16

S.Muralidharan 7
Dept of EEE Object Oriented Programming

Example
main()
template<class t> {
class num num<int> N1(10,20,30);
{ N1.display();
t a,b,c; Num<float> N2(3.14,5.17,6.25);
public: N2.display();
num(t x,t y,t z) }
{
a=x;
b=y;
c=z;
}
void display()
{
cout<<a<<“”<<b<<“ “<<c<<endl;
}
};

17

Another example
complex operator+(complex<T> oo)
{
complex<T> res;
#include <iostream> res.rl=rl+oo.rl;
using namespace std; res.img=img+oo.img;
template<class T> return res;
class complex }
{ T rl,img; void display()
public: {
complex(){} cout<<rl<<"+i"<<img<<endl;
complex(T a,T b) }};
{ rl=a;
img=b; int main()
} {
complex add(complex<T> oo) complex<int> C1(10,20);
{ complex<int> C2(30,40);
complex<T> res; complex<int> C3;
res.rl=rl+oo.rl; // C3=C1.add(C2);
res.img=img+oo.img; C3=C1+C2;
return res; C3.display();
} return 0;
18
}

S.Muralidharan 8
Dept of EEE Object Oriented Programming

19

▰ When defining body of the constructor/function outside class definition

Constructor

Function

20

S.Muralidharan 9
Dept of EEE Object Oriented Programming

Comparison between two situations..

Without template With class template

21

Inheritance with class template

22

S.Muralidharan 10

You might also like