Write a C++ Program to show Example of Static member variable of template class. Here’s a Simple C++ Program to show Example of Static member variable of template class in C++ Programming Language.
Templates are the foundation of generic programming, which involves writing code in a way that is independent of any particular type.
A template is a blueprint or formula for creating a generic class or a function. The library containers like iterators and algorithms are examples of generic programming and have been developed using template concept.
There is a single definition of each container, such as vector, but we can define many different kinds of vectors for example, vector <int> or vector <string>..
Function Template :
The general form of a template function definition is shown here:
Just as we can define function templates, we can also define class templates. The general form of a generic class declaration is shown here:
template <class type> class class-name
{ . . . }
Below is the source code for C++ Program to show Example of Static member variable of template class which is successfully compiled and run on Windows System to produce desired output as shown below :
SOURCE CODE : :
/* C++ Program to show Example of Static member variable of template class */
#include <iostream>
using namespace std;
template <class T>
class XYZ
{
public:
void putPri();
static T ipub;
private:
static T ipri;
};
template <class T>
void XYZ<T>::putPri()
{
cout << ipri++ << endl;
}
// Static variable initialization:
template <class T> T XYZ<T>::ipub = 1;
template <class T> T XYZ<T>::ipri = 1.2;
int main()
{
XYZ<int> aaa;
XYZ<float> bbb;
aaa.putPri();
cout << aaa.ipub << endl;
bbb.putPri();
return 0;
}
OUTPUT : :
/* C++ Program to show Example of Static member variable of template class */
1
1
1.2
Process returned 0
Above is the source code and output for C++ Program to show Example of Static member variable of template class which is successfully compiled and run on Windows System to produce desired output.
Templates are the foundation of generic programming, which involves writing code in a way that is independent of any particular type.
A template is a blueprint or formula for creating a generic class or a function. The library containers like iterators and algorithms are examples of generic programming and have been developed using template concept.
There is a single definition of each container, such as vector, but we can define many different kinds of vectors for example, vector <int> or vector <string>..
Function Template :
The general form of a template function definition is shown here:
Class Template :
Just as we can define function templates, we can also define class templates. The general form of a generic class declaration is shown here:
Below is the source code for C++ Program to show Example of Static member variable of template class which is successfully compiled and run on Windows System to produce desired output as shown below :
SOURCE CODE : :
OUTPUT : :
Above is the source code and output for C++ Program to show Example of Static member variable of template class which is successfully compiled and run on Windows System to produce desired output.
need an explanation for this answer? contact us directly to get an explanation for this answer