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

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

Đ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

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 template <class T, > returnType functionName(parameterList) { // Body of the function } 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 <class T, > is the template parameter list. 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. 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 template. Lập trình hướng đối tượng – Templates 10 template <class T> void swap ( T& first, T& second) { T temporary = first; first = second; second = temporary; } Example 1 [...]... i . 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. array[0]; for (int i = 0; i <= size - 2; i++) array[i] = array[i + 1]; array[size - 1] = temporary; } Lập trình hướng đối tượng – Templates 15 Making templates available to the compiler • Do

Ngày đăng: 09/07/2014, 00:20

Từ khóa liên quan

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

  • Đang cập nhật ...

Tài liệu liên quan