Templates Classes

C++ programming support generic function and generic classes.

Generic function are——> template function.
Generic classes are———-> Template Classes.

Generic classes: C++ says that you can use same class for multiple data-types but at a time only one data-type but you can write any type of data it is called generic classes i.e Template classes

//simple code to understand
template <class T>
class Arithmetic
T a;
T b;
public :
Arithmetic(T a, T b);
T add();
T sub();
}; //Template class terminate.
//Now Writing Function after class as Template
-> Arithmetic <T> :: Arithmetic(T a, T b)
this->a = a;
this->b = b;
-> template <class T>
T Arithmetic<T> :: add()
T c;
return c;
//next func()
->template <class T>{
int Arithmeetic <T> :: sub()
T c;
return c;
int main()
Arithmetic <int> ar(10,5);
Arithmetic <float> ar1(10.5 , 1.2);

Here, same class is working for two different classes so, I don’t have to write two different classes separately.


Related post

Leave a Reply

Your email address will not be published. Required fields are marked *