Lecture 15 - Writing Generic Functions

Lecture 15 - Writing Generic Functions - 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 hkaiser@cct.lsu.edu 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 ▫ Implies we do not know types until we use the functions This is called a Generic Function ▫ Key feature of C++ 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 operator + applicable to the types of ‘x’ and ‘y’ Implementation checks whether x + y is defined, and if yes, types of ‘x’ and ‘y’ are ‘appropriate’ 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 ▫ Single definition for a family of functions (or types) that behave similarly ▫ Types are parameters, relies on knowledge that different types still have common properties and behavior Template functions are written in terms of this
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 - Writing Generic Functions - 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