Messages, Instances and Initialization

Similar documents
Classes and Methods עזאם מרעי המחלקה למדעי המחשב אוניברסיטת בן-גוריון מבוסס על השקפים של אותו קורס שניתן בשנים הקודמות

Classes and Methods גרא וייס המחלקה למדעי המחשב אוניברסיטת בן-גוריון

Classes and Methods לאוניד ברנבוים המחלקה למדעי המחשב אוניברסיטת בן-גוריון

3.Constructors and Destructors. Develop cpp program to implement constructor and destructor.

Benefits of Data Encapsulation. Internal and External Views. Lecture 4 : Data Abstraction. Data Encapsulation. Information Hiding

Cpt S 122 Data Structures. Introduction to C++ Part II

Short Notes of CS201

CS201 - Introduction to Programming Glossary By

The PCAT Programming Language Reference Manual

A Short Summary of Javali

2 ADT Programming User-defined abstract data types

1. Describe History of C++? 2. What is Dev. C++? 3. Why Use Dev. C++ instead of C++ DOS IDE?

Overriding המחלקה למדעי המחשב עזאם מרעי אוניברסיטת בן-גוריון

Programming Languages Third Edition. Chapter 7 Basic Semantics

CPSC 427: Object-Oriented Programming

POLYMORPHISM 2 PART. Shared Interface. Discussions. Abstract Base Classes. Abstract Base Classes and Pure Virtual Methods EXAMPLE

POLYMORPHISM 2 PART Abstract Classes Static and Dynamic Casting Common Programming Errors

CS 376b Computer Vision

Programming Languages Third Edition. Chapter 10 Control II Procedures and Environments

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

CE221 Programming in C++ Part 2 References and Pointers, Arrays and Strings

THE NAME OF THE CONSTRUCTOR AND DESTRUCTOR(HAVING (~) BEFORE ITS NAME) FUNCTION MUST BE SAME AS THE NAME OF THE CLASS IN WHICH THEY ARE DECLARED.

C11: Garbage Collection and Constructors

Ch. 12: Operator Overloading

CSE 504: Compiler Design. Runtime Environments

Classes and Objects. Class scope: - private members are only accessible by the class methods.

Evolution of Programming Languages

Chapter 5. Names, Bindings, and Scopes

Implications of Substitution עזאם מרעי המחלקה למדעי המחשב אוניברסיטת בן-גוריון

KOM3191 Object Oriented Programming Dr Muharrem Mercimek OPERATOR OVERLOADING. KOM3191 Object-Oriented Programming

The SPL Programming Language Reference Manual

Starting Savitch Chapter 10. A class is a data type whose variables are objects. Some pre-defined classes in C++ include int,

Introduction to Programming Using Java (98-388)

Lecture 18 Tao Wang 1

C10: Garbage Collection and Constructors

CS 330 Lecture 18. Symbol table. C scope rules. Declarations. Chapter 5 Louden Outline

Lecture 23: Object Lifetime and Garbage Collection

Chapter 10 :: Data Abstraction and Object Orientation

Lecture 13: Object orientation. Object oriented programming. Introduction. Object oriented programming. OO and ADT:s. Introduction

CPS 506 Comparative Programming Languages. Programming Language

Part 3. Why do we need both of them? The object-oriented programming paradigm (OOP) Two kinds of object. Important Special Kinds of Member Function

COSC 2P95. Procedural Abstraction. Week 3. Brock University. Brock University (Week 3) Procedural Abstraction 1 / 26

1 Lexical Considerations

Where do we go from here?

Lexical Considerations

Intermediate Code Generation

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

Lecture 15: Object-Oriented Programming

Chapter 10 Introduction to Classes

Object Oriented Design

Lecture Notes on Programming Languages

Chapter 8. Operator Overloading, Friends, and References. Copyright 2010 Pearson Addison-Wesley. All rights reserved

Chapter 6 Introduction to Defining Classes


Computers Programming Course 6. Iulian Năstac

