Friday, October 22 CS 215 Fundamentals of Programming II - Lecture 25 1
Lecture 25
Log into Linux. Copy files on csserver from
/home/hwang/cs215/lecture25/*.*
Reminder: Homework 6 due on Monday. Questions?
Lecture 25 Log into Linux. Copy files on csserver from - - PowerPoint PPT Presentation
Lecture 25 Log into Linux. Copy files on csserver from /home/hwang/cs215/lecture25/*.* Reminder: Homework 6 due on Monday. Questions? Friday, October 22 CS 215 Fundamentals of Programming II - Lecture 25 1 Outline Template
Friday, October 22 CS 215 Fundamentals of Programming II - Lecture 25 1
Log into Linux. Copy files on csserver from
Reminder: Homework 6 due on Monday. Questions?
Friday, October 22 CS 215 Fundamentals of Programming II - Lecture 25 2
Template functions
Friday, October 22 CS 215 Fundamentals of Programming II - Lecture 25 3
Consider a linear search function for an array of
Analysis
Objects Type Movement Name array of values int [ ] received arr index of lower bound int received first index of upper bound int received target search target int received target index of target int returned position
Friday, October 22 CS 215 Fundamentals of Programming II - Lecture 25 4
Design
Code
int LinearSearch (const int arr[], int first, int last, int target) { for (int position=first; position < last; position++) if (arr[position] == target) // found target return position; return last; }
Friday, October 22 CS 215 Fundamentals of Programming II - Lecture 25 5
Suppose we want to use this function to search
Would have to change the type of arr and
Could use a value_type typedef
int LinearSearch (const value_type arr[], int first, int last, value_type target) { ... }
Friday, October 22 CS 215 Fundamentals of Programming II - Lecture 25 6
But what if we want to use this function for both
C++ allows function overloading, so we could
Then if we want to search an array of Dates, we
What we need is a type parameter (vs. a data
Friday, October 22 CS 215 Fundamentals of Programming II - Lecture 25 7
Template function syntax is:
Note: the textbook uses class rather than
Note: since we do not know if T is an object
Friday, October 22 CS 215 Fundamentals of Programming II - Lecture 25 8
Template functions are instantiated wherever
The type that is plugged into the template is
See files template-examples.cpp,
Friday, October 22 CS 215 Fundamentals of Programming II - Lecture 25 9
Since the compiler must have the full
Friday, October 22 CS 215 Fundamentals of Programming II - Lecture 25 10
In templates.h, write a template function
Add code to the end of template-
Friday, October 22 CS 215 Fundamentals of Programming II - Lecture 25 11
In templates.h, write a template function
Add code to the end of templates-
Friday, October 22 CS 215 Fundamentals of Programming II - Lecture 25 12
In templates.h, write a template function Sort
Add code at the end of template-
Friday, October 22 CS 215 Fundamentals of Programming II - Lecture 25 13
When you have completed the in-class
Submit this exercise to the submission system