lecture35-apr27 - Lecture 35 Announcements Read Ch. 19...

Info iconThis preview shows pages 1–2. Sign up to view the full content.

View Full Document Right Arrow Icon
Lecture 35 Announcements • Read Ch. 19 • Assignment 7 due on Friday; several clarifications sent • Sample exam 3 on BB later this week – Exam 3 on Friday, May 8 in class – No final • Topics of the day – Generics InterestAcct BankAccount IS-A Generics car selectionSort – from previously void selectionSort ( double array [ ], int size) { // this function sorts the given array into descending order int max; // will hold the index of the largest value found for (int i=0; i < size; i++) { max = findMaximum (array, i, size); swap(array, i, max); } } int findMaximum ( double array [ ], int i, int size) { // this function returns the index of the largest value in array int j, max = i; for (j= i + 1; j < size; j++) if (array[j] > array[max]) max =j; return max; } void swap ( double array [ ], int i, int j) { // this function swaps the 2 values at positions i and j in array double temp = array[i]; array[i] = array[j]; array[j] = temp; } What if we could parameterize these functions with a generic data type (say T ) instead of using double ? Template Function Syntax • template function syntax includes the keyword template followed by a non-empty list of formal types enclosed in angle brackets, followed by the function definition. • In the argument list, each type is preceded by the keyword typename , and types are separated by commas. template <typename T, typename U, typename V, . ..> selectionSort – from previously void selectionSort ( T array [ ], int size) { // this function sorts the given array into descending order int max; // will hold the index of the largest value found for (int i=0; i < size; i++) { max = findMaximum (array, i, size); swap(array, i, max); } } int findMaximum ( T array [ ], int i, int size) {// this function returns the index of the largest value in array int j, max = i; for (j= i + 1; j < size; j++) if (array[j] > array[max]) max =j; return max; } void swap ( T array [ ], int i, int j) { // this function swaps the 2 values at positions i and j in array T temp = array[i]; array[i] = array[j]; array[j] = temp; } template <typename T> selectionSort – calling fcn
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 2
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 01/24/2010 for the course EE 312 taught by Professor Shafer during the Spring '08 term at University of Texas at Austin.

Page1 / 3

lecture35-apr27 - Lecture 35 Announcements Read Ch. 19...

This preview shows document pages 1 - 2. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online