Back to slide -- Keyboard shortcut: 'u'  previous -- Keyboard shortcut: 'p'  next -- Keyboard shortcut: 'n'          constructors/prog5.cc - Passing Point objects in and out of functions.Lecture 4 - slide 18 : 40
Program 3

// Point(): (0,7)    Point(x): (x, 20)    Point(x,y): (x,y)    Point(p) = (p.x+1,p.y+2)

#include <iostream>
#include "point.h"

using namespace std;

Point g(Point p){
  return p;
}  

Point& h(Point& p){
  return p;
}  

Point i(Point& p){
  return p;
}  

void f(){
  Point o(21.0, 22.0),            // (21,22)
        p, q, r;                  // all (0,7)

  p = g(o);                       // o is (21,22). Passed in by value as (22, 24).
                                  // g copies the object back via return, 
                                  // using the copy constructor:  (23, 26).

  q = h(o);                       // o is (21,22). Passed by reference as (21,22).
                                  // the object is passed back by reference, still (21, 22).

  r = i(o);                       // o = (21, 22). Passed by reference: (21,22).
                                  // i returns the point, copied by  
                                  // use of copy constructor: (22, 24).

  cout << "Point p: " << p << endl;  // (23,26)
  cout << "Point q: " << q << endl;  // (21,22)
  cout << "Point r: " << r << endl;  // (22,24)
}

int main(){
  f();
}