DOC PREVIEW
Yale CPSC 427 - Lecture 5
School name Yale University
Pages 35

This preview shows page 1-2-16-17-18-34-35 out of 35 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 35 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 35 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 35 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 35 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 35 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 35 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 35 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 35 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

OutlineFunctions and MethodsParametersChoosing Parameter TypesThe Implicit ArgumentSimple VariablesPointersReferencesOutline Functions and Methods Simple Variables Pointers ReferencesCPSC 427a: Object-Oriented ProgrammingMichael J. FischerLecture 5September 15, 2011CPSC 427a, Lecture 5 1/35Outline Functions and Methods Simple Variables Pointers ReferencesFunctions and MethodsParametersChoosing Parameter TypesThe Implicit ArgumentSimple VariablesPointersReferencesCPSC 427a, Lecture 5 2/35Outline Functions and Methods Simple Variables Pointers ReferencesFunctions and MethodsCPSC 427a, Lecture 5 3/35Outline Functions and Methods Simple Variables Pointers ReferencesParametersCall by valueLike C, C++ passes explicit parameters by value.void f( int y ) { ... y=4; ... };...int x=3;f(x);Ix and y are independent variables.Iy is created when f is called and destroyed when it returns.IAt the call, the value of x (=3) is used to initialize y.IThe assignment y=4; inside of f has no effect on x.CPSC 427a, Lecture 5 4/35Outline Functions and Methods Simple Variables Pointers ReferencesParametersCall by pointerLike C, pointer values (which I call reference values) are thethings that can be stored in pointer variables.Also like C, references values can be passed as arguments tofunctions having corresponding pointer parameters.void g( int* p ) { ... (*p)=4; ... };...int x=3;g(&x);Ip is created when g is called and destroyed when it returns.IAt the call, the value of &x, a reference value, is used toinitialize p.IThe assignment (*p)=4; inside of g changes the value of x.CPSC 427a, Lecture 5 5/35Outline Functions and Methods Simple Variables Pointers ReferencesParametersCall by referenceC++ has a new kind of parameter called a reference parameter.void g( int& p ) { ... p=4; ... };...int x=3;g(x);IThis does same thing as previous example; namely, theassignment p=4 changes the value of x.IWithin the body of g, p is a synonym for x.IFor example, &p and &x are identical reference values.CPSC 427a, Lecture 5 6/35Outline Functions and Methods Simple Variables Pointers ReferencesParametersI/O uses reference parametersIThe first argument to << has type ostream&.Icout << x << y; is same as (cout << x) << y;.I<< returns a reference to its first argument, so this is also thesame ascout << x;cout << y;CPSC 427a, Lecture 5 7/35Outline Functions and Methods Simple Variables Pointers ReferencesChoosing Parameter TypesHow should one choose the parameter type?Parameters are used for two main purposes:ITo send data to a function.ITo receive data from a function.CPSC 427a, Lecture 5 8/35Outline Functions and Methods Simple Variables Pointers ReferencesChoosing Parameter TypesSending data to a function: call by valueFor sending data to a function, call by value copies the datawhereas call by pointer or reference copies only an address.IIf the data object is large, call by value is expensive of bothtime and space and should be avoided.IIf the data object is small (eg., an int or double), call byvalue is cheaper since it avoids the indirection of a reference.ICall by value protects the caller’s data from beinginadvertantly changed.CPSC 427a, Lecture 5 9/35Outline Functions and Methods Simple Variables Pointers ReferencesChoosing Parameter TypesSending data to a function: call by reference or pointerCall by reference or pointer allows the caller’s data to be changed.Use const to protect the caller’s data from inadvertane change.Ex: int f( const int& x ) or int g( const int* xp ).Prefer call by reference to call by pointer for input parameters.Ex: f( 234 ) works but g( &234 ) does not.Reason: 234 is not a variable and hence can not be the target of apointer.(The reason f( 234 ) does work is a bit subtle and will beexplained later.)CPSC 427a, Lecture 5 10/35Outline Functions and Methods Simple Variables Pointers ReferencesChoosing Parameter TypesReceiving data from a functionAn output parameter is expected to be changed by the function.Both call by reference and call by pointer work.Call by reference is generally preferred since it avoids the need forthe caller to place an ampersand in front of the output variable.Declaration: int f( int& x ) or int g( int* xp ).Call: f( result ) or g( &result ).CPSC 427a, Lecture 5 11/35Outline Functions and Methods Simple Variables Pointers ReferencesThe Implicit ArgumentThe implicit argumentEvery call to a class member function has an implicit argument,which is the object written before the dot in the function call.class MyExample {private:int count; // data memberpublic:void advance(int n) { count += n; }...};...MyExample ex;ex.advance(3);Increments ex.count by 3.CPSC 427a, Lecture 5 12/35Outline Functions and Methods Simple Variables Pointers ReferencesThe Implicit ArgumentthisThe implicit argument is passed by pointer.In the call ex.advance(3), the implicit argument is ex, and apointer to ex is passed to advance().The implicit argument can be referenced directly from within amember function using the keyword this.Within the definition of advance(), count and this->count aresynonymous.CPSC 427a, Lecture 5 13/35Outline Functions and Methods Simple Variables Pointers ReferencesSimple variablesCPSC 427a, Lecture 5 14/35Outline Functions and Methods Simple Variables Pointers ReferencesL-values and R-valuesProgramming language designers have long been bothered by theasymmetry of assignment.x = 3 is a legal assignment statement.3 = x is not legal.Expressions are treated differently depending on whether theyappear on the left or right sides of an assignment statement.Something that can appear on the left is called an L-value.Something that can appear on the right is called an R-value.Intuitively, an L-value is the address of a storage location – someplace where a value can be stored.An R-value is a thing that can be placed in a storage location.R-values are sometimes called pure data values.CPSC 427a, Lecture 5 15/35Outline Functions and Methods Simple Variables Pointers ReferencesSimple variable declarationThe declaration int x = 3; says several things:1. All values that can be stored in x have type int.2. The name x is bound (when the code is executed) to astorage location adequate to store an int.3. The int value 3 is initially placed in x’s storage location.The L-value of x is the address of the storage location of x.The R-value of x is the object of type int that is stored in x.CPSC 427a, Lecture 5 16/35Outline Functions and Methods Simple Variables Pointers ReferencesSimple


View Full Document

Yale CPSC 427 - Lecture 5

Download Lecture 5
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Lecture 5 and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Lecture 5 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?