Generics in C++
Generics is the idea to allow type (Integer, String, … etc and user-defined types) to be a parameter to methods, classes and interfaces. For example, classes like an array, map, etc, which can be used using generics very efficiently. We can use them for any type.
The method of Generic Programming is implemented to increase the efficiency of the code. Generic Programming enables the programmer to write a general algorithm which will work with all data types. It eliminates the need to create different algorithms if the data type is an integer, string or a character.
The advantages of Generic Programming are
- Code Reusability
- Avoid Function Overloading
- Once written it can be used for multiple times and cases
Templates
Templates are powerful features of C++ which allows you to write generic programs. In simple terms, you can create a single function or a class to work with different data types using templates.
Templates are often used in larger codebase for the purpose of code reusability and flexibility of the programs.
The concept of templates can be used in two different ways:
- Function Templates
- Class Templates
Function Templates
A function template works in a similar to a normal function, with one key difference.
A single function template can work with different data types at once but, a single normal function can only work with one set of data types.
Normally, if you need to perform identical operations on two or more types of data, you use function overloading to create two functions with the required function declaration.
However, a better approach would be to use function templates because you can perform the same task writing less and maintainable code.
How to declare a function template?
A function template starts with the keyword template followed by template parameter/s inside < >
which is followed by function declaration.
template <class T>
T someFunction(T arg)
{
... .. ...
}
In the above code T is a template argument that accepts different data types (int, float), and class is a keyword.
You can also use keyword typename
instead of class in the above example.
When, an argument of a data type is passed to someFunction( )
, compiler generates a new version of someFunction()
for the given data type.
Class Templates
Like function templates, you can also create class templates for generic class operations.
Sometimes, you need a class implementation that is same for all classes, only the data types used are different.
Normally, you would need to create a different class for each data type OR create different member variables and functions within a single class.
This will unnecessarily bloat your code base and will be hard to maintain, as a change is one class/function should be performed on all classes/functions.
However, class templates make it easy to reuse the same code for all data types.
How to declare a class template?
template <class T>
class className
{
... .. ...
public:
T var;
T someOperation(T arg);
... .. ...
};
In the above declaration, T
is the template argument which is a placeholder for the data type used.
Inside the class body, a member variable and a member function someOperation()
are both of type T
.
How to create a class template object?
To create a class template object, you need to define the data type inside a < >
when creation.
className<dataType> classObject;
For example:
className<int> classObject;
className<float> classObject;
className<string> classObject;
Function overloading
Creating two or more members that have the same name but are different in number or type of parameter is known as overloading.
In C++, we can overload:
- Methods
- Constructors
- Indexed Properties
The process of having two or more functions with the same name, but different parameters, is known as function overloading.
The function is redefined by either using different types of arguments or a different number of arguments. It is only through these differences that a compiler can differentiate between functions.
The advantage of function overloading is that it increases the readability of a program because you don’t need to use different names for the same action.
Function overloading example
Let’s see this simple example of function overloading where we change the number of arguments of add() method.
#include<iostream.h>.
using namespace std;
class Cal {
public:
static int add(int a,int b){
return a + b;
}
static int add(int a, int b, int c)
{
return a + b + c;
}
};
int main(void) {
Cal C; //class object declaration. // class object declaration.
cout<<C.add(10, 20)<<endl;
cout<<C.add(12, 20, 23);
return 0;
}
Explanation
- We have created two
add()
methods that have two different numbers of arguments. - Now, in the
main()
function, we call these two methods by passing different numbers of arguments through the same callc.add
. - The compiler will differentiate based on the number of passing arguments, and execute the correct method accordingly.