Java How to Program, 10/e. Copyright by Pearson Education, Inc. All Rights Reserved.

Chapter 9 :: Data Abstraction and Object Orientation

CPSC 427: Object-Oriented Programming

NOTE: Answer ANY FOUR of the following 6 sections:

News and information! Review: Java Programs! Feedback after Lecture 2! Dead-lines for the first two lab assignment have been posted.!

What are the characteristics of Object Oriented programming language?

CS201- Introduction to Programming Current Quizzes

CS 231 Data Structures and Algorithms, Fall 2016


Java Primer 1: Types, Classes and Operators

Chapter 1 INTRODUCTION SYS-ED/ COMPUTER EDUCATION TECHNIQUES, INC.

Object-Oriented Programming

Chapter 1 GETTING STARTED. SYS-ED/ Computer Education Techniques, Inc.

Lecture 02, Fall 2018 Friday September 7

Comp 248 Introduction to Programming Chapter 4 & 5 Defining Classes Part B

Programming 2. Object Oriented Programming. Daniel POP

Zhifu Pei CSCI5448 Spring 2011 Prof. Kenneth M. Anderson

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

I BCS-031 BACHELOR OF COMPUTER APPLICATIONS (BCA) (Revised) Term-End Examination. June, 2015 BCS-031 : PROGRAMMING IN C ++

PIC 10A Objects/Classes

CSC 533: Organization of Programming Languages. Spring 2005

Pointers and Memory 1

Fundamental Concepts and Definitions

Learning Objectives. C++ For Artists 2003 Rick Miller All Rights Reserved xli

Cpt S 122 Data Structures. Course Review Midterm Exam # 2

Mobile Application Development

Subtyping (Dynamic Polymorphism)

Binding and Variables

Object Oriented Design


In Java we have the keyword null, which is the value of an uninitialized reference type

CA341 - Comparative Programming Languages

Aggregation and Composition. [notes Chapter 4]

Object-Oriented Programming for Scientific Computing

Static and Dynamic Behavior עזאם מרעי המחלקה למדעי המחשב אוניברסיטת בן-גוריון מובסס על הרצאות של אותו קורס שניתן בשנים הקודמות ע "י ד"ר גרא וייס

Data Abstraction. Hwansoo Han

Classes. Logical method to organise data and functions in a same structure. Also known as abstract data type (ADT).

Simplest version of DayOfYear

Topic IV. Parameters. Chapter 5 of Programming languages: Concepts & constructs by R. Sethi (2ND EDITION). Addison-Wesley, 1996.

Lexical Considerations

Type Checking. Error Checking

Software Engineering Concepts: Invariants Silently Written & Called Functions Simple Class Example

Object Oriented Design

Program construction in C++ for Scientific Computing

Full file at

Transcription:

Messages, Instances and Initialization עזאם מרעי מבוסס על השקפים של הקורס תיכון תוכנה מונחה עצמים http://www.cs.bgu.ac.il/~oosd132/ http://www.cs.bgu.ac.il/~oosd142/

2 Dynamic Aspects of Classes In the last chapter we described the static, or compile time aspects of classes. In this chapter we examine their run-time features: How values are instantiated (or created)? How they are initialized? How they communicate with each other by means of message passing?

3 Roadmap In this chapter we examine: Message passing syntax Object creation and initialization (constructors) Accessing the receiver from within a method Memory management or garbage collection

4 Messages are not Function Calls Recall that we noted the following differences between a message and a function (or procedure) call: A message is always given to some object, called the receiver The action performed in response is determined by the receiver Different receivers can do different actions in response to the same message

5 Message Passing Syntax Although the syntax may differ in different languages, all messages have three identifiable parts: agame.displaycard (acard, 42, 27); agame.displaycard(acard, 42, 27); 1. The message receiver 2. The message selector 3. An optional list of arguments

6 Message Passing Syntax Smalltalk and Objective-C use a slightly different syntax: agame display: acard atlocation: 45 and: 56 In these languages a space is used as a separator Unary messages (messages that take no argument) are simply written following the receiver Messages that take arguments are written using keyword notation The message selector is split into parts, one part before each argument A colon follows each part of the key

