Chapter 5: Procedural abstraction. Function procedures. Function procedures. Proper procedures and function procedures

Similar documents
Ch. 7: Generic abstraction. Generic units and instantiation. Generic units and instantiation. Generic units and instantiation

Questions? Static Semantics. Static Semantics. Static Semantics. Next week on Wednesday (5 th of October) no

Graphical Interface and Application (I3305) Semester: 1 Academic Year: 2017/2018 Dr Antoun Yaacoub

Chapter 6 Introduction to Defining Classes

Short Notes of CS201

CS201 - Introduction to Programming Glossary By

Programming II (CS300)

G Programming Languages - Fall 2012

Data Abstraction. Hwansoo Han

Design issues for objectoriented. languages. Objects-only "pure" language vs mixed. Are subclasses subtypes of the superclass?

AP COMPUTER SCIENCE JAVA CONCEPTS IV: RESERVED WORDS

Zhifu Pei CSCI5448 Spring 2011 Prof. Kenneth M. Anderson

Introduction to Programming Using Java (98-388)

Weiss Chapter 1 terminology (parenthesized numbers are page numbers)

What are the characteristics of Object Oriented programming language?

10. Abstract Data Types

Concepts of Programming Languages

Casting -Allows a narrowing assignment by asking the Java compiler to "trust us"

Informatica 3 Syntax and Semantics

Chapter 5 Object-Oriented Programming

G Programming Languages Spring 2010 Lecture 4. Robert Grimm, New York University

C++ Important Questions with Answers

Code Generation & Parameter Passing

Programming Languages & Paradigms PROP HT Course Council. Subprograms. Meeting on friday! Subprograms, abstractions, encapsulation, ADT

Java Object Oriented Design. CSC207 Fall 2014

Programming II (CS300)

OBJECT ORIENTED PROGRAMMING USING C++ CSCI Object Oriented Analysis and Design By Manali Torpe

COP 3330 Final Exam Review

CSE 504. Expression evaluation. Expression Evaluation, Runtime Environments. One possible semantics: Problem:

CS558 Programming Languages

Paytm Programming Sample paper: 1) A copy constructor is called. a. when an object is returned by value

Chapter 4 Defining Classes I

Introduction to Programming (Java) 4/12

Overview. Elements of Programming Languages. Objects. Self-Reference

Object-Oriented Languages and Object-Oriented Design. Ghezzi&Jazayeri: OO Languages 1

Goal of lecture. Object-oriented Programming. Context of discussion. Message of lecture

JAVA MOCK TEST JAVA MOCK TEST II

CS558 Programming Languages Winter 2018 Lecture 4a. Andrew Tolmach Portland State University

CPS 506 Comparative Programming Languages. Programming Language

Chapter 13 Object Oriented Programming. Copyright 2006 The McGraw-Hill Companies, Inc.

Data Structures (list, dictionary, tuples, sets, strings)

PROGRAMMING LANGUAGE 2

Object-Oriented Design

Subtyping (Dynamic Polymorphism)

F1 A Java program. Ch 1 in PPIJ. Introduction to the course. The computer and its workings The algorithm concept

G Programming Languages Spring 2010 Lecture 8. Robert Grimm, New York University

Organization of Programming Languages CS 3200/5200N. Lecture 09

Implementing Subprograms

Object Oriented Programming. Java-Lecture 11 Polymorphism

The compilation process is driven by the syntactic structure of the program as discovered by the parser

G Programming Languages - Fall 2012

ITI Introduction to Computing II

Programming II (CS300)

Chapter 4: Writing Classes

AP Computer Science Chapter 10 Implementing and Using Classes Study Guide

Object-Oriented Programming

Outline. Java Models for variables Types and type checking, type safety Interpretation vs. compilation. Reasoning about code. CSCI 2600 Spring

Chapter 11. Categories of languages that support OOP: 1. OOP support is added to an existing language

Outline. Object Oriented Programming. Course goals. Staff. Course resources. Assignments. Course organization Introduction Java overview Autumn 2003

Compilation of Object Oriented Languages Tik Compilers Seminar

Chapter 11. Abstract Data Types and Encapsulation Concepts ISBN

Abstract Data Types and Encapsulation Concepts

Programming II (CS300)

Object Oriented Paradigm

Course Content. Objectives of Lecture 24 Inheritance. Outline of Lecture 24. Inheritance Hierarchy. The Idea Behind Inheritance

Operating Systems 2INC0 C course Pointer Advanced. Dr. Ir. Ion Barosan

Course Content. Objectives of Lecture 24 Inheritance. Outline of Lecture 24. CMPUT 102: Inheritance Dr. Osmar R. Zaïane. University of Alberta 4

CLASSES AND OBJECTS IN JAVA

CSE 307: Principles of Programming Languages

CS412/CS413. Introduction to Compilers Tim Teitelbaum. Lecture 17: Types and Type-Checking 25 Feb 08

ITI Introduction to Computing II

M301: Software Systems & their Development. Unit 4: Inheritance, Composition and Polymorphism

OOPs Concepts. 1. Data Hiding 2. Encapsulation 3. Abstraction 4. Is-A Relationship 5. Method Signature 6. Polymorphism 7. Constructors 8.

CONTENTS. PART 1 Structured Programming 1. 1 Getting started 3. 2 Basic programming elements 17

Object-Oriented Concepts and Principles (Adapted from Dr. Osman Balci)

Lecture Overview. [Scott, chapter 7] [Sebesta, chapter 6]

Java Fundamentals (II)

Interview Questions of C++

Chapter 11. Abstract Data Types and Encapsulation Concepts

Fibonacci in Lisp. Computer Programming: Skills & Concepts (CP1) Programming Languages. Varieties of Programing Language

DATA TYPES. CS 403: Types and Classes DATA TYPES (CONT D)

Introducing Wybe a language for everyone

Object Oriented Programming is a programming method that combines: Advantage of Object Oriented Programming

Semantic Analysis. How to Ensure Type-Safety. What Are Types? Static vs. Dynamic Typing. Type Checking. Last time: CS412/CS413

Semantic Analysis and Type Checking

Java Inheritance. Written by John Bell for CS 342, Spring Based on chapter 6 of Learning Java by Niemeyer & Leuck, and other sources.

From Java to C++ From Java to C++ CSE250 Lecture Notes Weeks 1 2, part of 3. Kenneth W. Regan University at Buffalo (SUNY) September 10, 2009

15CS45 : OBJECT ORIENTED CONCEPTS

Motivation was to facilitate development of systems software, especially OS development.

Java: introduction to object-oriented features

CMSC 4023 Chapter 11

Core Java - SCJP. Q2Technologies, Rajajinagar. Course content

Introduction to C++ Introduction to C++ Dr Alex Martin 2013 Slide 1

Programming Languages: Lecture 11

Storage. Outline. Variables and updating. Copy vs. Ref semantics Lifetime. Dangling References Garbage collection

Chapter 5. Names, Bindings, and Scopes

Overview of OOP. Dr. Zhang COSC 1436 Summer, /18/2017

1 Shyam sir JAVA Notes

Types and Type Inference

4 Bindings and Scope. Bindings and environments. Scope, block structure, and visibility. Declarations. Blocks. 2004, D.A. Watt, University of Glasgow

Transcription:

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