Do I Have To Template A Struct That Is Inside Of A Templated Clasws
A template is a simple and all the same very powerful tool in C++. The unproblematic thought is to pass data type equally a parameter so that we don't need to write the same lawmaking for different data types. For example, a software company may demand sort() for different data types. Rather than writing and maintaining the multiple codes, we tin write one sort() and pass data type every bit a parameter.
C++ adds ii new keywords to support templates: 'template' and 'typename'. The 2nd keyword tin can always be replaced by keyword 'form'.
How practice 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 role/class, merely compiled code may comprise multiple copies of same function/course.
Function Templates We write a generic function that can be used for different data types. Examples of office templates are sort(), max(), min(), printArray().
Know more than on Generics in C++
CPP
#include <iostream>
using
namespace
std;
template
<
typename
T>
T myMax(T 10, T y)
{
return
(x > y)? x: y;
}
int
main()
{
cout << myMax<
int
>(3, vii) << endl;
cout << myMax<
double
>(3.0, 7.0) << endl;
cout << myMax<
char
>(
'g'
,
'due east'
) << endl;
return
0;
}
Output:
vii 7 grand
Beneath is the plan to implement Bubble Sort using templates in C++:
CPP
#include <iostream>
using
namespace
std;
template
<
grade
T>
void
bubbleSort(T a[],
int
due north) {
for
(
int
i = 0; i < northward - 1; i++)
for
(
int
j = due north - one; i < j; j--)
if
(a[j] < a[j - 1])
swap(a[j], a[j - 1]);
}
int
main() {
int
a[5] = {10, 50, 30, 40, 20};
int
n =
sizeof
(a) /
sizeof
(a[0]);
bubbleSort<
int
>(a, due north);
cout <<
" Sorted array : "
;
for
(
int
i = 0; i < n; i++)
cout << a[i] <<
" "
;
cout << endl;
return
0;
}
Output
Sorted array : 10 20 30 40 50
Output:
Sorted array : 10 xx 30 40 50
Class Templates Similar function templates, course templates are useful when a class defines something that is contained of the data type. Can be useful for classes like LinkedList, BinaryTree, Stack, Queue, Array, etc.
Following is a simple example of template Array class.
CPP
#include <iostream>
using
namespace
std;
template
<
typename
T>
form
Assortment {
individual
:
T *ptr;
int
size;
public
:
Assortment(T arr[],
int
s);
void
print();
};
template
<
typename
T>
Array<T>::Array(T arr[],
int
s) {
ptr =
new
T[s];
size = s;
for
(
int
i = 0; i < size; i++)
ptr[i] = arr[i];
}
template
<
typename
T>
void
Array<T>::print() {
for
(
int
i = 0; i < size; i++)
cout<<
" "
<<*(ptr + i);
cout<<endl;
}
int
main() {
int
arr[5] = {i, 2, 3, 4, 5};
Array<
int
> a(arr, 5);
a.print();
return
0;
}
Output:
1 two 3 four 5
Tin there be more than one arguments to templates?
Aye, similar normal parameters, we can laissez passer more than than one data types as arguments to templates. The following example demonstrates the aforementioned.
CPP
#include<iostream>
using
namespace
std;
template
<
class
T,
form
U>
course
A {
T x;
U y;
public
:
A() { cout<<
"Constructor Called"
<<endl; }
};
int
principal() {
A<
char
,
char
> a;
A<
int
,
double
> b;
return
0;
}
Output
Constructor Called Constructor Called
Output:
Constructor Called Constructor Called
Can we specify default value for template arguments?
Aye, like normal parameters, we can specify default arguments to templates. The following instance demonstrates the same.
CPP
#include<iostream>
using
namespace
std;
template
<
form
T,
class
U =
char
>
grade
A {
public
:
T x;
U y;
A() { cout<<
"Constructor Called"
<<endl; }
};
int
master() {
A<
char
> a;
return
0;
}
Output:
Constructor Called
What is the difference between function overloading and templates?
Both function overloading and templates are examples of polymorphism feature of OOP. Office overloading is used when multiple functions do similar operations, templates are used when multiple functions exercise identical operations.
What happens when there is a static member in a template grade/function?
Each instance of a template contains its own static variable. See Templates and Static variables for more than details.
What is template specialization?
Template specialization allows us to accept dissimilar lawmaking for a item information type. Run across Template Specialization for more details.
Can we laissez passer nontype parameters to templates?
We tin can pass not-type arguments to templates. Not-blazon parameters are mainly used for specifying max or min values or whatsoever other abiding value for a particular instance of a template. The of import thing to notation about non-type parameters is, they must exist const. The compiler must know the value of non-type parameters at compile time. Because the compiler needs to create functions/classes for a specified non-type value at compile time. In below program, if we supplant 10000 or 25 with a variable, we become a compiler error. Please see this.
Below is a C++ program.
CPP
#include <iostream>
using
namespace
std;
template
<
grade
T,
int
max>
int
arrMin(T arr[],
int
northward)
{
int
m = max;
for
(
int
i = 0; i < n; i++)
if
(arr[i] < m)
m = arr[i];
return
one thousand;
}
int
chief()
{
int
arr1[] = {ten, 20, fifteen, 12};
int
n1 =
sizeof
(arr1)/
sizeof
(arr1[0]);
char
arr2[] = {i, ii, 3};
int
n2 =
sizeof
(arr2)/
sizeof
(arr2[0]);
cout << arrMin<
int
, 10000>(arr1, n1) << endl;
cout << arrMin<
char
, 256>(arr2, n2);
return
0;
}
Output:
x 1
Here is an instance of C++ program to testify different data types using constructor and template. We will perform few actions
- passing character value by creating an objects in chief() function.
- passing integer value past creating an objects in principal() function.
- passing float value by creating an objects in main() function.
C++
#include <iostream>
#include <conio.h>
template
<
form
Tl>
class
info
{
public
:
info(TI, A)
{
cout<<
"\northward"
<<
"A = "
<<A<<
" size of information in bytes:"
<<
sizeof
(ch);
}
};
int
primary()
{
clrscr();
info<
char
>p(
'x'
);
info<
int
>q(22);
info<
float
>r(2.25);
return
0;
}
Output:
A = 10 size of data in bytes: 1 A = 22 size of data in bytes: 2 A = two.25 size of information in bytes: 4
What is template metaprogramming?
See Template Metaprogramming
Yous may likewise like to take a quiz on templates.
Java as well supports these features. Java calls it generics .
Delight write comments if you find anything incorrect, or you want to share more information virtually the topic discussed above.
Do I Have To Template A Struct That Is Inside Of A Templated Clasws,
Source: https://www.geeksforgeeks.org/templates-cpp/
Posted by: rivasforengs.blogspot.com
0 Response to "Do I Have To Template A Struct That Is Inside Of A Templated Clasws"
Post a Comment