Lập trình hướng đối tượng - Chương 5
lượt xem 54
download
Tài liệu tham khảo giáo trình Lập trình hướng đối tượng - Chương 5: Templates
Bình luận(0) Đăng nhập để gửi bình luận!
Nội dung Text: Lập trình hướng đối tượng - Chương 5
- Chương 5 TEMPLATES 1 Lập trình hướng đối tượng – Templates
- Tài liệu đọc 2 Lập trình hướng đối tượng – Templates
- Templates • Function templates • Class templates • Inheriting Template Classes 3 Lập trình hướng đối tượng – Templates
- 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
- 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
- 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
- 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
- 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
- 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
- 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
- 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
- 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
- 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
- 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
- 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 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
- // 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
- // 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
- 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
- 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
CÓ THỂ BẠN MUỐN DOWNLOAD
-
Lập trình hướng đối tượng với C++ part 5
35 p | 345 | 162
-
Phân tích thiết kế hướng đối tượng (phần 5)
3 p | 291 | 143
-
Lập trình hướng đối tượng_ Chapter 5
9 p | 245 | 121
-
Tự học lập trình hướng đối tượng và lập trình cơ sở dữ liệu C part 5
40 p | 237 | 116
-
Giáo trình Lập trình viên mã nguồn mở PHP (Module 1) - Trung tâm tin học ĐH KHTN
100 p | 264 | 80
-
Bài giảng Ôn tập Lập trình hướng đối tượng - GV. Dương Khai Phong
41 p | 475 | 78
-
Giáo trình Lập trình viên mã nguồn mở PHP (Module 2) - Trung tâm tin học ĐH KHTN
101 p | 217 | 73
-
Lập trình hướng đối tượng trong java (Phần 5)
6 p | 179 | 54
-
Bài Giảng Phân tích thiết kế hướng đối tượng (phần 5)
32 p | 185 | 22
-
Bài giảng Lập trình ứng dụng Visualbasic: Bài 5 - Phạm Đình Sắc
19 p | 106 | 11
-
Bài giảng Lập trình hướng đối tượng - Bài 5: Một số kỹ thuật Java nâng cao
47 p | 81 | 8
-
Bài giảng Phương pháp lập trình hướng đối tượng: Bài tập 5 - Trần Phước Tuấn
4 p | 114 | 6
-
Bài giảng C# và môi trường Donet - Bài 5: Lớp và phương thức
22 p | 60 | 4
-
Bài giảng Lập trình hướng đối tượng: Bài 5 - Các kỹ thuật xây dựng lớp và sử dụng đối tượng
79 p | 28 | 4
-
Bài giảng Lập trình hướng đối tượng - Bài 5: Kết tập và kế thừa
0 p | 73 | 3
-
Bài giảng Lập trình hướng đối tượng: Bài 5 - Nguyễn Khanh Văn
0 p | 61 | 2
-
Bài giảng Lập trình hướng đối tượng: Bài 5 - ThS. Trịnh Thành Trung
47 p | 43 | 2
-
Bài giảng Nguyên lý lập trình hướng đối tượng: Bài 5 - TS. Lý Anh Tuấn
37 p | 23 | 2
Chịu trách nhiệm nội dung:
Nguyễn Công Hà - Giám đốc Công ty TNHH TÀI LIỆU TRỰC TUYẾN VI NA
LIÊN HỆ
Địa chỉ: P402, 54A Nơ Trang Long, Phường 14, Q.Bình Thạnh, TP.HCM
Hotline: 093 303 0098
Email: support@tailieu.vn