intTypePromotion=1
zunia.vn Tuyển sinh 2024 dành cho Gen-Z zunia.vn zunia.vn
ADSENSE

Lập trình hướng đối tượng - Chương 5

Chia sẻ: Đặng Kim Thạch | Ngày: | Loại File: PPT | Số trang:40

182
lượt xem
54
download
 
  Download Vui lòng tải xuống để xem tài liệu đầy đủ

Tài liệu tham khảo giáo trình Lập trình hướng đối tượng - Chương 5: Templates

Chủ đề:
Lưu

Nội dung Text: Lập trình hướng đối tượng - Chương 5

  1. Chương 5 TEMPLATES 1  Lập trình hướng đối tượng – Templates
  2. Tài liệu đọc 2  Lập trình hướng đối tượng – Templates
  3. Templates • Function templates • Class templates • Inheriting Template Classes 3  Lập trình hướng đối tượng – Templates
  4. Templates • According to the dictionary, a template is a pattern or guide used to replicate an object e.g. a biscuit cutter.    4  Lập trình hướng đối tượng – Templates
  5. Function templates • A function template is a pattern for creating a family of similar functions. • If you need two or more functions with identical behaviour that differ only in their parameter types, you can write a function template that the compiler will use to generate 5  Lập trình hướng đối tượng – Templates
  6. Function templates • A function template has the general form template    returnType functionName(parameterList)     {       // Body of the function    }  6  Lập trình hướng đối tượng – Templates
  7. Function templates (I) • The keyword template indicates that what follows is a function template, not an actual function. • The notation is the template parameter list. 7  Lập trình hướng đối tượng – Templates
  8. Function templates (II) • The keyword class indicates that T is a generic type i.e. a placeholder for a data type used by the function. • The identifier T appears throughout the function definition wherever this type needs to be written. 8  Lập trình hướng đối tượng – Templates
  9. Generated functions • When the function template is called, the compiler deduces the type of the actual argument and substitutes it for the generic type T, creating what is known as a generated function. • The act of generating a function is referred to as instantiating the  Lập trình hướng đối tượng – Templates 9
  10. template   void swap ( T& first,  T& second)  {      T   temporary = first;      first = second;     second = temporary; } Example 1 10  Lập trình hướng đối tượng – Templates
  11. int main(void)  {     int  x = 1 , y = 2 ;     char a = 'A‘ ;    char b = ‘B’;        swap(x, y);     swap(a, b);    // ... Example 1 .. 2    return 0;  }  11  Lập trình hướng đối tượng – Templates
  12. Explicitly specifying the type • The actual type that is substituted for a template's generic type can be explicitly specified in a function call by writing it between angle brackets immediately after the function name.  int i = 11;    int j = 22;  swap(i, j);  • The type that is explicitly specified 12  Lập trình hướng đối tượng – Templates
  13. Instantiating with different types • You cannot instantiate a function template if the types of arguments in your function call fail to match the template's expectations. int r = 3;  double s = 4.4;  swap(r, s);   // Error  13  Lập trình hướng đối tượng – Templates
  14. Mixing generic types and actual types • A function template's parameter list may contain a mixture of generic and actual types. template   void rotate ( T array[ ], int size)   {       T  temporary = array[0];      for (int i = 0; i 
  15. Making templates available to the compiler • Do not separate a function template's declaration from its definition by placing them in separate header and implementation files. • Place a function template's declaration and its definition in the header file.  Lập trình hướng đối tượng – Templates 15
  16. Overloading a function template • If a function template cannot handle all of the instantiations that you want it to perform, you may wish to overload it. • If you explicitly overload a function template, then the overloaded function will take 16  Lập trình hướng đối tượng – Templates
  17. // Swaps two character strings  // The strings are large enough for the swap to //occur template void swap(char* first, char* second)  {      int maxLength;  // Length of longest string       // Find length of longest string       if (strlen(first) >= strlen(second))           maxLength = strlen(first);       else  Example 2          maxLength = strlen(second);  17  Lập trình hướng đối tượng – Templates
  18. // Allocate memory for temporary string      char* temporary = new char [maxLength + 1];      assert(temporary != 0);      // Exchange strings      strcpy(temporary, first);      strcpy(first, second);      strcpy(second, temporary);      // Delete temporary string      delete [ ] temporary;  Example 2 .. 2 }  18  Lập trình hướng đối tượng – Templates
  19. Class templates • A class template is a pattern for creating a family of similar classes. • If you need two or more classes with identical members that differ only in their parameter types, you can write a class template that the compiler will use to generate the definitions  Lập trình hướng đối tượng – Templates 19
  20. Class templates • A class template has the general form    template    class ClassName      {       // ...     }; • The keyword template indicates that what follows is a class  Lập trình hướng đối tượng – Templates 20
ADSENSE

CÓ THỂ BẠN MUỐN DOWNLOAD

 

Đồng bộ tài khoản
2=>2