Lập trình hướng đối tượng khuân mẫu

40 219 0
Lập trình hướng đối tượng   khuân mẫu

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

Tài liệu này dành cho sinh viên, giáo viên khối ngành công nghệ thông tin tham khảo và có những bài học bổ ích hơn, bổ trợ cho việc tìm kiếm tài liệu, giáo án, giáo trình, bài giảng các môn học khối ngành công nghệ thông tin

Lập trình hướng đối tượng – Templates 1 Chương 5 TEMPLATES Lập trình hướng đối tượng – Templates 2 Tài liệu đọc Lập trình hướng đối tượng – Templates 3 Templates • Function templates • Class templates • Inheriting Template Classes 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.  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 the definitions of the actual functions. Lập trình hướng đối tượng – Templates 6 Function templates • A function template has the general form       Lập trình hướng đối tượng – Templates 7 Function templates (I) • The keyword  indicates that what follows is a function template, not an actual function. • The notation  is the template parameter list. Lập trình hướng đối tượng – Templates 8 Function templates (II) • The keyword  indicates that  is a generic type i.e. a placeholder for a data type used by the function. • The identifier  appears throughout the function definition wherever this type needs to be written. 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 , creating what is known as a generated function. • The act of generating a function is referred to as instantiating the template. Lập trình hướng đối tượng – Templates 10  !"#"  $#% #$% $%   Example 1 [...]... specified takes precedence over the type that is deduced by the 12 Lập trình compiler hướng đối tượng – Templates 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 Lập trình hướng đối tượng – Templates 13 Mixing generic types and actual types... array[i + 1]; array[size - 1] = temporary; } Lập trình hướng đối tượng – Templates 14 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 Overloading a function... exact match 16 Lập trình hướng đối tượng – Templates // 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); Example 2 else maxLength = strlen(second); 17 Lập trình hướng đối tượng – Templates... temporary; Example 2 2 } Lập trình hướng đối tượng – Templates 18 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 of the actual classes Lập trình hướng đối tượng – Templates 19 Class... class template, not an actual class Lập trình hướng đối tượng – Templates 20 Class templates • The notation is the template parameter list • The keyword class indicates that T is generic type i.e a placeholder for a data type used by the class • The identifier T appears throughout the class definition wherever this type needs to be written Lập trình hướng đối tượng – Templates 21 Generated classes... general form className objectName; Lập trình hướng đối tượng – Templates 22 Member functions of a generated class • The member functions of a class template are function templates, and must be written as such • Each member function definition that appears outside the class definition must begin with the prefix template Lập trình hướng đối tượng – Templates 23 Member functions of a... parameterised class name ClassName followed by the scope resolution operator Lập trình hướng đối tượng – Templates 24 // Header file for the Stack class, stack.h #include const int size = 100; template class Stack Example 3 { (stack.h) private: T array[size]; int numItems; Lập trình hướng đối tượng – Templates 25 public: // Constructor Stack() : numItems(0) { } // Pushes an... push(T item); (stack.h) 2 // Pops an item off the stack T pop(); }; Lập trình hướng đối tượng – Templates 26 // Push item onto stack template void Stack :: push( T item) { if (numItems == size) { cout . Lập trình hướng đối tượng – Templates 1 Chương 5 TEMPLATES Lập trình hướng đối tượng – Templates 2 Tài liệu đọc Lập trình hướng đối tượng – Templates 3 Templates • Function. Classes 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.  Lập trình hướng đối. !"#"  $#% #$% $%   Example 1 Lập trình hướng đối tượng – Templates 11    &$'$(% $)*+% ,$+ -%  !&% !,%  .%  Example

Ngày đăng: 23/10/2014, 21:04

Mục lục

  • Chương 5

  • Tài liệu đọc

  • Templates

  • Slide 4

  • Function templates

  • Function templates

  • Function templates (I)

  • Function templates (II)

  • Generated functions

  • Slide 10

  • Slide 11

  • Explicitly specifying the type

  • Instantiating with different types

  • Mixing generic types and actual types

  • Making templates available to the compiler

  • Overloading a function template

  • Slide 17

  • Slide 18

  • Class templates

  • Slide 20

Tài liệu cùng người dùng

Tài liệu liên quan