Generic programming
● Generic programming is a style of computer programming in which
algorithms are written in terms of types to-be-specified-later that are
then instantiated when needed for specific types provided as
parameters.
● A significant benefit of oops is reusability of code which eliminates
redundant coding.
● An important feature of c++ called templates strengthen this benefit of
OOP and provides great flexibility to the language
● Templates support generic programming that allows to develop
reusable software components such as functions and classes ,
supporting different data type in a single framework
Example:
#include<iostream>
using namespace std;
template<class T>
class weight
{
T kg;
Public:
void setdata(T x)
{
kg=x;
}
T getdata()
{
return kg;
}
};
int main()
{
weight<int>obj1;
[Link](5);
cout<<"value is:"<<[Link]()<<endl;
weight<double>obj2;
[Link](5.87657);
cout<<"value is:"<<[Link]()<<endl;
return 0;
}