Encapsulation by Subprograms and Type Definitions

Slides:



Advertisements
Similar presentations
OO Programming in Java Objectives for today: Overriding the toString() method Polymorphism & Dynamic Binding Interfaces Packages and Class Path.
Advertisements

Semantic Analysis Chapter 6. Two Flavors  Static (done during compile time) –C –Ada  Dynamic (done during run time) –LISP –Smalltalk  Optimization.
Programming Languages and Paradigms
CSI 3120, Implementing subprograms, page 1 Implementing subprograms The environment in block-structured languages The structure of the activation stack.
Composition CMSC 202. Code Reuse Effective software development relies on reusing existing code. Code reuse must be more than just copying code and changing.
Names and Bindings.
Programming Languages Marjan Sirjani 2 2. Language Design Issues Design to Run efficiently : early languages Easy to write correctly : new languages.
Object Oriented Programming COP3330 / CGS5409.  C++ Automatics ◦ Copy constructor () ◦ Assignment operator =  Shallow copy vs. Deep copy  DMA Review.
Subprogram Control - Data sharing Mechanisms to exchange data Arguments - data objects sent to a subprogram to be processed. Obtained through  parameters.
Chapter 5: Elementary Data Types Properties of types and objects –Data objects, variables and constants –Data types –Declarations –Type checking –Assignment.
Chapter 9 Subprogram Control Consider program as a tree- –Each parent calls (transfers control to) child –Parent resumes when child completes –Copy rule.
Chapter 9 Subprograms Sections 1-5 and 9. Copyright © 2007 Addison-Wesley. All rights reserved. 1–2 Introduction Two fundamental abstraction facilities.
Chapter 14: Overloading and Templates
ISBN Chapter 11 Abstract Data Types and Encapsulation Concepts.
ISBN Chapter 9 Subprograms. Copyright © 2006 Addison-Wesley. All rights reserved.1-2 Introduction Two fundamental abstraction facilities.
Chapter 9: Subprogram Control
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 9 Objects and Classes.
 2006 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
1 Type Type system for a programming language = –set of types AND – rules that specify how a typed program is allowed to behave Why? –to generate better.
Abstract Data Types and Encapsulation Concepts
1 Chapter 8 Objects and Classes. 2 Motivations After learning the preceding chapters, you are capable of solving many programming problems using selections,
OOP Languages: Java vs C++
Programming Languages and Paradigms Object-Oriented Programming.
Imperative Programming
1 The Java Virtual Machine Yearly Programming Project.
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
Names Variables Type Checking Strong Typing Type Compatibility 1.
March 12, ICE 1341 – Programming Languages (Lecture #6) In-Young Ko Programming Languages (ICE 1341) Lecture #6 Programming Languages (ICE 1341)
Programming Languages and Paradigms Object-Oriented Programming (Part II)
CS212: Object Oriented Analysis and Design Lecture 6: Friends, Constructor and destructors.
Chapter 06 (Part I) Functions and an Introduction to Recursion.
Overloading Binary Operators Two ways to overload –As a member function of a class –As a friend function As member functions –General syntax Data Structures.
/* Documentations */ Pre process / Linking statements Global declarations; main( ) { Local Declarations; Program statements / Executable statements; }
Objects and Classes Chapter 6 CSCI CSCI 1302 – Objects and Classes2 Outline Introduction Defining Classes for Objects Constructing Objects Accessing.
CSE 425: Data Types I Data and Data Types Data may be more abstract than their representation –E.g., integer (unbounded) vs. 64-bit int (bounded) A language.
CSC3315 (Spring 2008)1 CSC 3315 Subprograms Hamid Harroud School of Science and Engineering, Akhawayn University
CPS120: Introduction to Computer Science Functions.
Programming Languages and Paradigms Imperative Programming.
An Object-Oriented Approach to Programming Logic and Design Chapter 3 Using Methods and Parameters.
Copyright 2008 Oxford Consulting, Ltd 1 October C++ Classes Enhanced Structures C  struct only permitted to have data members  Functions to.
MT311 Java Application Development and Programming Languages Li Tak Sing( 李德成 )
ISBN Chapter 9 Subprograms Sections
Object-Oriented Programming Chapter Chapter
Chapter 10: Classes and Data Abstraction. Objectives In this chapter, you will: Learn about classes Learn about private, protected, and public members.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 9 Java Fundamentals Objects/ClassesMethods Mon.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 15: Overloading and Templates.
ISBN Object-Oriented Programming Chapter Chapter
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 1 Chapter 9 Objects and Classes.
An Advanced Code Pattern: Inner Classes CSE301 University of Sunderland Harry R. Erwin, PhD Half Lecture.
Chapter 9 Subprograms. Copyright © 2006 Addison-Wesley. All rights reserved.1-2 Chapter 9 Topics Introduction Fundamentals of Subprograms Design Issues.
1 Compiler Construction (CS-636) Muhammad Bilal Bashir UIIT, Rawalpindi.
SWE 4743 Abstraction Richard Gesick. CSE Abstraction the mechanism and practice of abstraction reduces and factors out details so that one can.
1 Chapter 11 © 1998 by Addison Wesley Longman, Inc The Concept of Abstraction - The concept of abstraction is fundamental in programming - Nearly.
1 CS Programming Languages Class 22 November 14, 2000.
1 Copyright © 1998 by Addison Wesley Longman, Inc. Chapter 10 Abstraction - The concept of abstraction is fundamental in programming - Nearly all programming.
Object Oriented Programming. OOP  The fundamental idea behind object-oriented programming is:  The real world consists of objects. Computer programs.
Chapter 10: Classes and Data Abstraction. Classes Object-oriented design (OOD): a problem solving methodology Objects: components of a solution Class:
 Static  Example for Static Field  Example for Static Method  Math class methods  Casting  Scope of Declaration  Method Overloading  Constructor.
Constructor It is a special member of a class that has the following characteristic 1)It has the same name as of its class. 2)It don’t have an explicit.
Methods.
Procedure Definitions and Semantics Procedures support control abstraction in programming languages. In most programming languages, a procedure is defined.
ISBN Chapter 9 Subprograms. Copyright © 2007 Addison-Wesley. All rights reserved.1-2 Chapter 9 Topics Introduction Fundamentals of Subprograms.
Chapter 9 Subprograms. Chapter 9 Topics Introduction Fundamentals of Subprograms Design Issues for Subprograms Local Referencing Environments Parameter-Passing.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 10 Java Fundamentals Objects/ClassesMethods.
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 1 Chapter 9 Introduction of Object Oriented Programming.
Dr. M. Al-Mulhem Introduction 1 Chapter 6 Type Systems.
CSCI 3370: Principles of Programming Languages Chapter 9 Subprograms
Subprograms and Programmer Defined Data Type
SPL – PS3 C++ Classes.
Presentation transcript:

