1
Submitted to :- Submitted By :-
Mr.Chetan Mali Muskan Soni
• Need of templates
• Introduction
• Function template
• Examples
• Class template
• Examples
• Advantages and disadvantages
2
Objectives
• In general if we want to add two integer no we create a function which receive the int values. If we want to
add two float no’s we have to create another function and for double values we again create another function.
• For different data types we create different-different functions for performing the task.
• This create more line of code.
• Program become complex.
3
Why Template is used?
4
Program :-
#include<iostream.h>
#include<conio.h>
void show(int a)
{
cout<<“n value of a=”<<a;
}
void show(float a)
{
cout<<“n value of a=”<<a;
}
void main()
{
int x=234;
float y=34.56f;
clrscr();
Output :-
value of a =234
value of a=34.56
show(x);
show(y);
getch();
}
 So can we use one function for all the datatypes?
ANSWER
is
TEMPLATES
5
• Template is a one of the most important and useful feature of C++.
• It provide the idea of generic classes.
• Use of one function or class that works for all data types is generalization.
• Template is a mechanism which allows us to declare generic classes and functions.
6
Template
• A template created for function so a function works for variety of data types is termed as
function template.
• For example a function template max is written which finds maximum of two integers, two
floats, two chars etc. similarly when a template is written for a class so one single class works
for variety of data types is termed as class templates.
7
Contd…
• Function template is created when we write one definition of function which works with
different type of data types.
• A function template does not occupies space in memory.
• When function is called with specific data type the actual definition of function template
is generated.
8
Function Template
 Syntax:-