7 Message Passing Syntax In Objective-C a Smalltalk-like message is enclosed in a pair of square brackets Termed a message passing expression The brackets only surround the message itself They do not, for example, surround an assignment the places the result of a message into a variable: int cardrank = [ acard getrank ]

8 Binary Operations In Smalltalk even binary operations, such as addition, are interpreted as a message Sent to the left value with the right value as argument: z <- x + y "message to x to add y to itself and return sum" It is possible to define binary operators in C++ to have similar meanings Example: Vector arithmetic

9 Message Passing in Various Languages

10 Statically Typed and Dynamically Typed Languages A statically typed language requires the programmer to declare a type for each variable The validity of a message passing expression will be checked at compile time based on the declared type of the receiver Java, C++, C# A dynamically typed language associates types with values, not with variables A variable is just a name The legality of a message cannot be determined until run-time Smalltalk, Python, CLOS

11 The Receiver Variable Inside a method, the receiver can be accessed by means of a pseudo-variable Called this in Java, C++, C# Called self in Smalltalk, Objective-C, Object Pascal Called current in Eiffel function PlayingCard.color : colors; begin if (self.suit = Heart) or (self.suit = Diamond) then color := Red else color := Black; end

12 Implicit Use of This Within a method, a message expression or a data access with no explicit receiver is implicitly assumed to refer to this: class PlayingCard {... public void flip () { setfaceup(!faceup ); }... } Is assumed to be equivalent to: class PlayingCard {... public void flip () { this.setfaceup(!this.faceup); }... }

