What are Generics?
Generics is the concept of allowing type (Integer, String,...) to be sent as a parameter to methods, classes, and interfaces. For example, generics may be utilized quite well with classes like arrays, maps, and so on. They are useful in all cases.
The Generic Programming approach is used to improve the efficiency of the code. Generic programming allows a programmer to create a broad algorithm that can handle any data type. If the data type is an integer, string, or character, it avoids the need for multiple algorithms.
Templates:
Generics can be implemented in C++ using Templates. Template is a simple and yet very powerful tool in C++. A template is a simple and yet very powerful tool in C++. The basic concept is to pass data type as a parameter to avoid having to write the same code for multiple data kinds. Sort(), for example, may be required by a software firm for various data formats. We can construct a single sort() function and pass data type as a parameter, rather than developing and maintaining numerous codes.
C++ adds two new keywords to support templates:
‘template’ and ‘typename’. The keyword ‘class' can always be used in place of the second keyword.
How
do templates work?
Templates are expanded at compiler time. This is like
macros. The compiler, on the other hand, performs type checking prior to template expansion. The concept is straightforward: source code only contains one function or class, whereas generated code may have numerous copies of the same function or class.
Class Templates
When a class describes anything that isn't dependent on the data type, class templates come in handy. LinkedList, BinaryTree, Stack, Queue, Array, and more classes may benefit from this.
Class Templates features:
You can create class templates for general class operations in the same way that you can construct function templates.
You may occasionally require a class implementation that is the same for all classes with the exception of the data types used.
Normally, you'd have to make a separate class for each data type.
OR
Within a same class, define distinct member variables and functions.
This would unnecessarily bloat your code base and make it difficult to maintain, because any modification to one class or function should be applied to all classes and functions. Class templates, on the other hand, make it simple to reuse the same code for all data kinds.
How to declare a class template?
template <class T>
class className
{
... .. ...
public:
T var;
T someOperation(T
arg);
... .. ...
};
T is the template argument in the above declaration, which is a placeholder for the data type utilised. A member variable var and a member method someOperation() are both of type T inside the class body.
How to create a class template object?
When creating a class template object, you must specify the data type inside a < >.
For example:
1. className<int>
classObject;
2. className<float>
classObject;
3. className<string>
classObject;
Program
for Generic Class using Class Template:
Program 1:
#include
<iostream>
using
namespace std;
template <class T>
class
Calculator
{
private:
T num1, num2;
public:
Calculator(T a, T b)
{
num1 = a;
num2 = b;
}
void Add()
{
cout << "Two
numbers are: " << num1 << " and " << num2
<<endl;
cout << "Addition
is: " << num1+num2 << endl;
}
};
int main()
{
Calculator<int> Addint(5, 7);
Calculator<float> Addfloat(2.9,
5.2);
cout << "Addition of
integers:" << endl;
Addint.Add();
cout << endl <<
"Addition of floats:" << endl;
Addfloat.Add();
return 0;
}
Output:
Addition of integers:
Two
numbers are: 5 and 7
Addition
is: 12
Addition of floats:
Two
numbers are: 2.9 and 5.2
Addition
is: 8.1
Program 2:
#include<bits/stdc++.h>
using namespace std;
template<class T>
class Array
{
T *arr;
int size;
public:
Array(int n)
{
size=n;
arr=new T[size];
for(int i=0;i<size;i++)
arr[i]=0;
}
~Array()
{
delete arr;
}
void printarr()
{
cout<<"Printing Array elements:\n";
for (int i = 0; i < size; i++)
{
if(i<size-1)
cout<<arr[i]<<" , ";
else
cout<<arr[i]<<"\n";
}
}
void setvalue()
{
for (int i = 0; i < size; i++)
{
arr[i]='a'+i;
}
}
};
int main()
{
int n=10;
Array<int>arr1(n);
Array<char>arr2(n);
arr1.setvalue();
arr2.setvalue();
cout <<endl;
arr1.printarr();
arr2.printarr();
}
Output:
Printing Array elements:
97 , 98 , 99 , 100 , 101 , 102 , 103 , 104 , 105 , 106
Printing Array elements:
a , b , c , d , e , f , g , h , i , j
Advantages of Generic Programming:
- Code Reusability
- Avoid Function Overloading
- Once written, it can be used in a variety of situations.
Very Informative Blog👏👏
ReplyDeleteNice work 👍🏼
ReplyDeleteInformative 👍
ReplyDeleteNice blog
ReplyDeleteGreat Blog 🙌
ReplyDeleteUseful blog!
ReplyDelete