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