Classes and Objects Digging a little deeper. Rectangles We want a class to represent a rectangle which located somewhere in XY plane. The question is,

Slides:



Advertisements
Similar presentations
ACM/JETT Workshop - August 4-5, Classes, Objects, Equality and Cloning.
Advertisements

Python Mini-Course University of Oklahoma Department of Psychology Lesson 26 Classes and Objects 6/16/09 Python Mini-Course: Lesson 26 1.
Object Oriented Programming COP3330 / CGS5409.  C++ Automatics ◦ Copy constructor () ◦ Assignment operator =  Shallow copy vs. Deep copy  DMA Review.
Lecture 16 Subroutine Calls and Parameter Passing Semantics Dragon: Sec. 7.5 Fischer: Sec Procedure declaration procedure p( a, b : integer, f :
Functions ROBERT REAVES. Functions  Interface – the formal description of what a subprogram does and how we communicate with it  Encapsulation – Hiding.
DICTIONARIES. The Compound Sequence Data Types All of the compound data types we have studies in detail so far – strings – lists – Tuples They are sequence.
Week 10: Objects and Classes 1.  There are two classifications of types in Java  Primitive types:  int  double  boolean  char  Object types: 
Python Programming Chapter 12: Classes and Objects Saad Bani Mohammad Department of Computer Science Al al-Bayt University 1 st 2010/2011.
CSC Programming for Science Lecture 30: Pointers.
Four simple expressions in meta. Data objects Pieces of data in a computer are called objects Today, we’ll talk about four kinds of objects Numbers Pictures.
1 Programming for Engineers in Python Autumn Lecture 5: Object Oriented Programming.
Collections, Part 2 You were introduced to Generic collections for assignment 3—ThingsToDraw was a Generic.List of Idrawable objects. Since the Generic.List.
Error Bars & Uncertainty in Slope IB DP Physics. Error Bars  Used on graphs to display the uncertainty in measurements of the data points.  There may.
State,identity and behavior of objects Sem III K.I.R.A.S.
CMPT 120 Lists and Strings Summer 2012 Instructor: Hassan Khosravi.
Programming with Alice Computing Institute for K-12 Teachers Summer 2011 Workshop.
Unit 2, cont. September 12 More HTML. Attributes Some tags are modifiable with attributes This changes the way a tag behaves Modifying a tag requires.
Hello, little turtles. Hello, little turtles! There are many modules in Python that provide very powerful feature that we can use in our own program.
Checking Equality of Reference Variables. Arrays and objects are both “reference” types n They are allocated a chunk of memory in the address space n.
More About Classes Ranga Rodrigo. Information hiding. Copying objects.
Games Development 2 Concurrent Programming CO3301 Week 9.
Object-oriented programming and design 1 Object Identity = vs. == copying objects Value Object ValueWithHistory.
Chapter 8 Part 2 SQL-99 Schema Definition, Constraints, Queries, and Views.
Question of the Day You overhear a boy & his mother talking: Mom:What is ? Boy: That's easy, 33. Mom: Good. What's ? Boy:Simple. It's 40. Mom:Excellent!
CPSC 252 The Big Three Page 1 The “Big Three” Every class that has data members pointing to dynamically allocated memory must implement these three methods:
 You can also divide an image into regions that link to different documents depending on where someone clicks.  This is called an imagemap, and any.
1 Programming for Engineers in Python Autumn Lecture 6: More Object Oriented Programming.
Assembly and Coordinate Management with OSD and WM.
1 Structs. 2 Defining a Structure Often need to keep track of several pieces of information about a given thing. Example: Box We know its length width.
Lecture 04 – Models of memory Mutable and immutable data.
Modifying HTML attributes and CSS values. Learning Objectives By the end of this lecture, you should be able to: – Select based on a class (as opposed.
CSC 212 – Data Structures Lecture 2: Primitives, References, & Classes.
CS 325 Introduction to Computer Graphics 04 / 12 / 2010 Instructor: Michael Eckmann.
The Assignment Operator. Rule of Three Any object which manages memory needs: – Custom Destructor – Custom Copy Constructor – Custom Assignment Operator.
Scope, Aliasing, Tuples & Mutability Intro2CS – week 4a 1.
Question of the Day You overhear a boy & his mother talking: Mom:What is ? Boy: That's easy, 33. Mom: Good. What's ? Boy:Simple. It's 40. Mom:Excellent!
Make a Model A box company makes boxes to hold popcorn. Each box is made by cutting the square corners out of a rectangular sheet of cardboard. The rectangle.
CS 1114: Finding things Prof. Graeme Bailey (notes modified from Noah Snavely, Spring 2009)
CS 115 Lecture 7 Graphics – coordinate systems, Text, Entry, aliases Taken from notes by Dr. Neil Moore.
Internet Computing Module II. Syllabus Creating & Using classes in Java – Methods and Classes – Inheritance – Super Class – Method Overriding – Packages.
This I Believe Essay Writer’s Workshop: Introductions, Juicy Details, & Conclusions 8 th ELA St. Joseph School.
Parts of an Expression. TERMS Each expression is made up of terms. A term can be a signed number, a variable, or a constant multiplied by a variable or.
Equality, references and mutability COMPSCI 105 SS 2015 Principles of Computer Science.
Chapter 9 A Second Look at Classes and Objects - 2.
A Second Look at Classes and Objects - 1 Static Class Members
Pixels, Colors and Shapes
10 - Python Dictionary John R. Woodward.
CSS Layouts: Grouping Elements
CMSC201 Computer Science I for Majors Lecture 12 – Lists (cont)
Chapter 10: Void Functions
A Second Look at Classes and Objects - 2
Area of Triangles.
Barnes Art Museum, Philadelphia
This pointer, Dynamic memory allocation, Constructors and Destructor
Introduction to Classes
CSCI 1260 – Lecture 2: Instantiation, Aggregation, and Composition
Implementing Non-Static Features
Teaching Java using Turtles part 2
Defining Classes and Methods
Java Classes Aliases & Null & This
References.
Reference semantics, variables and names
Defining Classes and Methods
L.O Read and plot coordinates in all quadrants
A simple function.
Department of Computing Science
Making a copy of an object: shallow and deep copy
Layout Organization and Management
Teaching Java using Turtles part 2
Objects Management.
Presentation transcript:

Classes and Objects Digging a little deeper

Rectangles We want a class to represent a rectangle which located somewhere in XY plane. The question is, what information do we have to provide in order to specify such a rectangle?

There are a few possibililities Specify the center of the rectangle(two coordinates) and its size (width and height) Or specify the center of the rectangle (two coordinates)

width = height = corner x  0.0 Y  0.0 box

Objects are mutable We can change the state of an object by making an assignment to one of its attribute. For example, to grow the size of a rectangle without changing its position, we could modify the values of width and height.

Sameness If we say, “Alice and Bob have the same car” we mean that her car and his are the same make and model. But that they are two different cars. If we say, “Alice and Bob have the same mother”, We mean that her mother and his are the same person.

Sameness When we talk about objects, there is a similar ambiguity. For example, if two Points are the same, does that mean they contain the same data (coordinates) or that they are actually the same object?

The type of equality Shallow equality It compares only the references, not the contents the objects. Deep equality To compare the contents of the objects

The is operator It allow us to find out if two references refer the same object This type of equality is called shallow equality because it compares only the references, not the contents of the objects.

Two different points Now if we create two different objects that contain the same data, we can use same_coordinate to find out if they represent points with the same coordinates.

Find out two different points with the same coordinates

Two different points If the two variables refer to the same object, they have both shallow and deep equality.

Beware of ==

So we conclude that even though the two lists (or tuples, etc.) are distinct objects with different memory addresses, for lists the == operator tests for deep equality, while in the case of points it makes a shallow test Beware of ==

Copying Aliasing can make a program difficult to read because changes made in one place might have unexpected effects in another place. It is hard to keep track of all the variables that might refer to a given object. Copying an object is often an alternative to aliasing. The copy module contains a function called copy that duplicate any object.

When we import the copy module, we can use the copy function to make a new point. p1 and p2 are not the same point. But they contain the data

Copying To copy the simple object like a point, which doesn’t contain any embedded objects, copy is sufficient. This is called shallow copying. For something like a Rectangle, which contains a reference to a Point, copy doesn’t do quite the right thing. It copies the reference to the Point object, so both the old Rectangle and the new one refer to a single Point

Copying If we create a box, b1. In the usual way and then make a copy, b2, using copy. The resulting state diagram look like this: width height corner x = 0.0 y = width height corner b1 b2

Copying width height corner x = 0.0 y = width height corner b1 b2 This almost certainly not what we want. In this case, invoking grow on one of the Rectangle objects would not effect the other, but invoking move on either would affect both!. The shallow copy has created an alias to the Point that represents the corner.

deepcopy The copy module contains a function named deepcopy that copies not only the object but also any embedded objects Now b1 and b2 are completely separate objects.