Home  Writing  Games  Music  Dev  About 

C++ Object Oriented Programming

Classes

Templates

functions


    template <class type> ret-type func-name(parameter list) {
       // body of function
    } 
    
    template <typename T>
    T Max (T a, T b) { 
       return a < b ? b:a; 
    }
    
    //Function Definition
    template <class myType>
    myType GetMax (myType a, myType b) {
     return (a>b?a:b);
    }
    
    template <class T, class U>
    T GetMin (T a, U b) {
      return (a<b?a:b);
    }
    
    // Function Call
    int x,y,i;
    long l;
    
    i = GetMin<int,long> (x,l);
    GetMax <int> (x,y);
    
    // in these cases, compiler can figure out types    
    i = GetMin (j,l);
    GetMax(x,y);
    
    template <typename T>
    inline T const& Max (T const& a, T const& b) { 
       return a < b ? b:a; 
    }

classes


    template<typename T> class LinkList;
    
    
    template <class T>
    class mypair {
        T a, b;
      public:
        mypair (T first, T second)
          {a=first; b=second;}
        T getmax ();
    };
    
    template <class T>
    T mypair<T>::getmax ()
    {
      T retval;
      retval = a>b? a : b;
      return retval;
    }
    
    int main () {
      mypair <int> myobject (100, 75);
      cout << myobject.getmax();
      return 0;
    }