Learning  C++
Home
Tutorials
C++  Programs
Contact  us
Sitemap
// Program to illustrate class templates

#include<iostream.h>
#include<conio.h>

template <class T>
class sample
{

     T a;
     T b;
     public:
      T sum(T x,T y);
};

template <class T>
T sample<T>::sum(T x,T y)
{
     a=x;
     b=y;
     return a+b;
}

void main()
{
     clrscr();
     sample <int> obj1;
     cout<<"\n sum of two integers "<<obj1.sum(4,5);
     sample <float> obj2;
     cout<<"\n sum of two float values "<<obj2.sum(1.5,2.3);
}



Test data

sum of two integers 9
sum of two float values 3.8