0% found this document useful (0 votes)
21 views15 pages

Lecture 11

The document provides an overview of templates in C++, highlighting their role in generic programming through function and class templates. It explains the syntax and usage of function templates, including examples of overloading and multiple parameters, as well as class templates with multiple data types. Additionally, it discusses non-type template arguments and provides examples of their implementation.
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
0% found this document useful (0 votes)
21 views15 pages

Lecture 11

The document provides an overview of templates in C++, highlighting their role in generic programming through function and class templates. It explains the syntax and usage of function templates, including examples of overloading and multiple parameters, as well as class templates with multiple data types. Additionally, it discusses non-type template arguments and provides examples of their implementation.
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/ 15

Object Oriented programming using C++

Lecture - 10

Priya Pankaj Kumar


Assistant Professor-CSE
Sershah Engineering College, Sasaram
Template in C++
 A C++ template is a powerful feature added
to C++. It allows you to define the generic
classes and generic functions and thus
provides support for generic programming.
 Generic programming is a technique where
generic types are used as parameters in
algorithms so that they can work for a
variety of data types.
Representation of Template
 Templates can be represented in two ways:
 Function templates
 Class templates .
 Function Templates: We can define a template
for a function. For example, if we have an add()
function, we can create versions of the add
function for adding the int, float or double type
values.
 Class Template: We can define a template for a
class. For example, a class template can be
created for the array class that can accept the
array of various types such as int array, float
array or double array.
Syntax of Function Template
template < class Ttype> ret_type func_name (parameter_list)
{
// body of function
}
Where
Ttype: It is a placeholder name for a data type used by
the function.
It is used within the function definition. It is only a
placeholder that the compiler will automatically
replace this placeholder with the actual data type.
class: A class keyword is used to specify a generic
type in a template declaration.
Example Function Template
#include <iostream>
int main()
using namespace std;
{
template<class T> T add(T &a,T &b) int i =2;
{ int j =3;
T result = a+b; float m = 2.3;
return result; float n = 1.2;
cout<<add(i,j)<<endl;
}
cout<<add(m,n);
return 0;
}
Function Templates with Multiple Parameters

 We can use more than one generic type in the template function
by using the comma to separate the list.
Syntax
template<class T1, class T2,.....>return_type function_name
(arguments of type T1, T2....)
{
// body of function.
}
Note : In the above syntax, we have seen that the template
function can accept any number of arguments of a different
type.
Example

#include <iostream>
using namespace std;
template<class X,class Y> void fun(X a,Y b)
{
cout << "Value of a is : " <<a<< endl;
cout << "Value of b is : " <<b<< endl;
}
int main()
{
fun(15,12.3); r
return 0;
}
Overloading a Function Template

 We can overload the generic function means that the


overloaded template functions can differ in the parameter list.

#include <iostream>
using namespace std; int main()
template<class X> void fun(X a) {
{
fun(10);
cout << "Value of a is : " <<a<<endl;
} fun(20,30.5);
return 0;
template<class X,class Y> void fun(X b ,Y c)
}
{
cout << "Value of b is : " <<b<< endl;
cout << "Value of c is : " <<c<< endl;
}
Restrictions of Generic Functions

 Generic functions perform the same operation for all the versions of
a function except the data type differs. Let's see a simple example of
an overloaded function which cannot be replaced by the generic
function as both the functions have different functionalities.
#include <iostream>
void fun(double a)
using namespace std;
{
void fun(int b)
cout<<"value of a is : "<<a<<'\n';
{
}
if(b%2==0)
int main()
{
{
cout<<"Number is even";
fun(4.6);
}
fun(6);
else
return 0;
{
}
cout<<"Number is odd";
}
}
CLASS TEMPLATE
 Class Template can also be defined similarly to the Function Template. When
a class uses the concept of Template, then the class is known as generic class.
Syntax
template<class Ttype>
class class_name
{

}
 Ttype is a placeholder name which will be determined when the class is
instantiated.
 We can define more than one generic data type using a comma-separated
list.
 The Ttype can be used inside the class body.
CLASS TEMPLATE EXAMPLE
#include <iostream>
using namespace std;
template<class T> int main()
class A {
{ A<int> d;
public: d.add();
T num1 = 5; return 0;
T num2 = 6;
void add() }
{
cout << num1+num2<<endl;
}
};
CLASS TEMPLATE WITH MULTIPLE PARAMETERS

 We can use more than one generic data type in a class


template, and each generic data type is separated by
the comma.

Syntax
template<class T1, class T2, ......>
class class_name
{
// body of Class
}
Example
#include <iostream>
using namespace std;
template<class T1, class T2> void display()
class A {
{
cout << a<<" ,"<<b << endl ;
T1 a;
T2 b;
}
public: int main()
A(T1 x,T2 y) {
{ A<int,float> d(5,6.5);
a = x; d.display();
b = y; return 0;
} }
};
Nontype Template Arguments
 The template can contain multiple arguments, and we can also use the non-
type arguments(basic/derived data types) In addition to the type T argument.
 we can also use other types of arguments such as strings, function names,
constant expression and built-in types.
template<class T, int size>
class array
{
T arr[size]; // automatic array initialization.
};
 In the above case, the nontype template argument is size and
therefore, template supplies the size of the array as an
argument.
 Arguments are specified when the objects of a class are created:
1. array<int, 15> t1; // array of 15 integers.
2. array<float, 10> t2; // array of 10 floats.
3. array<char, 4> t3; // array of 4 chars.
Example
#include <iostream>
using namespace std;
template<class T, int size>
class A
{
public:
int main()
T arr[size]; {
void insert()
{ A<int,10> t1;
int i =1;
for (int j=0;j<size;j++) t1.insert();
{ t1.display();
arr[j] = i;
i++; return 0;
}
} }
void display()
{
for(int i=0;i<size;i++)
{
cout << arr[i] << " ";
}
}
};

You might also like