Operator overload for generic parameters : generic class « template « C++ Tutorial






#include<iostream.h>
template<class T>
class Sample
{
  T n;
  public:
         Sample(T i){
           n=i;
         }
    int operator==(Sample &s)
    {
      if(n==s.n)
         return 1;
      else
         return 0;
    }

};

int main()
{
  Sample<int> s1(2),s2(3);
  cout<<(s1==s2)<<endl;
  Sample<double> s3(2.5),s4(2.5);
  cout<<(s1==s2)<<endl;
}
0
0








13.12.generic class
13.12.1.A generic class
13.12.2.Operator overload for generic parameters
13.12.3.Class specialization
13.12.4.array template
13.12.5.Use two generic data types in a class definition