C++
Template Class 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 the data type as a parameter.
For supporting templates, C++ adds two new keywords: ‘template’ and ‘typename’. The second keyword can always be replaced by the keyword ‘class’.
How templates work?
Like Macros, templates are expanded at compile time. 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 the same function/class.
Like Macros, templates are expanded at compile time. 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 the same function/class.
Function Templates We write a generic function that can be used for different data types. Examples of function templates are sort(), max(), min(), printArray().
Below is the program to implement the Bubble Sort using templates in C++:
Output:
Sorted array : 10 20 30 40 50
Class Templates Like function templates, class templates are useful when a class defines something that is independent of the data type. It can be useful for classes like LinkedList, BinaryTree, Stack, Queue, Array, etc.
Following is a simple example of a template Array class.
Output:
1 2 3 4 5
Can there be more than one arguments to templates?
Yes, like normal parameters, we can pass more than one data type as arguments to templates. The following example demonstrates the same.
Yes, like normal parameters, we can pass more than one data type as arguments to templates. The following example demonstrates the same.
Output:
Constructor Called Constructor Called
Can we specify a default value for template arguments?
Yes, like normal parameters, we can specify default arguments to templates. The following example demonstrates the same.
Output:
Constructor Called
What is the difference between function overloading and templates?
Both function overloading and templates are examples of polymorphism features of OOP. Function overloading is used when multiple functions do similar operations, templates are used when multiple functions do identical operations.
What happens when there is a static member in a template class/function?
Each instance of a template contains its own static variable.
Each instance of a template contains its own static variable.
What is a template specialization?
Template specialization allows us to have different codes for a particular data type.
Template specialization allows us to have different codes for a particular data type.
Can we pass nontype parameters to templates?
We can pass non-type arguments to templates. Non-type parameters are mainly used for specifying max or min values or any other constant value for a particular instance of a template. The important thing to note about non-type parameters is, they must be const. The compiler must know the value of non-type parameters at compile time. Because compiler needs to create functions/classes for a specified non-type value at compile time. In the below program, if we replace 10000 or 25 with a variable, we get a compiler error.
We can pass non-type arguments to templates. Non-type parameters are mainly used for specifying max or min values or any other constant value for a particular instance of a template. The important thing to note about non-type parameters is, they must be const. The compiler must know the value of non-type parameters at compile time. Because compiler needs to create functions/classes for a specified non-type value at compile time. In the below program, if we replace 10000 or 25 with a variable, we get a compiler error.
Below is a C++ program.
Output:
10 1
Post a Comment
0 Comments