Last Time: Objects and Abstraction. If it walks like a circle, swims like a circle, and quacks like a circle...!

Similar documents
Objects, Object-Oriented Design, Methods /

Lecture 16: Object-oriented Programming

Quick Review of Object-Oriented Programming in Go

Issue with Implementing PrimeSieve() in Go

Pointers /

Lecture Overview Methods and Interfaces Methods review Interfaces Example: using the sort interface Anonymous fields in structs

Homework 5: Spatial Games : Programming for Scientists Due: Thursday, March 3, 2016 at 11:59 PM

Erlang and Go (CS262a, Berkeley Fall 2016) Philipp Moritz

Welcome to CSC148! Introduction to Computer Science

Lexical and Syntax Analysis

Lexical and Syntax Analysis. Abstract Syntax

Question: How can we compare two objects of a given class to see if they are the same? Is it legal to do: Rectangle r(0,0,3,3);,, Rectangle s(0,0,4,4)

A Slice of Life

TUTORIAL: D3 (1) Basics. Christoph Kralj Manfred Klaffenböck

Module 01 Processing Recap. CS 106 Winter 2018

The Awesomeness of Go. Igor Lankin DevFest Karlsruhe, Nov 2016

Introduzione a Go e RPC in Go

Computer Programming

Lecture 9: Lists. Lists store lists of variables. Declaring variables that hold lists. Carl Kingsford, , Fall 2015

Prof. Carl Schultheiss MS, PE. CLASS NOTES Lecture 12

Clean Classes. Christopher Simpkins Chris Simpkins (Georgia Tech) CS 2340 Objects and Design CS / 11

Homework 5: Graphics Due: 11:59pm on Thursday, October 22

+ Inheritance. Sometimes we need to create new more specialized types that are similar to types we have already created.

CS21: INTRODUCTION TO COMPUTER SCIENCE. Prof. Mathieson Fall 2018 Swarthmore College

2.2 Volumes of Solids of Revolution

Suppose we store information and employees and teams using the following types:

Arrays and Strings

Solution Notes. COMP 151: Terms Test

Object Design Guidelines

Object-Oriented Programming Concepts

Painting your window

Introduction to Programming. Structures

Homework 6: Spatial Games Due: 11:59pm on Friday, October 30

Graphics Overview ECE2893. Lecture 19. ECE2893 Graphics Overview Spring / 15

Type Analysis. Type Checking vs. Type Inference

Area and Volume. where x right and x left are written in terms of y.

