آخـــر الـــمـــشـــاركــــات

تحميل برنامج الغاء تثبيت البرامج من الكمبيوتر Should I Remove It » آخر مشاركة: اردني وافتخر دردشة وتعليله وسواليف.. » آخر مشاركة: عاشق الحصن بريد الاعضاء » آخر مشاركة: محمد العزام اهلا بكم ..رمضان كريم » آخر مشاركة: حسان القضاة شو عم تسمع هلا » آخر مشاركة: حسان القضاة ما هو سبب تواجدك في المنتدى والى اي حدّ يستمر او ينتهي إنتسابك له ؟ » آخر مشاركة: قلعتي أبدية مرحبا » آخر مشاركة: محمد العزام " أميــــرةُ قـوسِ النَّصـــــر" » آخر مشاركة: قلعتي أبدية ~ إبريـــــــــــــــــل ~ » آخر مشاركة: حسان القضاة اسئلة مهمة بالفوتوشوب في المطابع 2019 » آخر مشاركة: المصمم يزن جبريل صاحب المركز الاول فى مجال تنزيل الملفات كامل مدي الحياة IDM 6.32 » آخر مشاركة: siiin همسات وأشوق » آخر مشاركة: حسان القضاة ""أيلـول""... » آخر مشاركة: قلعتي أبدية تبليغ عن رسالة زائر بواسطة راشد مرشد » آخر مشاركة: أميرة قوس النصر اشتقنالكم » آخر مشاركة: Mahmoud Zaben تُراهات ما قبل النوم ... » آخر مشاركة: قلعتي أبدية شو مزاجك اليوم... » آخر مشاركة: قلعتي أبدية قبول بلاغ عطل ثلاجات كلفينيتور 01092279973 & 0235700997 وكيل كلفينيتور (م .الجديدة) » آخر مشاركة: الوكيل1 قبول بلاغ عطل ثلاجات هوفر 01154008110 & 0235699066 وكيل هوفر (م.6اكتوبر) » آخر مشاركة: الوكيل1 قبول بلاغ عطل ثلاجات جنرال اليكتريك 01207619993 & 0235700997 وكيل جنرال اليكتريك (الز » آخر مشاركة: الوكيل1
+ الرد على الموضوع
النتائج 1 إلى 5 من 5

الموضوع: Templates

  1. #1
    عضو مؤسس الصورة الرمزية Ammar Qasaimeh
    تاريخ التسجيل
    Dec 2007
    العمر
    36
    المشاركات
    12,914

    افتراضي Templates

    Function templates

    Function templates are special functions that can operate with generic types. This allows us to create a function template whose functionality can be adapted to more than one type or class without repeating the entire code for each type.
    In C++ this can be achieved using template parameters. A template parameter is a special kind of parameter that can be used to pass a type as argument: just like regular function parameters can be used to pass values to a function, template parameters allow to pass also types to a function. These function templates can use these parameters as if they were any other regular type.

    The format for declaring function templates with type parameters is:

    template <class identifier> function_declaration;
    template <typename identifier> function_declaration;


    The only difference between both prototypes is the use of either the keyword class or the keyword typename. Its use is indistinct, since both expressions have exactly the same meaning and behave exactly the same way.

    For example, to create a template function that returns the greater one of two objects we could use:
    كود:
    template <class myType>
    myType GetMax (myType a, myType b) {
     return (a>b?a:b);
    }
    Here we have created a template function with myType as its template parameter. This template parameter represents a type that has not yet been specified, but that can be used in the template function as if it were a regular type. As you can see, the function template GetMax returns the greater of two parameters of this still-undefined type.

    To use this function template we use the following format for the function call:

    function_name <type> (parameters);


    For example, to call GetMax to compare two integer values of type int we can write:
    كود:
    int x,y;
    GetMax <int> (x,y);
    When the compiler encounters this call to a template function, it uses the template to automatically generate a function replacing each appearance of myType by the type passed as the actual template parameter (int in this case) and then calls it. This process is automatically performed by the compiler and is invisible to the programmer.

    Here is the entire example:
    كود:
    // function template
    #include <iostream>
    using namespace std;
    
    template <class T>
    T GetMax (T a, T b) {
      T result;
      result = (a>b)? a : b;
      return (result);
    }
    
    int main () {
      int i=5, j=6, k;
      long l=10, m=5, n;
      k=GetMax<int>(i,j);
      n=GetMax<long>(l,m);
      cout << k << endl;
      cout << n << endl;
      return 0;
    }
    In this case, we have used T as the template parameter name instead of myType because it is shorter and in fact is a very common template parameter name. But you can use any identifier you like.

    In the example above we used the function template GetMax() twice. The first time with arguments of type int and the second one with arguments of type long. The compiler has instantiated and then called each time the appropriate version of the function.

    As you can see, the type T is used within the GetMax() template function even to declare new objects of that type:
    كود:
    T result;
    Therefore, result will be an object of the same type as the parameters a and b when the function template is instantiated with a specific type.

    In this specific case where the generic type T is used as a parameter for GetMax the compiler can find out automatically which data type has to instantiate without having to explicitly specify it within angle brackets (like we have done before specifying <int> and <long>). So we could have written instead:
    كود:
    int i,j;
    GetMax (i,j);
    Since both i and j are of type int, and the compiler can automatically find out that the template parameter can only be int. This implicit method produces exactly the same result:
    كود:
    // function template II
    #include <iostream>
    using namespace std;
    
    template <class T>
    T GetMax (T a, T b) {
      return (a>b?a:b);
    }
    
    int main () {
      int i=5, j=6, k;
      long l=10, m=5, n;
      k=GetMax(i,j);
      n=GetMax(l,m);
      cout << k << endl;
      cout << n << endl;
      return 0;
    }
    Notice how in this case, we called our function template GetMax() without explicitly specifying the type between angle-brackets <>. The compiler automatically determines what type is needed on each call.

    Because our template function includes only one template parameter (class T) and the function template itself accepts two parameters, both of this T type, we cannot call our function template with two objects of different types as arguments
    كود:
    int i;
    long l;
    k = GetMax (i,l);
    This would not be correct, since our GetMax function template expects two arguments of the same type, and in this call to it we use objects of two different types.

    We can also define function templates that accept more than one type parameter, simply by specifying more template parameters between the angle brackets. For example:
    كود:
    template <class T, class U>
    T GetMin (T a, U b) {
      return (a<b?a:b);
    }
    In this case, our function template GetMin() accepts two parameters of different types and returns an object of the same type as the first parameter (T) that is passed. For example, after that declaration we could call GetMin() with:
    كود:
    int i,j;
    long l;
    i = GetMin<int,long> (j,l);
    or simply:
    كود:
    i = GetMin (j,l);
    even though j and l have different types, since the compiler can determine the appropriate instantiation anyway.

    Class templates

    We also have the possibility to write class templates, so that a class can have members that use template parameters as types. For example:
    كود:
    template <class T>
    class mypair {
        T values [2];
      public:
        mypair (T first, T second)
        {
          values[0]=first; values[1]=second;
        }
    };
    The class that we have just defined serves to store two elements of any valid type. For example, if we wanted to declare an object of this class to store two integer values of type int with the values 115 and 36 we would write:
    كود:
    mypair<int> myobject (115, 36);
    this same class would also be used to create an object to store any other type:
    كود:
    mypair<double> myfloats (3.0, 2.18);
    The only member function in the previous class template has been defined inline within the class declaration itself. In case that we define a function member outside the declaration of the class template, we must always precede that definition with the template <...> prefix:
    كود:
    // class templates
    #include <iostream>
    using namespace std;
    
    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;
    }
    Notice the syntax of the definition of member function getmax:
    كود:
    template <class T>
    T mypair<T>::getmax ()
    Confused by so many T's? There are three T's in this declaration: The first one is the template parameter. The second T refers to the type returned by the function. And the third T (the one between angle brackets)


    Good Luck





  2. #2
    عضو جديد
    تاريخ التسجيل
    Dec 2008
    العمر
    46
    المشاركات
    16

    افتراضي رد: Templates

    مشكورررررررررررر

  3. #3
    عضو جديد
    تاريخ التسجيل
    Jan 2009
    العمر
    41
    المشاركات
    4

    افتراضي رد: Templates

    شكرا على الموضوع الممتاز ...........................

  4. #4
    عضو مؤسس الصورة الرمزية Ammar Qasaimeh
    تاريخ التسجيل
    Dec 2007
    العمر
    36
    المشاركات
    12,914

    افتراضي رد: Templates

    شكرا ع المرور

  5. #5
    كبار الشخصيات الصورة الرمزية ريمي
    تاريخ التسجيل
    Jan 2009
    الدولة
    الاردن
    العمر
    26
    المشاركات
    1,642

    افتراضي رد: Templates

    شكرا على الموضوع
    لا شيئ يستحق العناء ..

+ الرد على الموضوع

معلومات الموضوع

الأعضاء الذين يشاهدون هذا الموضوع

الذين يشاهدون الموضوع الآن: 1 (0 من الأعضاء و 1 زائر)

مواقع النشر (المفضلة)

مواقع النشر (المفضلة)

ضوابط المشاركة

  • لا تستطيع إضافة مواضيع جديدة
  • تستطيع الرد على المواضيع
  • لا تستطيع إرفاق ملفات
  • لا تستطيع تعديل مشاركاتك
  •