DOC PREVIEW
UCLA PIC 10B - Lec7

This preview shows page 1-2-3 out of 10 pages.

Save
View full document
Premium Document
Do you want full access? Go Premium and unlock all 10 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

Lecture 7 Template Functions Classes PIC 10B Todd Wittman Flexible Parameter Types In general when we write a function we have to know the type of each parameter we receive void print int x double y string z cout x x ny y nz z return But what if we don t know ahead of time that x is an int y is a double and z is a string One solution is to overload the function many different ways void print int x int y int z void print string x int y double z void print double x double y int z But this sux A better solution would be to write a single function with flexible parameter types 1 Sec 16 1 Template Functions A template function sets up flexible parameter types The type is defined by the call not the function We create our own data type T right above the function template typename T void my function T parameter T acts like a dummy data type We usually use a single capital letter for templated types Then my function can receive different parameter types my function 2 T is int my function 3 14 T is double my function yoda T is string my function false T is bool my function myProduct T is Product Template Functions Can also create multiple tempated types template typename T1 typename T2 So our print function could be rewritten template typename A typename B typename C void print A x B y C z cout x x ny y nz z return Note the template is part of the function declaration The type is now defined by the function call string s Yoda IntMatrix M 3 4 int v 42 print s M v A is string B is IntMatrix C is int What is the output 2 Be Careful of Different Cases In order for the print function to work it assumes that the output push is defined for each type A B C string s Yoda IntMatrix M 3 4 vector int v 10 print s M v A is string B is IntMatrix C is vector int The string and IntMatrix will print because we defined for our IntMatrix class But trying to print the vector will give a compile error We can print an element v i but trying to print v cout v doesn t make sense If we re worried about the last parameter z being a vector we could write a special version of print to handle it Overloading Template Functions Overload the print function with another function where the 3rd parameter z is an integer vector template typename A typename B void print A x B y const vector int z cout x x ny y nz for int i 0 i z size i cout z i return If the compiler has a choice it will choose the matching function with the fewest templated types Because each function can be templated differently the template command only works for the next function 3 Template Function Example template typename T void printMin ostream out T first T second out The minimum is if first second out first n else out second n return Sample calls printMin cout 2 4 3 89 printMin fout Yoda chewbacca printMin cout true 1 2 Can we use this on our own class Product IntMatrix Templates Are Local The template type definition is just for that one function This means you have to include the template line for every function that uses it not just for the first function Note we declared T twice template typename T in same program but void print T x that s OK for template types cout x Without this line you template typename T void read T y cin y would get a compile error saying T is undeclared identifier 4 Vector Templates We can also template the type of a vector template typename T void printVector const vector T v for int i 0 i v size i cout i v i n return This function will print out any type vector assuming T is printable Note that we can use the same template type in multiple parameters template typename T vector T merge vector T A vector T B Sec 16 3 Template Classes We can use templates to design general purpose classes We ve actually already seen this in action with the vector class vector double v 10 v 3 42 3 vector string s 20 s 3 Yoda For example this week you made an IntMatrix class to store a matrix of integers It would be awesome if we could design just one class Matrix to hold a matrix of integers doubles bools strings or whatever we want Matrix double D 3 4 D 1 2 42 3 Matrix string S 3 4 S 1 2 Yoda By the way this is next week s HW assignment 5 Template Classes List all template types before the class declaration template typename T1 typename T2 class ClassName public functions private data When defining a member function write the template line above each function Copy and paste Also list all types in after the class name template typename T1 typename T2 returnType ClassName T1 T2 function name params When you call the constructor tell it what types you want Just like we do with the vector class ClassName int string myObject Template Class Pair Write a class Pair that stores two variables possibly of different types Pair int int myPair1 1 3 Pair int double myPair2 1 3 14 Pair int string myPair3 1 Yoda We can then print the variables using the accessor functions cout myPair get first cout myPair get second Define component wise addition 1 3 2 4 3 7 6 Template Class Pair template typename F typename S class Pair public Pair const F a const S b F get first const S get second const Pair F S operator const Pair F S right private F first S second What s missing here from the Big 4 Is that OK Template Class Pair template typename F typename S Pair F S Pair const F a const S b first a Every function needs second b the template above it template typename F typename S F Pair F S get first const return first Note that the templated types are placed with class name Pair F S template typename F typename S S Pair F S get second const return second 7 Template Class Pair template typename F typename S Pair F S Pair F S operator const Pair F S right Pair F S newPair first right first second right second return newPair Note we have to specify the templated types F S in many different places Think of Pair F S as a specific data type For example vector int and vector string are two very different types What data types F S will this work for For practice can you re write operator as a non member function As a friend function Template Class Pair Example code using the Pair class include iostream …


View Full Document

UCLA PIC 10B - Lec7

Download Lec7
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Lec7 and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Lec7 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?