Page 129 Exercise 5: Suppose that the joint p.d.f. of two random variables X and Y is as follows: { c(x. 0 otherwise. ( 1 = c. = c

ALGEBRA II UNIT X: Conic Sections Unit Notes Packet

Now it only remains to supply the code. Begin by creating three fonts:

Module 01 Processing Recap

Advanced Internet Programming CSY3020

CMSC 104 -Lecture 5 John Y. Park, adapted by C Grasso

Surface Area and Volume

COMP 250. Lecture 29. interfaces. Nov. 18, 2016

If the ball goes off either the right or left edge, turn the ball around. If x is greater than width or if x is less than zero, reverse speed.

Compact Off-Heap Structures in the Java Language

1. More jquery Methods 2. JavaScript + SVG: Raphaël 3. About SVG 4. Working with SVG 5. Animating SVG

Range Reporting. Range Reporting. Range Reporting Problem. Applications

More C++ : Vectors, Classes, Inheritance, Templates. with content from cplusplus.com, codeguru.com

CS16 Final Exam E03, 10S, Phill Conrad, UC Santa Barbara Wednesday, 06/09/2010

Object-Oriented Programming in Processing

Objects. say something to express one's disapproval of or disagreement with something.

A student was asked to point out interface elements in this code: Answer: cout. What is wrong?

QUIZ. Source:

Arrays/Slices Store Lists of Variables

We start by looking at a double cone. Think of this as two pointy ice cream cones that are connected at the small tips:

The S.O.L.I.D. Principles. of Object Oriented Programming

QUIZ. What are 3 differences between C and C++ const variables?

Problem 1: Textbook Questions [4 marks]

Data Visualization (CIS/DSC 468)

Pointers and scanf() Steven R. Bagley

Outline. Review of Last Week II. Review of Last Week. Computer Memory. Review Variables and Memory. February 7, Data Types

Conditionals & Loops /

Collisions/Reflection

Mobile Application Programming. Layout Techniques

<script type="text/javascript" src="breakout.js"> </script>

CS2 Assignment A1S The Simple Shapes Package

Grade 7/8 Math Circles Fall Nov.4/5 The Pythagorean Theorem

Outline. Inheritance. Abstract Classes Interfaces. Class Extension Overriding Methods Inheritance and Constructors Polymorphism.

Outline. Data Definitions and Templates Syntax and Semantics Defensive Programming

MACS 261J Final Exam. Question: Total Points: Score:

Today in CS161. Lecture #7. Learn about. Rewrite our First Program. Create new Graphics Demos. If and else statements. Using if and else statements

HO-1: INTRODUCTION TO FIREWORKS

10.7 Surface Area and Volume of Cylinders

Bioinformatics Resources

Singly linked lists in C.

CISC220 Lab 2: Due Wed, Sep 26 at Midnight (110 pts)

Covering & Surrounding

Introduction to the Go Programming Language

We'll dive right in with an example linked list. Our list will hold the values 1, 2, and 3.!

This lecture. Introduction to VRT. Hardware. Implementation levels. Scene-Graph libraries. Computer Graphic libraries

Introduction to VRT. Interactive Graphical Systems HT2007. Lars Pettersson. Interactive Graphical Systems

COMP200 ABSTRACT CLASSES. OOP using Java, from slides by Shayan Javed

Double Integration: Non-Rectangular Domains

Chapter 2 Exercise Solutions

More Examples /

Summary of Go Syntax /

= 25)(10) 10. =

JAVA PROGRAMMING LAB. ABSTRACT In this Lab you will learn how to describe objects and classes and how to define classes and create objects

Web Programming 1 Packet #5: Canvas and JavaScript

AP Calculus. Areas and Volumes. Student Handout

AP * Calculus Review. Area and Volume

Programming in C++: Assignment Week 4

blur Melissa Kaufman-Gomez Team Leader Timothy Goodwin System Architect Dexter Callender Language Guru Daniel Hong Test Wizard

CS7026 HTML5. Canvas

Volumes of Solids of Revolution Lecture #6 a

CSC 102 Lecture Notes Week 1 Introduction to the Course Introduction to Jav a

Ce qui est important dans l'enseignement des mathématiques. Marian Small novembre 2017

Module 05 User Interfaces. CS 106 Winter 2018

Definition 3.1 The partial derivatives of a function f(x, y) defined on an open domain containing (x, y) are denoted by f

Transcription:

Pointers 02-201

Last Time: Objects and Abstraction If it walks like a circle, swims like a circle, and quacks like a circle...!

Bird s Eye View of Object-Oriented Programming http://null-byte.wonderhowto.com! Object: Collection of variables seen as a whole (noun) Field: A property of the object (adjective) Method: An essential action of the object (verb).

Last Time: Rectangle and Circle Structs type Rectangle struct { x1 float64 y1 float64 width float64 height float64 type Circle struct { x1 float64 y1 float64 radius float64

Last Time: Methods Methods allow us to have multiple functions with the same name. func (R Rectangle) Area() float64 { return R.width*R.length func (C Circle) Area() float64 { return C.radius * C.radius * math.pi

Last Time: Rectangle and Circle Structs type Rectangle struct { x1 float64 y1 float64 width float64 height float64 type Circle struct { x1 float64 y1 float64 radius float64 Exercise: Write Translate() methods that shift circle and rectangle by a in the x-direction and b in the y-direction.

Translating Points func (R Rectangle) Translate(a, b float64) { R.x1 += a R.y1 += b func (C Circle) Translate(a, b float64) { C.x1 += a C.y1 += b

Translating Points func main() { var R Rectangle R.x1 = 1.0 R.y1 = 3.0 R.width = 3 R.height = 5 R.Translate(-2.1, 4.7) fmt.println(r.x1, R.y1) Think: What is printed?

Two Problems 1. We can t change the fields of a struct inside of a method, i.e., we would like to do the following: func (C Circle) DoubleRadius() { C.radius = 2*C.radius 2. This fixes the problem but is very wasteful of memory since the entire Circle is copied. func (C Circle) DoubleRadius() Circle { C.radius = 2*C.radius return C

Solution: Think Meta

Courtesy:! pobox321.com!

RAM: A Giant Memory Storage Facility

Pointers: One Solution to Two Problems Pointer: A variable that holds the address (in memory) of some other variable. var b int = -14 var a *int = &b //* = pointer to //& = location of -14 Courtesy: Benutzer! (Wikimedia Commons)!

Accessing Struct Fields with a Pointer var C Circle var pointertoc *Circle // at this point, pointertoc == nil // which Circle does pointertoc point to?

Accessing Struct Fields with a Pointer var C Circle var pointertoc *Circle // at this point, pointertoc == nil // which Circle does pointertoc point to? pointertoc = &C // pointertoc points to the Circle named C

Accessing Struct Fields with a Pointer var C Circle var pointertoc *Circle // at this point, pointertoc == nil // which Circle does pointertoc point to? pointertoc = &C // pointertoc points to the Circle named C (*pointertoc).x1= -1.7 // change x coordinate of center of C

Accessing Struct Fields with a Pointer var C Circle var pointertoc *Circle // at this point, pointertoc == nil // which Circle does pointertoc point to? pointertoc = &C // pointertoc points to the Circle named C (*pointertoc).x1= -1.7 // change x coordinate of center of C pointertoc.x1= -1.7 // this is equivalent to make things easy!

Pointers Help Us Change Fields Inside Method Before, we couldn t change fields inside a method... func (C Circle) DoubleRadius() { C.radius = 2*C.radius Solution: Take a pointer to a Circle as the input. func (C *Circle) DoubleRadius() { C.radius = 2*C.radius // note double-meaning of *

Pointers Help Us Change Fields Inside Method Go doesn t mind if C isn t even a pointer! func main() { var C Circle C.radius = 2 C.DoubleRadius()

Pointers Help Us Change Fields Inside Method We get a pointer when using new with a struct. func main() { var C Circle C.radius = 2 C.DoubleRadius() D := new(circle) // D has type *Circle, but no big deal! D.x1, D.y1 = -1.4, 3.19

Quick Quiz Think: What does the following print? func ChangeFirst(list []int) { list[0] = 1 list := make([]int, 10) ChangeFirst(list) fmt.println(list[0])

Quick Quiz Think: What does the following print? func ChangeFirst(list []int) { list[0] = 1 list := make([]int, 10) ChangeFirst(list) fmt.println(list[0]) Answer: 1... and now we can say that this is because slices are really pointers to underlying arrays.

We Have Already Been Working with This! type Canvas struct { gc *draw2d.imagegraphiccontext img image.image width int height int Pointer to a struct that represents the pen An object that represents the image MoveTo(c *Canvas, x, y float64) LineTo(c *Canvas, x, y float64) SetStrokeColor(c *Canvas, col color.color) SetFillColor(c *Canvas, col color.color) SetLineWidth(c *Canvas, w float64) Stroke(c *Canvas) FillStroke(c *Canvas) Fill(c *Canvas) ClearRect(c *Canvas, x1, y1, x2, y2 int) SaveToPNG(c *Canvas, filename string) Width(c *Canvas) Height(c *Canvas)

More Practice Exercise: Say that we have a collection of one-way flights between cities. Design an object-oriented approach to represent the flight network. Courtesy: PIT airport