template<class name>
function definition;
• Template is a keyword.
• In the brackets we write class and any name which serves as the generic type.
• The name may be a single character or word.
• The name usually in capital letter but may be in small case too.
9
Contd…
Displaying different types of variables with the help of single function
using function template.
10
Example
11
#include<iostream.h>
#include<conio.h>
#include<typeinfo.h>
template<class FUNC>
void show(FUNC par)
{
cout<<“Displaying ”<<typeid(par).name() <<“ t parameter t”<<par<<endl;
}
void main()
{
int x=234;
float y=34.56f;
double d=3.444456;
char ch=‘F’;
char *s= “Template”;
show(x);
show(y);
show(d);
show(ch);
show(s);
getch();
}
12
Continue..
13
Output
Displaying int parameter 234
Displaying float parameter 34.56
Displaying double parameter 3.44446
Displaying char parameter F
Displaying char * parameter Template
• The FUNC is generic data type name. It is replaced by the actual data type when a specific data type is
used with the function call.
• For 5 different data types with function template show five different versions of show are generated in
memory.
• Function template does not save memory.
14
Explanation
15
#include<iostream.h>
#include<conio.h>
#include<typeinfo.h>
template<class FUNC>
FUNC max(FUNC a, FUNC b)
{
if(a>b)
{
return(a);
}
else
{
return(b);
}
}
Example- To find maximum of two numbers using function template.
void main()
{
int x=10, y=20;
float f1=2, f2=4.5;
char ch=‘A’, ch1=‘B’;
cout<<“n Max of two integers ” << x << “ & ” <<y<< “ is t“;
cout<<max(x,y);
cout<<“n Max of two floats ” <<f1<< “ & “ <<f2<<“ is t”;
cout<<max (f1,f2);
cout<<“n Max of two characters ” <<ch<< “ & “ <<ch1<<“ is t”;
cout<<max (ch,ch1);
getch();
}
16
Continue....
17
Output
Max of two integers 10 & 20 is 20
Max of two floats 2.4 & 4.5 is 4.5
Max of two chars A & B is B
18
#include<iostream.h>
#include<conio.h>
#include<typeinfo.h>
template<class T1,class T2>
void show (T1 par1, T2 par2)
{
cout<<typeid(par1) .name()<<“ parameter=“<<par1<<“t”;
cout<<typeid(par2) .name()<<“ parameter=“<<par2<<“t”;
}
Example- Working with two generic data type at a time.
Cont...
void main()
{
clrscr();
show(12, ‘A’);
show(“hello",34.45);
getch();
}
20
Output
int parameter =12 char parameter=A
char * parameter=hello double parameter=34.45
• In the program we have a function template show which takes two generic types as
arguments.
• Many more generic types can be declared separating by comma but each must be declared
using class keyword.
21
Explanation
Checking equality of data type of two variables using function
template.
22
Example
23
#include<iostream.h>
#include<conio.h>
#include<typeinfo.h>
#include<string.h>
template<class T1, class T2>
void equal (T1 x, T2 y)
{
const char *p1=typeid(x).name() ;
const char *p2=typeid(y).name() ;
if(strcmp (p1,p2)==0)
cout<<Data type “<<p1<<“ and “ <<p2<<“ is same n”;
Cont..
else
cout<<Data type “<<p1<<“ and “ <<p2<<“ is not same n”;
}
void main()
{
int a=10,b=20;
float c=23.45;
char ch=‘A’;
Char *s1=“string”,*s2=“string”;
equal(a,c);
equal(ch,b);
equal(a,b);
equal(s1,s2);
getch();
}
Continue....
25
26
Output
Data type int and float is not same
Data type char and int is not same
Data type int and int is same
Data type char* and char* is same
27
#include<iostream.h>
#include<conio.h>
#include<typeinfo.h>
template <class T>
void show(T par,char *s)
{
cout<<“ ***** ”<< s <<“ ***** ”<<endl;
cout<<typeid(par) .name()<<“ parameter = ”<<par<<endl;
}
Example- Mixing built in type with generic type.
Cont..
void main()
{
show(12,”Good Morning”);
show(34.45, “Good Morning”);
getch();
}
29
Output
***** Good Morning *****
int parameter =12
***** Good Morning *****
double parameter =34.45
• In the function show the first argument of any type but second argument is fix which must be of char* type.
• We can fix any number of arguments in the function template.
• Fix argument must not be written in the template declaration.
• It must be written as function argument.
30
Explanation
31
#include<iostream.h>
#include<conio.h>
#include<typeinfo.h>
template <class T>
void show(T par,char *s=”Good Morning”)
{
cout<<“ ***** ”<< s <<“ ***** ”<<endl;
cout<<typeid(par) .name()<<“ parameter = ”<<par<<endl;
}
Example- Mixing built in type with generic type with default argument.
Cont...
void main()
{
show(12);
show(34.45);
show(123,"GoodAfter Noon");
getch();
}
33
Output
***** Good Morning *****
int parameter =12
***** Good Morning *****
double parameter =34.45
*****GoodAfter Noon*****
int parameter=123
• Template is created for class.
• The class template are used for creating container classes.
• By this it can contains varieties of objects of any type.
• Example
template <class T>
class demo
{
T x,y;
public:
……….;
};
34
Class Template
35
#include<iostream.h>
#include<conio.h>
#include<typeinfo.h>
template <class PAR>
class demo
{
PAR n1,n2;
public:
demo{}
demo(PAR x,PAR y)
{
n1=x;
n2=y;
show();
}
void show()
{
cout<<typeid(PAR) .name()<<“Data“;
cout<<n1<<“/n”<<n2<<endl;
}
};
void main()
{
demo<int>d1(10,20);
demo<float>d2(2.4f,4.5f);
demo<char>d3(‘P’,’T’);
demo<char*>d4(“One”,”Two”);
getch();
}
Example- Display of different types of data using class template.
36
Output
int Data
10 20
float Data
2.4 4.5
char Data
P T
char* Data
One Two
37
#include<iostream.h>
#include<conio.h>
#include<typeinfo.h>
template <class T1,class T2>
class demo
{
T1 n1;
T2 n2;
public:
demo{}
demo(T1 x,T2 y)
{
n1=x;
n2=y;
show();
}
void show()
{
cout<<n1<<" is of type"<<typeid(T1).name()<<"/n"<<endl;
cout<<n2<<"is of
type"<<typeid(T2).name()<<"/n"<<endl;
}
};
Example- Display of 2 different types of data using class template.
Cont..
void main()
{
demo<int,float>d1(10,20.54f);
demo<float,char>d2(2.4f,’R’);
demo<char,char*>d3(‘K’,”Boom”);
demo<char*,int>d4(“One”,2);
getch();
}
39
Output
10 is of type int
20.54 is of type float
2.4 is of type float
R is of type char
K is of type char
Boom is of type char*
One is of type char*
2 is of type int
• Templates are easier to write. We can create only one generic version of our class or function
instead of manually creating specializations.
• Templates can be easier to understand, since they can provide a straightforward way of abstracting
type information.
• Templates are typesafe. Because the types that templates act upon are known at compile time, the
compiler can perform type checking before errors occur.
• No need to declaration of any extra header file.
40
Advantages
• Some compilers exhibited poor support for templates. So, the use of templates could
decrease code portability.
• It can be difficult to debug code that is developed using templates. Since the compiler
replaces the templates, it becomes difficult for the debugger to locate the code.
41
Disadvantages
FUNCTION OVERLOADING
• If any class have multiple function with the same names but
different parameters then they are said to be overloaded.
• Function overloading allows we to use the same name for
different functions , to perform , either same or different functions
in the same or different functions in the same class .
 If we have to perform one single operation but with
different number or types of arguments ,then we can
simply overload the function.
 function overloading is an example of
polymorphism.
[ polymorphism-> Greek meaning “having multiple
forms” such as a function have more than one form.
WAY TO OVERLOAD A FUNCTION
• WHEN MORE THEN ONE FUNCTION NAME IS SAME THEN
THEY SHOULD BE DIFFER IN THE FOLLOWING
MANNER:-
1. By changing number of argument.
2. By changing order of argument.
3. By having different datatypes of arguments (return type
does not matter).
Number of argument(program)
#include<conio.h>
#include<iostream.h>
class sum
{
public:
void addition( int a , int b)
{
cout<<a+b;
}
void addition (int a , int b , int c)
{
cout<<a+b+c;
}
};
Number of argument continue
void main()
{
sum s1;
s1.addition(10,20);
s1.addition(30,40,30);
getch();
}
OUTPUT ( number of argument )
30
100
ORDER OF ARGUMENT
#include<iostream.h>
#include<conio.h>
class sum
{
public:
void addition(int a , float b)
{
cout<<a+b;
}
void addition(float a , int b)
{
cout<<a+b;
}
};
void main()
{
sum s1;
s1.addition(10,2.5f);
s1.addition(1.1f,5);
getch();
}
Order of argument continue
OUTPUT (order of argument)
12.5
6.1
DATA TYPE OF ARGUMENT
#include < iostream.h>
#include<conio.h>
class sum
{
public:
void addition(int a,int b)
{
cout<<a+b;
}
void addition(float a,float b)
{
cout<<a+b;
}
Data type of argument continue
void addition(double a,double b)
{
cout<<a+b;
}
};
void main()
{
clrscr();
sum s1;
s1.addition(10,20);
s1.addition (10.5f,3.5f);
s1.addition(10.05,1.20);
getch();
}
OUTPUT (data type of argument )
30
14
11.25
Thank you

TEMPLATES IN JAVA