Function and class templates EECE.3220 Data Structures Instructor: Dr. Michael Geiger Spring 2017 Lecture 24: Function and class templates Recursion intro
Data Structures: Lecture 24 Lecture outline Announcements/reminders Program 4 still to be posted; due at some point before 4/28 Exam 2 in class Friday, 3/31 Will be allowed one 8.5” x 11” double-sided note sheet Today’s material will not be on exam—exam coverage stops with queues Today’s lecture Review: Queue implementations Function and class templates Recursion intro 11/10/2018 Data Structures: Lecture 24
Review: queue implementations Array-based queues Treat array as circular so you can add new items to lowest-indexed positions as original values in those positions are dequeued Two possible solutions Only store front/back and leave 1 array slot empty Store front/back & extra “empty” variable Linked queues Front pointer points to first node in queue Back pointer points to last node in queue Could use circular linked list with pointer only to last node (back = last node; front = (last node)->next 11/10/2018 Data Structures: Lecture 24
Review: Justifying templates Want to write general code we can use in specific cases General functions that work for different data types General classes that can store different data types 11/10/2018 Data Structures: Lecture 24
Data Structures: Lecture 24 Templates Templates allow us to write general functions/classes and specify the data type later template keyword indicates that what follows is a pattern, not a full definition Generic typename specified in angle brackets with typename (or class) keyword At least one templated parameter must be used Desired type automatically bound if calling function Desired type must be specified if declaring object 11/10/2018 Data Structures: Lecture 24
Function template example General form: template <typename type> function definition Rewriting swap with templates: template <typename T> void swap(T &v1, T &v2) { T temp = v1; v1 = v2; v2 = temp; } Calling swap: int x = 10; int y = 20; swap(x, y); 11/10/2018 Data Structures: Lecture 24
Data Structures: Lecture 24 Class templates Specify template outside of class definition Can then use templated type(s) inside definition All member functions must be declared as template functions Template specification and implementation cannot be split Function definitions listed in .h file 11/10/2018 Data Structures: Lecture 24
Class template example: array-based queue template <typename T> class AQueue { public: AQueue(); bool isEmpty(); void enqueue(T val); void dequeue(); T front(); void display(ostream &out); private: T Qarr[CAPACITY]; int Qfront, Qback; }; 11/10/2018 Data Structures: Lecture 24
Class template example: enqueue template <typename T> void AQueue<T>::enqueue(T val) { int newBack = (Qback + 1) % CAPACITY; if (newBack != Qfront) { Qarr[newBack] = val; Qback = newBack; } else cerr << "Queue is full--can't enqueue\n"; 11/10/2018 Data Structures: Lecture 24
Class template example: objects Specify desired types of storage after typename Two different instances of AQueue: AQueue <int> intQ; AQueue <double> doubleQ; 11/10/2018 Data Structures: Lecture 24
Data Structures: Lecture 24 Recursion Some functions call other functions Recursive functions call themselves A recursive function has two parts Anchor / base case: function value is defined for one or more specific argument values Inductive / recursive case: function value for current argument value defined in terms of previously defined function values and/or arguments 11/10/2018 Data Structures: Lecture 24
Data Structures: Lecture 24 Recursive example Recursive power function double power(double x, unsigned n){ if (n == 0) return 1.0; else return x * power(x, n – 1); } 11/10/2018 Data Structures: Lecture 24
Data Structures: Lecture 24 Recursion downsides Space efficiency Every function needs a stack frame/activation record Computational efficiency Recursive solution may not be as efficient Example: Fibonacci sequence (1, 1, 2, 3, 5, 8 …) Recursive function to find nth Fibonacci number int fib(unsigned n) { if (n <= 2) return 1; else return fib(n-1) + fib(n-2); } 11/10/2018 Data Structures: Lecture 24
Data Structures: Lecture 24 Common recursive uses Use if, on each iteration, problem splits into 2 or more similar tasks Don’t want to repeat work Graph/tree traversal Will see this with binary trees Divide and conquer algorithms Search, sort algorithms very common examples Think about binary search Test value at midpoint of array If higher than value you’re searching for, test lower half If lower than value you’re searching for, test upper half 11/10/2018 Data Structures: Lecture 24
Data Structures: Lecture 24 Final notes Next time: Exam 2 Preview Reminders: Program 4 still to be posted; due date TBD Exam 2 in class Friday, 3/31 Will be allowed one 8.5” x 11” double-sided note sheet Today’s material will not be on exam—exam coverage stops with queues 11/10/2018 Data Structures: Lecture 24