1. Khái niệm:
    - Xét hàm Max(x, y) để tính max của 2 đối số x, y. Nếu không sử dụng khuôn hình hàm thì với mỗi kiểu dữ liệu của x, y (int, float, long, ...) thì ta phải xây dựng một hàm Max riêng.
   -  Dùng khuôn hình hàm, ta có thể định nghĩa một mẫu cho một họ các hàm tương ứng, bằng cách thay kểu dữ liệu như một tham số.

2. Ví dụ khuôn hình hàm:
    Tìm max của hai số:
#include <iostream>

using namespace std;
template <class T> T GetMax(T a, T b)
{
      return (a > b ? a : b);
}

void main()
{
      int n = 10, m = 20;
      cout << "Max = " << GetMax<int>(n, m) << endl;
      float x = 1.5f, y = 2.5f;
      cout << "Max = " << GetMax<float>(x, y) << endl;
      getchar();
}



#include <iostream>

using namespace std;

template <class U, class V> U GetMax(U a, V b)
{
      return (a > b ? a : b);
}

void main()
{
      float n = 10.5f; int m = 12;
      cout << "Max = " << GetMax<float, int>(n, m) << endl;
      cout << "Max = " << GetMax<float>(10.5, 12.5) << endl;
      getchar();
}

  Ví dụ hàm tính tổng hai số:
#include <iostream>

using namespace std;

template <class T> T Add(T a, T b)
{
      return a + b;
}

void main()
{
      int n = 10, m = 20;
      cout << "Add = " << Add<int>(n, m) << endl;
      getchar();
}

3. Ví dụ khuôn hình lớp:
#include <iostream>

using namespace std;

template <class T>
class MyPair
{
private:
      T a, b;//Tạo class MyPair có các biến thành phần kiểu T
public:
      MyPair(T first, T second)
      {
            a = first;
            b = second;
      }
      //Hàm Print() in giá trị của các biến a và b
      void Print()
      {
            cout << "First = " << a << ", Second = "<< b << endl;
      }
      T GetMax();
};

template <class T> T MyPair<T>::GetMax()//Cài đặt hàm GetMax
{
      return a > b ? a : b;
}
void main()
{
      MyPair<int> pair1(100, 200);
      pair1.Print();
      cout << "Max: " << pair1.GetMax() << endl;
     
      MyPair<char> pair2('A', 'B');
      pair2.Print();
      cout << "Max: " << pair2.GetMax() << endl;

      getchar();
}


 Ví dụ khuôn hình lớp:


. Vdfafsfsa