Minggu, 21 Oktober 2012

Materi 1 Tempalte


template <class T>
T min(const  T &a, const T &b)
{
      if (a<b)
            return a;
      else
            return b;
};
template <class T, class U>
void  mins(const  T& a, const U &b){
      if (a<b)
            cout<< a;
      else
            cout<<b;
      cout<<endl;


};
template <class T, class U>
T reverse (const U& x){
      return(-x);
};
//excersise 1
template <class haha>
haha square(const haha& hihi)
{
      return hihi*hihi;
}
//excercise 3
template <class ha, class he>
he find(const ha& hihi, const he& hu)
{     int i;
      for(i=0;i<5;i++)
      {     if(hihi[i] == hu) break;
      }
      if (i>=5) return -1;
      else return i;
}
#include "iostream.h"
void main()
{     int tes [5] = {2,3,4,5,6};
      //cout<<min(2,4)<<endl;       //integer
      //cout<<min(2.2,1.4)<<endl;   //float

      //mins('a',100);
      //cout<<reverse<int>(100)<<endl;
      //cout<<square(19.2)<<endl;
      cout<<find(tes,2)<<endl;
}

0 komentar:

Posting Komentar