0% found this document useful (0 votes)
6 views12 pages

Understanding Generic Programming in C++

Generic programming allows algorithms to be written in terms of types that are specified later using templates. Templates support generic programming in C++ by enabling the development of reusable functions and classes that can support different data types through a single interface. For example, a template class can be used to store both integer and double values by instantiating it with the appropriate type.

Uploaded by

Harshada Jadhav
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views12 pages

Understanding Generic Programming in C++

Generic programming allows algorithms to be written in terms of types that are specified later using templates. Templates support generic programming in C++ by enabling the development of reusable functions and classes that can support different data types through a single interface. For example, a template class can be used to store both integer and double values by instantiating it with the appropriate type.

Uploaded by

Harshada Jadhav
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

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;
}

You might also like