UAF CS 311 - Software Engineering Concepts: Invariants Silently Written & Called Functions

Unformatted text preview:

Software Engineering Concepts: InvariantsSilently Written & Called FunctionsSimple Class ExampleCS 311 Data Structures and AlgorithmsLecture SlidesFriday, September 11, 2009Glenn G. ChappellDepartment of Computer ScienceUniversity of Alaska [email protected]© 2005–2009 Glenn G. Chappellcontinued11 Sep 2009 CS 311 Fall 2009 2Unit OverviewAdvanced C++ & Software Engineering ConceptsMajor Topics: Advanced C++ The structure of a package Parameter passing Operator overloading Silently written & called functions Pointers & dynamic allocation Managing resources in a class Templates Containers & iterators Error handling Introduction to exceptions Introduction to Linked ListsMajor Topics: S.E. Concepts Abstraction Invariants Testing Some principles(part) 11 Sep 2009 CS 311 Fall 2009 3ReviewThe Structure of a PackageA client of a module is code that uses it.Type conversion: take value and return value of another type. Implicit: double d = 4.5 + 3; Explicit: double d = 4.5 + double(3); No conversion: double d = 4.5 + 3.0;3 has type int.To add 3 to 4.5 (which has type double), we must use a type conversion to get a double.3.0, on the other hand, has type double.11 Sep 2009 CS 311 Fall 2009 4ReviewSoftware Engineering Concepts: Abstraction [1/2]Abstraction: Separate the purpose of a module from its implementation. Functional abstraction Data abstractionKey term: Abstract Data Type An abstract data type (ADT) is a collection of data and a set of operations on the data. The implementation is not specified. ADTs will be a major topic of this course.ModuleClientClientClient(defined by the specification)Implementation(hidden from clients andnot part of the abstraction)InterfaceRecall: Function, class, or other unit of code. Generally smaller than a package.11 Sep 2009 CS 311 Fall 2009 5ReviewSoftware Engineering Concepts: Abstraction [2/2]void printIntArray(const int arr[], std::size_t size){for (std::size_t i = 0; i < size; ++i)std::cout << arr[i] << " ";std::cout << std::endl;}Function printIntArray is given an array of ints called “arr” and a size_t called “size”. It executes a for loop in which local size_t variable i is initialized to 0, the loop continues as long as “i < size” evaluates to true, and i is pre-incremented after each loop iteration. Inside the loop, a reference to an item in array arr is retrieved using the bracket operator, with parameter i, and then inserted in cout (using overloaded operator<<), followed by an array of chars containing a blank and a null. After the loop, stream manipulator endl is inserted in cout. The function then terminates.Function printIntArrayprints an array of ints to cout, given the array and its size. Items are separated by blanks, and followed by a blank and a newline.Describe this function, in detail.(Functional)abstraction11 Sep 2009 CS 311 Fall 2009 6ReviewParameter Passing [1/2]*These are problems when we pass objects.**Maybe this is bad. When we want to send changes back to the client (which is a big reason for passing by reference), disallowing const values is a good thing.So, for most purposes, when we pass objects, reference-to-const combines the best features of the other two methods.YES ☺NO YES ☺Allows implicit type conversionsYES ☺NO **YES ☺Allows passing of const valuesYES ☺YES ☺NO *Allows for polymorphismNO ☺NO ☺YES *Makes a copyBy reference-to-constBy referenceBy value11 Sep 2009 CS 311 Fall 2009 7ReviewParameter Passing [2/2]We pass parameters by reference when we want to modify the client’s copy.void addThree(int & theInt){ theInt += 3; }Otherwise, we generally pass: simple types by value. objects by reference-to-const.void func(double d, const MyClass & q);We usually return by value, unless we return an object not local to this function. Return by reference if we return a pre-existing object for the client to modify. Return by reference-to-const if we return a pre-existing object that the client should not modify (in particular, if the object is const).int & arrayLookUp(int theArray[], int index);const int & arrayLookUp(const int theArray[], int index);11 Sep 2009 CS 311 Fall 2009 8ReviewOperator OverloadingOperators can be implemented using global or member functions. Global: the parameters are the operands. Member: first operand is *this, the rest are parameters. Postfix increment & decrement (n++, n--) get a dummy intparameter, to distinguish them from the prefix versions (++n, --n).Implement an operator using a member function, unless you have a good reason not to. Good Reason #1: To allow for implicit type conversions on the first argument. Applies to: non-modifying arithmetic, comparison, and bitwise operators. For example: + - * / % == != < <= > >= Good Reason #2: When you cannot make it a member, because it would have to be a member of a class you cannot modify. Quintessential examples: stream insertion (<<) and extraction (>>).We usually use operators only for operations that happen quickly. One exception: Assignment for container types.11 Sep 2009 CS 311 Fall 2009 9Software Engineering Concepts: InvariantsBasics [1/2]An invariant is a condition that is always true at a particular point in an algorithm.Example Suppose that myArray is an array of int’s with size myArraySize. We wish to set the variable myItem equal to myArray[i], if possible.if (i < 0){errorMessage("Error: i is too small");return;}// Invariant: i >= 0if (i >= myArraySize){errorMessage("Error: i is too large");return;}// Invariant: (i >= 0) && (i < myArraySize)myItem = myArray[i];11 Sep 2009 CS 311 Fall 2009 10Software Engineering Concepts: InvariantsBasics [2/2]We use invariants: To ensure that we are allowed to perform various operations. To remind ourselves of the information that is implicitly known in a program. To document ways in which code can be used. To help us verify that our programs are correct.11 Sep 2009 CS 311 Fall 2009 11Software Engineering Concepts: InvariantsPre & Post [1/3]We are particularly interested in two special kinds of invariants: preconditions and postconditions.A precondition is an invariant at the beginning of a function. The responsibility for making sure the precondition is true rests with the calling code. In practice, a precondition states what must be true for the function to execute properly.A postcondition is an invariant at


View Full Document

UAF CS 311 - Software Engineering Concepts: Invariants Silently Written & Called Functions

Download Software Engineering Concepts: Invariants Silently Written & Called Functions
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Software Engineering Concepts: Invariants Silently Written & Called Functions and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Software Engineering Concepts: Invariants Silently Written & Called Functions 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?