0% found this document useful (0 votes)
11 views8 pages

Templates Notes

C++ templates enable generic programming by allowing the definition of generic classes and functions. Function templates allow operations on various data types, while class templates create generic classes that can handle multiple types. Both templates and function overloading are forms of polymorphism in C++, with templates being used for identical operations across different types.

Uploaded by

anjalishreya69
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views8 pages

Templates Notes

C++ templates enable generic programming by allowing the definition of generic classes and functions. Function templates allow operations on various data types, while class templates create generic classes that can handle multiple types. Both templates and function overloading are forms of polymorphism in C++, with templates being used for identical operations across different types.

Uploaded by

anjalishreya69
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

C++ Templates

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.

Templates can be represented in two ways:


1. Function templates
2. 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.

Function Template
Generic functions use the concept of a function template. Generic
functions define a set of operations that can be applied to the various
types of data.

The type of the data that the function will operate on depends on the
type of the data passed as a parameter.

For example, Quick sorting algorithm is implemented using a generic


function, it can be implemented to an array of integers or array of floats.

A Generic function is created by using the keyword template. The


template defines what function will do.

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
#include <iostream>
using namespace std;
template <typename T>
T myMax(T x, T y)
{
return (x > y)? x: y;
}
int main()
{
cout << myMax<int>(87, 76) << endl;
cout << myMax<double>(3.5, 5.5) << endl;
cout << myMax<char>('S', 'K') << endl;
return 0;
}

Output
87
5.5
S

Process returned 0 (0x0) execution time : 0.013 s


Press any key to continue.
Example2
A program to define a function template for swapping two
items of the various data types such as integer, and
floating point numbers.

#include <iostream>
using namespace std;
template<class T>
void Swap (T &first,T &second)
{
T temp;
temp = first;
first = second;
second = temp;
}
int main()
{
int ix,iy;
float fx,fy;
cout << "enter any two integers \n";
cin >> ix >> iy;
cout << "enter any two floating point numbers ? \n";
cin >> fx >> fy;
Swap (ix,iy);
cout << " after swapping integers \n";
cout << " ix = " << ix << " iy = " << iy << endl;
Swap (fx,fy);
cout <<" after swapping floating point numbers \n";
cout << " fx = " << fx << " fy = " << fy << endl;
return 0;
}

Output

enter any two integers


5
6
enter any two floating point numbers ?
2.5
4.5
after swapping integers
ix = 6 iy = 5
after swapping floating point numbers
fx = 4.5 fy = 2.5

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.
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.
Now, we create an instance of a class
class_name<type> ob;
where class_name: It is the name of the class.
type: It is the type of the data that the class is operating on.
ob: It is the name of the object.
Example
A program to illustrate how to define and declare class
template for reading two data items from the keyboard and to
the sum of the given two data items.
#include <iostream>
using namespace std;
template <class T>
class sample {
private :
T value1,value2;
public :
void getdata()
{
cin >> value1 >> value2;
}
void sum()
{
T value;
value = value1+value2;
cout<< " sum of ="<< value << endl;
}
};

int main()
{
sample <int> obj1;
sample <float> obj2;
cout << "Enter any two integers : "<< endl;
obj1.getdata();
obj1.sum();
cout << "Enter any two floating point numbers : " << endl;
obj2.getdata();
obj2.sum();
return 0;
}

Output
Enter any two integers :
45
67
sum of =112
Enter any two floating point numbers :
46.77
34.88
sum of =81.65

Process returned 0 (0x0) execution time : 62.712 s


Press any key to continue.

You might also like