Lecture 15 - Writing Generic Functions

Lecture 15- - Lecture20 HartmutKaiser [email protected] http/www.cct.lsu.edu/hkaiser/fall_2011/csc1254.html 2 CSC1254 Fall2011 Writing Generic

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

View Full Document Right Arrow Icon
Writing Generic Functions Lecture 20 Hartmut Kaiser [email protected] http://www.cct.lsu.edu/˜ hkaiser /fall_2011/csc1254.html  
Background image of page 1

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

View Full DocumentRight Arrow Icon
Programming Principle of the Day Minimize Coupling - Any section of code (code block,  function, class, etc) should minimize the dependencies on  other areas of code.  This is achieved by using shared variables as little as  possible.  Low coupling is often a sign of a well-structured computer  system and a good design, and when combined with high  cohesion, supports the general goals of high readability and  maintainability http://en.wikipedia.org/wiki/Coupling_(computer_programming) 11/03/ 2011  Lecture  20 CSC 1254,  Fall 2011,  Writing  Generic  Functions 2
Background image of page 2
Abstract So far we have concentrated on the abstractions  provided to us by C++ and the Standard library Now we start looking into creating our own  abstractions This lecture talks about generic functions, which  are functions with parameter types that we do not  know until we call the functions The next lectures will focus on abstract data types  and later on object oriented programming 11/03/ 2011  Lecture  20 CSC 1254,  Fall 2011,  Writing  Generic  Functions 3
Background image of page 3

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

View Full DocumentRight Arrow Icon
Generic Functions For all functions we’ve seen so far we knew the  types of its parameters Seems natural, however we have already used  (not written) functions which didn’t have that  property For instance std::find() Takes two iterators and a value Usable for any appropriate type for any container 11/03/ 2011  Lecture  20 CSC 1254,  Fall 2011,  Writing  Generic  Functions 4
Background image of page 4
Generic Functions What exactly does it mean to have arguments of  “any appropriate type”? How can we know whether it will work for a given  set of argument types? Two parts to that answer Inside C++: the ways a function uses the  arguments of unknown type constrains that  arguments type Function does x + y, this implies there is a defined  11/03/ 2011  Lecture  20 CSC 1254,  Fall 2011,  Writing  Generic  Functions 5
Background image of page 5

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

View Full DocumentRight Arrow Icon
Generic Functions Two parts to that answer Outside C++: the way the Standards library  constrains the argument types for its functions Iterators: supports a collection of operations with  well defined semantics Function expecting iterators as arguments will use  those in a way relying on the iterator semantics  Writing your own containers implies to write  iterators exposing ‘appropriate’ operators and  semantics 11/03/ 2011  Lecture  20 CSC 1254,  Fall 2011,  Writing  Generic  Functions 6
Background image of page 6
Median of Unknown Type Generic functions are implemented using  template functions
Background image of page 7

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

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

This note was uploaded on 12/29/2011 for the course CSC 1254 taught by Professor Blanks,l during the Fall '08 term at LSU.

Page1 / 29

Lecture 15- - Lecture20 HartmutKaiser [email protected] http/www.cct.lsu.edu/hkaiser/fall_2011/csc1254.html 2 CSC1254 Fall2011 Writing Generic

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

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