Template Functions & Classes

Template Functions & Classes - Lecture 7: Template...

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

View Full Document Right Arrow Icon
1 Lecture 7: PIC 10B Todd Wittman Flexible Parameter Types b 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; } b But what if we don't know ahead of time that x is an int, y is a double, and z is a string? b 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); b But this sux. b A better solution would be to write a single function with flexible parameter types.
Background image of page 1

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

View Full DocumentRight Arrow Icon
2 Sec 16.1 : Template Functions b A template function sets up flexible parameter types. b The type is defined by the call, not the function. b We create our own data type T right above the function: template <typename T> void my_function (T parameter) { . .. b T acts like a dummy data type. b We usually use a single capital letter for templated types. b 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 b Can also create multiple tempated types: template <typename T1, typename T2, . ..> b 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; } b Note the template is part of the function declaration. b 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 b What is the output?
Background image of page 2
3 Be Careful of Different Cases b 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> b The string and IntMatrix will print, because we defined << for our IntMatrix class. b
Background image of page 3

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

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

This note was uploaded on 04/27/2010 for the course PIC 157-051-20 taught by Professor Wittman during the Winter '10 term at UCLA.

Page1 / 10

Template Functions &amp; Classes - Lecture 7: Template...

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

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