Lec16Templates_6per.pdf

# Lec16Templates_6per.pdf - EECS 402 Consider This Program...

• 3

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

1 The University Of Michigan Andrew M. Morgan EECS402 Lecture 16 Savitch Ch. 16 Templates EECS 402 EECS 402 Andrew M Morgan 2 Consider This Program int main(void) { int i1 = 5, i2 = 8, i3, i4; float f1 = 19.2, f2 = -2.3, f3, f4; char c1 = 'h', c2 = 'p', c3, c4; BlipClass b1(6, 9), b2(7, 7), b3, b4; i3 = lesser(i1, i2); i4 = greater(i1, i2); f3 = lesser(f1, f2); f4 = greater(f1, f2); c3 = lesser(c1, c2); c4 = greater(c1, c2); b3 = lesser(b1, b2); b4 = greater(b1, b2); cout << "Ints: " << i3 << " " << i4 << endl; cout << "Floats: " << f3 << " " << f4 << endl; cout << "Chars: " << c3 << " " << c4 << endl; cout << "Blips: " << b3 << " " << b4 << endl; return (0); } Ints: 5 8 Floats: -2.3 19.2 Chars: h p Blips: blip: 7 7 blip: 6 9 EECS 402 EECS 402 Andrew M Morgan 3 Functions We Need float lesser( float v1, float v2) { float res; if (v1 < v2) res = v1; else res = v2; return (res); } float greater( float v1, float v2) { float res; if (v1 > v2) res = v1; else res = v2; return (res); } int lesser( int v1, int v2) { int res; if (v1 < v2) res = v1; else res = v2; return (res); } int greater( int v1, int v2) { int res; if (v1 > v2) res = v1; else res = v2; return (res); } char lesser( char v1, char v2) { char res; if (v1 < v2) res = v1; else res = v2; return (res); } char greater( char v1, char v2) { char res; if (v1 > v2) res = v1; else res = v2; return (res); } BlipClass lesser( BlipClass v1, BlipClass v2) { BlipClass res; if (v1 < v2) res = v1; else res = v2; return (res); } BlipClass greater( BlipClass v1, BlipClass v2) { BlipClass res; if (v1 > v2) res = v1; else res = v2; return (res); } EECS 402 EECS 402 Andrew M Morgan 4 What A Pain Eh? Notice that all the functions are essentially the same The only thing that changes is the data type that the function performs on Wouldn't it be nice if we could use a variable to describe the type? Obviously this can not be done, because we would have some sort of assignment like "myType = float;" This is obviously NOT valid C++ syntax However, we can achieve similar functionality using templates in C++ EECS 402 EECS 402 Andrew M Morgan 5 Introduction To Templates A template is a special construct in C++ that allows multiple types to be operated on by a single implementation of a function Think of a template as describing an algorithm Since the operation we are performing is performed the same way for all different data types, it is sufficient to give code to implement an algorithm without being specific to a data type The word template is a keyword in C++ A function can be "templated" - as would be the case for the lesser() and greater() functions A class can also be "templated" A templated function or class can have different data types that are left unspecified In other words, multiple types can be templated EECS 402 EECS 402 Andrew M Morgan 6 Templates, Example template < class T > T lesser(T val1, T val2) { T res; if (val1 < val2) res = val1; else res = val2; return (res); } template < class T > T greater(T val1,

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

This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### What students are saying

• As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

Kiran Temple University Fox School of Business ‘17, Course Hero Intern

• I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

Dana University of Pennsylvania ‘17, Course Hero Intern

• The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

Jill Tulane University ‘16, Course Hero Intern