13 Explicit use of the pseudo variable One place where the use of the variable often cannot be avoided is when a method wishes to pass itself as an argument to another function As in the following bit of Java: class QuitButton extends Button implements ActionListener { public QuitButton () {... // install ourselves as a listener for button events addactionlistener(this); }... };

14 Explicit use of the pseudo variable Some style guidelines for Java suggest the use of this when arguments in a constructor are used to initialize a data member The same name can then be used for the argument and the data member, with the explicit this being used to distinguish the two names: class PlayingCard { public PlayingCard (int suit, int rank) { this.rank = rank; // this.rank is the data member this.suit = suit; // rank is the argument value this.faceup = true; }... private int suit; private int rank; private boolean faceup; }

15 Object Creation In most programming languages objects can be created dynamically, usually using the new operator: PlayingCard acard; // simply names a new variable acard = new PlayingCard(Diamond, 3); // creates the new object The declaration simply names a variable, The new operator is needed to create the new object value

16 Syntax used for Object Creation

17 Pointers and Memory Allocation All object-oriented languages use pointers in their underlying representation Not all languages expose this representation to the programmer It is sometimes said that Java has no pointers as a point of contrast to C++ A more accurate statement would be that Java has no pointers that the programmer can see Since all object references are in fact pointers in the internal representation

18 Pointers and Memory Allocation 1 st reason Pointers normally reference memory that is heap allocated And thus does not obey the normal rules associated with variables in conventional imperative languages In an imperative language a value created inside a procedure will exist as long as the procedure is active, and will disappear when the procedure returns

19 Pointers and Memory Allocation 1 st reason Pointers normally reference memory that is heap allocated And thus does not obey the normal rules associated with variables in conventional imperative languages In an imperative language a value created inside a procedure will exist as long as the procedure is active, and will disappear when the procedure returns A heap allocated value, on the other hand, will continue to exist as long as there are references to it Which often will be much longer than the lifetime of the procedure in which it is created

20 Pointers and Memory Allocation 2 nd reason A second reason is that some languages, notably C++, distinguish between conventional values and pointer values In C++ a variable that is declared in the normal fashion, a so-called automatic variable, has a lifetime tied to the function in which it is created When the procedure exits, the memory for the variable is recovered: void exampleprocedure { PlayingCard ace(diamond, 1);... // memory is recovered for ace // at end of execution of the procedure } Values that are assigned to pointers (or as references, which are another form of pointers) are not tied to procedure entry

21 Memory Recovery Because in most languages objects are dynamically allocated, they must be recovered at run-time. There are two broad approaches to this: Force the programmer to explicitly say when a value is no longer being used: delete acard; // C++ example Use a garbage collection system that will automatically determine when values are no longer being used, and recover the memory

22 Memory Errors Garbage collection systems impose a run-time overhead, but prevent a number of potential memory errors: 1. Running out of memory because the programmer forgot to free values: 2. Using a memory value after it has been recovered: PlayingCard * acard = new PlayingCard(Spade, 1); delete acard; cout << acard.rank(); 3. Free the same value twice: PlayingCard * acard = new PlayingCard(Spade, 1); delete acard; delete acard; // delete already deleted value

23 How to Manage Memory in an Object Oriented Program? When a garbage collection system is not available, to avoid these problems It is often necessary to ensure that every dynamically allocated memory object has a designated owner The owner of the memory is responsible for ensuring that the memory location is used properly and is freed when it is no longer required In large programs, as in real life: Disputes over the ownership of shared resources can be a source of difficulty

24 How to manage memory in an Object Oriented Program? When a single object cannot be designated as the owner of a shared resource, another common technique is to use reference counts: Count of the number of pointers that reference the shared object Care is needed to ensure that the count is accurate When the count reaches zero it indicates that no pointers refer to the object, and its memory can be recovered As with the arguments for and against dynamic typing, the arguments for and against garbage collection tend to pit efficiency against flexibility Automatic garbage collection can be expensive, as it necessitates a run-time system to manage memory On the other hand, the cost of memory errors can be equally expensive

25 Constructors A constructor is a function that is implicitly invoked when a new object is created The constructor performs whatever actions are necessary in order to initialize the object In C++, Java, C# a constructor is a function with the same name as the class In Python constructors are all named init In Delphi, Objective-C, constructors have special syntax, but can be named anything class PlayingCard { // a Java constructor public PlayingCard (int s, int r) { suit = s; rank = r; faceup = true; }... }

26 Linking creation and initialization together Linking creation and initialization together has many beneficial consequences Most importantly, it guarantees that an object can never be used before it has been properly initialized When creation and initialization are separated (as they must be in languages that do not have constructors), a programmer can easily forget to call an initialization routine after creating a new value Often with unfortunate consequences A less common problem, although often just as unfortunate, is to invoke an initialization procedure twice on the same object

27 Overloaded Constructors Constructors are often overloaded, meaning there are a number of functions with the same name They are differentiated by the type signature, and the arguments used in the function call or declaration: class PlayingCard { public: PlayingCard ( ) // default constructor, // used when no arguments are given { suit = Diamond; rank = 1; faceup = true; } PlayingCard (Suit is) // constructor with one argument { suit = is; rank = 1; faceup = true; } }; PlayingCard (Suit is, int ir) // constructor with two arguments { suit = is; rank = ir; faceup = true; }

28 The Orthodox Canonical Class Form A default constructor This is used internally to initialize objects and data members when no other value is available A copy constructor This is used, among other places, in the implementation of call-by-value parameters An assignment operator This is used to assign one value to another A destructor This is invoked when an object is deleted

29 Metaclasses In Smalltalk (and Objective-C) classes are just objects, instances of class Class. new is just a message given to a class object If we want to create constructors, where do we put them? They can't be part of the collection of messages of instances of the class, since we don't yet have an instance They can't be part of the messages understood by class Class, since not all classes have the same constructor message Where do we put the behavior for individual class instances?

30 Solution: Create New Classes for Classes The solution is to create a new class, who's only instance is itself a class. An elegant solution that maintains the simple instance/class relationship.

31 Chapter Summary In this chapter we have examined the following topics: Message Passing Syntax Object Creation and Initialization (constructors) Accessing the Receiver from within a method Memory Management or garbage collection Metaclasses in Smalltalk