{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

Lecture 15 - Writing Generic Functions

Lecture 15 - Writing Generic Functions - Lecture20...

Info icon This 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  
Image of page 1

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

View Full Document Right 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
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
Image of page 3

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

View Full Document Right 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 Implies we do not know types until we use the  11/03/ 2011  Lecture  20 CSC 1254,  Fall 2011,  Writing  Generic  Functions 4
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  operator + applicable to the types of ‘x’ and ‘y’ 11/03/ 2011  Lecture  20 CSC 1254,  Fall 2011,  Writing  Generic  Functions 5
Image of page 5

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

View Full Document Right 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
Image of page 6
Median of Unknown Type Generic functions are implemented using 
Image of page 7

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

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

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    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.

    Student Picture

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

  • Left Quote Icon

    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.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    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.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern