tailieunhanh - Lecture Data structures and other objects using C++ - Chapter 6: Template functions

Templates are an important part of C++ that allows a programmer to reuse existing code for new purposes. In some sense, templates ensure that you don’t have to continually “reinvent the wheel.” This lecture introduces how to implement and use template functions. The best time for the lecture is just before the students read chapter 6. | Chapter 6 introduces templates, which are a C++ feature that easily permits the reuse of existing code for new purposes. This presentation shows how to implement and use the simplest kinds of templates: template functions. Template Functions CHAPTER 6 Data Structures and Other Objects Templates are an important part of C++ that allows a programmer to reuse existing code for new purposes. In some sense, templates ensure that you don’t have to continually “reinvent the wheel.” This lecture introduces how to implement and use template functions. The best time for the lecture is just before the students read Chapter 6. Here’s a small function that you might write to find the maximum of two integers. int maximum(int a, int b) { if (a > b) return a; else return b; } Finding the Maximum of Two Integers To illustrate template functions, let’s look at this simple function. It would appear as part of a program that needs to find the larger of two integers. The function returns a copy of the larger of its two arguments. Here’s a small function that you might write to find the maximum of two double numbers. int maximum(double a, double b) { if (a > b) return a; else return b; } Finding the Maximum of Two Doubles Suppose your program also needs to find the larger of two double numbers. Then you could add a second function for double numbers. By the way, you may use the same name, maximum, for the two functions. The compiler is smart enough to determine which function your program is using at any given point, by looking at the types of the parameters. Here’s a small function that you might write to find the maximum of two knafns. int maximum(knafn a, knafn b) { if (a > b) return a; else return b; } Finding the Maximum of Two Knafns Suppose your program also has another data type, called knafn. This could be a class that you wrote yourself for some purpose. Anyway, perhaps knafn objects can be compared with the > operation (because the knafn implementation includes an overloaded | Chapter 6 introduces templates, which are a C++ feature that easily permits the reuse of existing code for new purposes. This presentation shows how to implement and use the simplest kinds of templates: template functions. Template Functions CHAPTER 6 Data Structures and Other Objects Templates are an important part of C++ that allows a programmer to reuse existing code for new purposes. In some sense, templates ensure that you don’t have to continually “reinvent the wheel.” This lecture introduces how to implement and use template functions. The best time for the lecture is just before the students read Chapter 6. Here’s a small function that you might write to find the maximum of two integers. int maximum(int a, int b) { if (a > b) return a; else return b; } Finding the Maximum of Two Integers To illustrate template functions, let’s look at this simple function. It would appear as part of a program that needs to find the larger of two integers. The function returns a copy of the .

crossorigin="anonymous">
Đã phát hiện trình chặn quảng cáo AdBlock
Trang web này phụ thuộc vào doanh thu từ số lần hiển thị quảng cáo để tồn tại. Vui lòng tắt trình chặn quảng cáo của bạn hoặc tạm dừng tính năng chặn quảng cáo cho trang web này.