0% found this document useful (0 votes)
5 views

Classes-Template in C++

C++ templates are a powerful feature that allows for generic programming by enabling functions and classes to operate on different data types without code duplication. Templates are defined using the keywords 'template' and 'typename' (or 'class'), and they can be used for both function and class templates, supporting multiple parameters and overloading. The Standard Template Library (STL) is built on these templates, providing a collection of template classes for common data structures and algorithms.

Uploaded by

vasanthaxukai
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)
5 views

Classes-Template in C++

C++ templates are a powerful feature that allows for generic programming by enabling functions and classes to operate on different data types without code duplication. Templates are defined using the keywords 'template' and 'typename' (or 'class'), and they can be used for both function and class templates, supporting multiple parameters and overloading. The Standard Template Library (STL) is built on these templates, providing a collection of template classes for common data structures and algorithms.

Uploaded by

vasanthaxukai
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

Templates in C++

A template is a simple and yet very powerful tool in C++. 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. For 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.
C++ adds two new keywords to support templates: ‘template’ and ‘typename’.
The second keyword can always be replaced by keyword ‘class’.

How do templates work?


Templates are expanded at compiler time. This is like macros. The difference
is, the 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.

Example for template


References:

https://www.youtube.com/watch?v=CWj7lLY2GLA&t=57s (Class Template)


https://www.youtube.com/watch?v=CWj7lLY2GLA&t=57s (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().
Know more on Generics in C++

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:

o Function templates
o 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
o 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.
o The type of the data that the function will operate on depends on the type of the data
passed as a parameter.
o For example, Quick sorting algorithm is implemented using a generic function, it can be
implemented to an array of integers or array of floats.
o A Generic function is created by using the keyword template. The template defines what
function will do.

Syntax of Function Template

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.

Let's see a simple example of a function template:


In the above example, we create the function template which can perform the addition
operation on any type either it can be integer, float or double.

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

In the above syntax, we have seen that the template function can accept any number of
arguments of a different type.

Let's see a simple example:

In the above example, we use two generic types in the template function, i.e., X and Y.
Overloading a Function Template
We can overload the generic function means that the overloaded template functions
can differ in the parameter list.

Let's understand this through a simple example:


In the above example, template of fun() function is overloaded.

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.

Let's understand this through a simple example:


In the above example, we overload the ordinary functions. We cannot overload the
generic functions as both the functions have different functionalities. First one is
displaying the value and the second one determines whether the number is even or not.

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

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

1. 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.

Let's see a simple example:


In the above example, we create a template for class A. Inside the main() method, we
create the instance of class A named as, 'd'.

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

Let's see a simple example when class template contains two generic data types.
Let's see a simple example of nontype template arguments.
In the above example, the class template is created which contains the nontype
template argument, i.e., size. It is specified when the object of class 'A' is created.

Conclusion
Points to Remember

o C++ supports a powerful feature known as a template to implement the concept


of generic programming.
o A template allows us to create a family of classes or family of functions to handle
different data types.
o Template classes and functions eliminate the code duplication of different data
types and thus makes the development easier and faster.
o Multiple parameters can be used in both class and function template.
o Template functions can also be overloaded.
o We can also use nontype arguments such as built-in or derived data types as
template arguments.

The C++ Standard Template Library


(STL)
The Standard Template Library (STL) is a set of C++ template classes to
provide common programming data structures and functions such as lists,
stacks, arrays, etc. It is a library of container classes, algorithms, and iterators.
It is a generalized library and so, its components are parameterized. A working
knowledge of template classes is a prerequisite for working with STL.
STL has four components
 Algorithms
 Containers
 Functions
 Iterators

You might also like