Lecture4 - ADTImplementations:Templatesand StandardContainers Dr.YingwuZhu 07:03:31AM 07:03:31AM 1 Contents ThevectorContainer Iterators 07:03:31AM

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

View Full Document Right Arrow Icon
07:03:31 AM  07:03:31 AM   1 ADT Implementations: Templates and  Standard Containers Dr. Yingwu Zhu
Background image of page 1

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

View Full DocumentRight Arrow Icon
07:03:31 AM  07:03:31 AM   2 Contents Function Genericity – overloading and templates Class Genericity – templates The vector Container Iterators
Background image of page 2
07:03:31 AM  07:03:31 AM   3 Function Genericity: Overloading and  Templates Initially code was reusable by encapsulating it within  functions Example lines of code to swap values stored in two  variables Instead of rewriting those 3 lines Place in a function { int temp = first; first = second; second = temp; } Then call swap(x,y);
Background image of page 3

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

View Full DocumentRight Arrow Icon
07:03:31 AM  07:03:31 AM   4 Function Genericity: Overloading and  Templates To swap variables of different types, write another function Overloading  allows functions to have  same  name Signatures  (types and numbers of parameters) keep them unique to the  compiler Problem:  this could lead to a library of swap functions One function for each standard type Compiler chooses which to use from signature But… What about  user-defined types ?
Background image of page 4
07:03:31 AM  07:03:31 AM   5 Function Templates In overloading, note how  similar  the 3  swap()  function in  p.449-p450 Difference: the place where the type is specified Question: can we pass the type somehow? Yes!  You can! Templates make this possible… Declare functions that receive  both  data and types via parameter  Thus, codes become more generic Easier to reuse
Background image of page 5

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

View Full DocumentRight Arrow Icon
07:03:31 AM  07:03:31 AM   6 Template Mechanism Declare a type parameter (or type placeholder) Use it in the function instead of a specific type This requires a different kind of parameter list: {   ________ temp  = first;   first = second;   second = temp; }
Background image of page 6
07:03:31 AM  07:03:31 AM   7 Template Mechanism (p.454) template ” is a C++ keyword Specify that what follows is pattern  for a function NOT  a function definition “Normal” parameters within function parentheses Type parameters within template brackets ( <> )
Background image of page 7

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

View Full DocumentRight Arrow Icon
07:03:31 AM  07:03:31 AM   8 Template Mechanism Attention : A function template cannot be split across files Specification and implementation must be in the same file A function template is a  pattern Describes how specific functions are constructed Constructed based on the actual type ( instantiation ) Type parameter said to be “bound” to the actual type passed to it for  each instantiation
Background image of page 8
07:03:31 AM  07:03:31 AM   9 Template Mechanism Each of the type parameters must  appear at least once  in  parameter list of the function
Background image of page 9

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

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

This note was uploaded on 03/01/2011 for the course CSSE 250 taught by Professor Dr.yingwuzhu during the Fall '11 term at UH Clear Lake.

Page1 / 40

Lecture4 - ADTImplementations:Templatesand StandardContainers Dr.YingwuZhu 07:03:31AM 07:03:31AM 1 Contents ThevectorContainer Iterators 07:03:31AM

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

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