Chapter 5: Procedural abstraction Proper procedures and function procedures Abstraction in programming enables distinction: What a program unit does How a program unit works This enables separation of concerns Procedural abstraction concerned with simple program units proper procedures and function procedures parameters and arguments A procedure is an entity that embodies a computation a function procedure embodies an expression to be evaluated a proper procedure embodies a command to be executed Methods in OO languages are procedures but closely related to classes / Faculteit Wiskunde en Informatica 30-10-2008 PAGE 0 / Faculteit Wiskunde en Informatica 30-10-2008 PAGE 1 Function procedures Function procedures A function procedure evaluates an expression and yields a value as result In C/C++: T I (FPD 1,, FPD n ) B where: T is result type I is function s identifier FPD i are formal parameter declarations B is a block command (also called body), B must contain at least one statement of the form return E; Call via I(AP 1,, AP n ) where: AP i are actual parameters In C++ without recursion: float power(float x, int n) { float p = 1.0; for (int i = 1; i <= n; i++) { p *= x }; return p; } In C++ with recursion float power(float x, int n) { if (n == 0) { return 1.0 } else { return x * power(x, n-1); } } In Haskell power(x : Float, n: Int) = if n = 0 then 1.0 else x * power(x, n-1) / Faculteit Wiskunde en Informatica 30-10-2008 PAGE 2 / Faculteit Wiskunde en Informatica 30-10-2008 PAGE 3
Function procedures Proper procedures Function s body is a block command: side effects are possible return value is not guaranteed A proper procedure embodies a command to be executed and will update variables In C/C++: void I(FPD 1,, FPD n ) B where: I is function s identifier FPD i are formal parameter declarations B is a block command (also called body), Call via I(AP 1,, AP n ) where: AP i are actual parameters / Faculteit Wiskunde en Informatica 30-10-2008 PAGE 4 / Faculteit Wiskunde en Informatica 30-10-2008 PAGE 5 Abstraction principle Parameters and arguments A function procedure abstracts over an expression A proper procedure abstracts over a command An argument is a value or other entity that is passed to a procedure Abstraction principle: It is possible to design procedures that abstract over any syntactic category, provided that the constructs in that syntactic category specify some kind of computation An actual parameter is an expression that yields an argument A formal parameter is an identifier through which a procedure can access an argument / Faculteit Wiskunde en Informatica 30-10-2008 PAGE 6 / Faculteit Wiskunde en Informatica 30-10-2008 PAGE 7
Parameters and arguments Parameters and arguments Possible arguments passed as parameter: First-class values variables or pointers to variables pointers to procedures (in C/C++) Association between formal parameter and argument is called parameter mechanism value parameters result parameters value-result parameters constant parameters variable parameters procedural parameters functional parameters / Faculteit Wiskunde en Informatica 30-10-2008 PAGE 8 / Faculteit Wiskunde en Informatica 30-10-2008 PAGE 9 Parameters and arguments Copy parameter mechanisms Association between formal parameter and argument is called parameter mechanism two basic concepts: copy parameter mechanism reference parameter mechanism Copy parameter mechanism allows the transfer of values to and from a procedure A formal parameter FP denotes a local variable of the procedure C, C++ and Java support only copy-in parameters / Faculteit Wiskunde en Informatica 30-10-2008 PAGE 10 / Faculteit Wiskunde en Informatica 30-10-2008 PAGE 11
Copy parameter mechanisms Reference parameter mechanisms 3 mechanisms: Copy-in parameter (value parameter): creation: local variable is created and initialized with argument value execution: local variable may be inspected and updated return: no effect to outside world Copy-out parameter (result parameter): argument must be a variable creation: local variable is created but not initialized return: final value is assigned to the argument variable Copy-in-copy-out parameter (value-result parameter) combination of copy-in and copy-out A reference parameter allows for the formal parameter FP to be bound directly to the argument Forms of reference parameters: constant parameter: argument must be a value inspection of FP is indirect inspection of argument value variable parameter: argument must be a variable inspection/update of FP is indirect inspection/update of argument variable procedural parameter: argument must be a procedure Call to FP is an indirect call to the argument procedure / Faculteit Wiskunde en Informatica 30-10-2008 PAGE 12 / Faculteit Wiskunde en Informatica 30-10-2008 PAGE 13 Reference parameter mechanisms Reference vs copy parameter C does not support reference parameters, but it can be achieved via variable parameters using pointers Function with parameter of type T* The corresponding argument must be a pointer to variable of type T Caller obtains pointer to any variable via expression &V C++ supports variable parameters directly Choice between reference and copy parameter mechanism is an important language design decision: Reference parameters have simpler semantics: suitable for all types of value indirect access to argument data more efficient for composite types (in general) Copy parameters more efficient for primitive types Variable parameters lead to aliasing: two or more identifiers are bound simultaneously to the same variable / Faculteit Wiskunde en Informatica 30-10-2008 PAGE 14 / Faculteit Wiskunde en Informatica 30-10-2008 PAGE 15
Chapter 6: Data abstraction Program units: packages and encapsulation Decomposition in program units: packages group together declarations of several components encapsulation; components of package may be public or private abstract types: private representations, but public operations classes subclasses and inheritance A program unit: a named part of program that can be designed and implemented independently single purpose simple application program interface modifiable reusable Application program interface (API): minimum of information of application developer interface specification of behavior no algorithmic details / Faculteit Wiskunde en Informatica 30-10-2008 PAGE 16 / Faculteit Wiskunde en Informatica 30-10-2008 PAGE 17 Packages Abstract types Package is a group of components declared for a common purpose: types constants variables procedures Encapsulation: public components visible outside the package private components only visible inside the package An abstract type is a type whose identifier is public but whose representation is private a group of operations to access the representation procedures constants values of the abstract type are only the variables generated by the operations only properties can be observed by application code using operations Very Ada specific / Faculteit Wiskunde en Informatica 30-10-2008 PAGE 18 / Faculteit Wiskunde en Informatica 30-10-2008 PAGE 19
Abstract types Objects and classes Defining abstract types involves more work: representation is private, thus sufficient operations to generate and process all values classification of operations: constructors for creation of a new value of type T but not using existing values of T transformers for computation of a value of type T using existing values of T accessors for computation of a value of some type using existing values of T An object is a group of variable components with a group of operations to access these variables A class is a set of similar objects supported by all object-oriented languages a variable component is instance variable member variable an operation is constructor method / Faculteit Wiskunde en Informatica 30-10-2008 PAGE 20 / Faculteit Wiskunde en Informatica 30-10-2008 PAGE 21 Objects and classes Objects and classes A constructor creates a new object of the class A method inspects and/or updates an existing object of the class Components of an object are private public A method call names a method and identifies a target object on which the method will operate O.M(E 1,, E n ) object O must have method M, otherwise type error inside a method body this denotes the target object Constant components are often public Variable components should always be private (loss of encapsulation) Constructors and methods are public unless only used the class declaration / Faculteit Wiskunde en Informatica 30-10-2008 PAGE 22 / Faculteit Wiskunde en Informatica 30-10-2008 PAGE 23
Subclasses and inheritance Subclasses and inheritance A subclass of C is a set of objects similar to class C but richer: extra variable components extra methods If S is a subclass of C then C is a superclass of S Private components are only visible in its own class Public components are everywhere visible Protected components are visible in its own class and any subclass A subclass inherits its superclass s variable components and methods A subclass may override methods of its superclass / Faculteit Wiskunde en Informatica 30-10-2008 PAGE 24 / Faculteit Wiskunde en Informatica 30-10-2008 PAGE 25 Subclasses and inheritance Subclasses and inheritance Methods of a superclass may be inherited or overridden: default behavior in Java is that methods are inherited Overridden means the subclass provides a method with the same identifier, parameter types and result type but different behavior A method is overridable is a subclass is allowed to override it: In Java not declared as final In C++ if method is declared as virtual Overriding may lead to dynamic dispatching at compile-time it is impossible to determine which version of the method has to be called run-time determination of class and method Dynamic dispatching is needed when named method is overridable and exact class of target object at compile-time is unknown some subclasses inherit some method M, whereas other override method M / Faculteit Wiskunde en Informatica 30-10-2008 PAGE 26 / Faculteit Wiskunde en Informatica 30-10-2008 PAGE 27
Abstract classes Single vs multiple inheritance An abstract class is a class in which no objects can be constructed it may have variable components and methods no constructor some of its methods may be undefined (abstract methods) It serves as superclass abstract methods have to be defined by all subclasses Single inheritance: each class has at most one superclass Multiple inheritance: a class may have any number of superclasses variable components and methods are inherited from all superclasses Java supports single inheritance via extends clause C++ supports multiple inheritance / Faculteit Wiskunde en Informatica 30-10-2008 PAGE 28 / Faculteit Wiskunde en Informatica 30-10-2008 PAGE 29 Single vs multiple inheritance Interfaces Conceptual difficulty of multiple inheritance: class C defines a method named M subclass C1 (of C) overrides method M subclass C2 (of C) overrides method M which method to use in subclass C3? (inheriting both C1 and C2) Possible solutions: Call method M of C Be explicit in the class declaration C3 which method M to use Be explicit in the method call which method of M to use Prohibit such a method call: C++ solution An interface declares (but not defines) operations that other program unit(s) must define Java uses an interface to declare abstract methods to be defined other classes Interfaces can be used for multiple inheritance / Faculteit Wiskunde en Informatica 30-10-2008 PAGE 30 / Faculteit Wiskunde en Informatica 30-10-2008 PAGE 31
Ch. 7: Generic abstraction Generic units and instantiation Reusable program units are applicable in a variety of applications stack queue list set such program units are generic wrt the data the process A generic unit is a program unit parameterized wrt entities on which it depends Instantiation of a generic unit generates an ordinary program unit Generic units can be instantiated more than once prevents code duplication / Faculteit Wiskunde en Informatica 30-10-2008 PAGE 32 / Faculteit Wiskunde en Informatica 30-10-2008 PAGE 33 Generic units and instantiation Generic units and instantiation A procedure is an abstraction over an expression A generic unit is an abstraction over a declaration Generic units are supported by: C++ Ada Java (since 2004 and only types) C++ generic class templates template <int capacity> class Queue { private: char elems[capacity]; int front, rear, length; public: Queue(); void add (char e); char remove(); } / Faculteit Wiskunde en Informatica 30-10-2008 PAGE 34 / Faculteit Wiskunde en Informatica 30-10-2008 PAGE 35
Generic units and instantiation Generic units and instantiation C++ generic function templates template <int capacity> Queue<capacity>::Queue() { front = rear = length } template <int capacity> void Queue<capacity>::add(char e) { elems[rear] = e; rear = (rear+1) % capacity; length++; } template <int capacity> void Queue<capacity:: remove() { } Every constructor and method definition must be prefixed by template <int capacity> Instantiation of generic classes via: Typedef Queue<80> Input_Buffer; Typedef Queue<120> Line_Buffer; Declaration of variables of type Input_Buffer and Line_Buffer: Input_Buffer inbuf; Line_Buffer outbuf; Line_Buffer errbuf; Alternatively: Queue<80> inbuf; Queue<120>outbuf; Queue<120>errbuf; / Faculteit Wiskunde en Informatica 30-10-2008 PAGE 36 / Faculteit Wiskunde en Informatica 30-10-2008 PAGE 37 Generic units and instantiation A conceptual problem with C++ generic classes: Type equivalence: outbuf and errbuf are equivalent because both are derived from Queue<120> Queue<m> and Queue<n-1> are not equivalent arguments must be evaluated at compile-time A pragmatic problem with C++ generic classes: if Queue<120> occurs multiple times, multiple instance may be generated by a simple-minded compiler / Faculteit Wiskunde en Informatica 30-10-2008 PAGE 38