Encapsulation by Subprograms and Type Definitions Subprograms as abstract operations Subprogram definition and invocation Type Definitions

Subprograms as abstract operations A mathematical function that maps each particular set of arguments into a particular set of results

Specification of a subprogram the name of the subprogram the signature of the subprogram: arguments, results the action performed by the subprogram

Type checking for subprograms Type checking: similar to type checking for primitive operations. Difference: types of operands and results are explicitly stated in the program

Problems when describing the function computed by a subprogram Implicit arguments in the form of non-local variables Implicit results – changes in non-local variables History sensitiveness – results may depend on previous executions

Implementation of a subprogram Uses the data structures and operation provided by the language Defined by the subprogram body Local data declarations Statements defining the actions over the data Interface with the user: arguments and returned result

Implementation of subprogram definition and invocation A simple (but not efficient) approach: Each time the subprogram is invoked, a copy of its executable statements, constants and local variables is created. A better approach: The executable statements and constants are invariant part of the subprogram - they do not need to be copied for each execution of the subprogram.

Subprogram Definition and Activation Subprogram definition: the set of statements constituting the body of the subprogram. Static property; the only information available during translation. Subprogram activation: a data structure (record) created upon invoking the subprogram. It exists while the subprogram is being executed. After that the activation record is destroyed.

The definition serves as a template to create the activation record

Static code and dynamic activation record A single copy is used for all activations of the subprogram. This copy is called code segment. This is the static part. The activation record contains only the parameters, results and local data. This is the dynamic part. It has same structure, but different values for the variables.

Type Definitions Type definitions are used for definition of a new type in terms of already defined type. They do not define a complete abstract data type, because the definitions of the operations are not included. Format: typedef definition name Meaning: definition is already defined type. name is substituted with definition.

Examples typedef int key_type; key_type key1, key2; struct rational_number {int numerator, denominator;} typedef rational_number rational; rational r1, r2;

Type equivalence and equality of data objects Two questions to be answered: When are two types the same? When do 2 objects have the same value?

Name equivalence Two data types are considered equivalent only if they have the same name. Issues Every object must have an assigned type, there can be no anonymous types A singe type definition must serve all or large parts of a program

Structural equivalence Two data types are considered equivalent if they define data objects that have the same internal components. Issues Do components need to be exact duplicates? Can field order be different in records? Can field sizes vary?

Data object equality Two objects are equal if each member in one object is identical to the corresponding member of the other object. The compiler has no way to know how to compare data values of user-defined type. It is the task of the programmer that has defined that particular data type to define also the operations with the objects of that type.

Type definition with parameters Parameters allow for user to prescribe the size of data types needed – array sizes. Implementation Type definition with parameters is used as a template as any other type definition during compilation.