DOC PREVIEW
Yale CPSC 427 - Lecture 6
School name Yale University
Pages 31

This preview shows page 1-2-14-15-30-31 out of 31 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 31 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 31 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 31 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 31 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 31 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 31 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 31 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

OutlineFunctions and MethodsChoosing Parameter TypesThe Implicit ArgumentSimple VariablesPointersReferencesOutline Functions and Methods Simple Variables Pointers ReferencesCPSC 427a: Object-Oriented ProgrammingMichael J. FischerLecture 6September 21, 2010CPSC 427a 1/31Outline Functions and Methods Simple Variables Pointers ReferencesFunctions and MethodsChoosing Parameter TypesThe Implicit ArgumentSimple VariablesPointersReferencesCPSC 427a 2/31Outline Functions and Methods Simple Variables Pointers ReferencesFunctions and Methods (continued)CPSC 427a 3/31Outline 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 4/31Outline 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.If the data object is large, call by value is expensive of both timeand space and should be avoided.If the data object is small (eg., an int or double), call by value ischeaper since it avoids the indirection of a reference.Call by value protects the caller’s data from being inadvertantlychanged.CPSC 427a 5/31Outline 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 6/31Outline 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 value 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 7/31Outline Functions and Methods Simple Variables Pointers ReferencesThe Implicit ArgumentThe implicit argumentEvery call to a class function has an implicit argument, which isthe name written before the function call.class MyExample {private:int count; // data memberpublic:void advance(int n) { count += n; }...};...MyExample ex;ex.advance(3);Increments count by 3.CPSC 427a 8/31Outline Functions and Methods Simple Variables Pointers ReferencesThe Implicit ArgumentthisThe implicit argument is passed as a 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 9/31Outline Functions and Methods Simple Variables Pointers ReferencesSimple variablesCPSC 427a 10/31Outline 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 11/31Outline Functions and Methods Simple Variables Pointers ReferencesSimple variable declarationThe declaration int x = 3; says several things:1. The 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 12/31Outline Functions and Methods Simple Variables Pointers ReferencesSimple assignmentThe assignment statement x = 3; means the following:1. Get an L-value from the left hand side (x).2. Get an R-value from the right hand side (3).3. Put the R value from step 2 into the storage location whoseaddress was obtained from step 1.CPSC 427a 13/31Outline Functions and Methods Simple Variables Pointers ReferencesAutomatic dereferencingGivenint x = 3;int y = 4;Considerx = y;This is processed as before, except what does it mean to get anR-value from y?Whenever an L-value is presented and an R-value is needed,automatic deferencing occurs.This means to go the storage location specified by the presentedL-value (y) and fetching its contents (4).Then the assignment takes place as before.CPSC 427a 14/31Outline Functions and Methods Simple Variables Pointers ReferencesPointersCPSC 427a 15/31Outline Functions and Methods Simple Variables Pointers ReferencesPointer valuesIA pointer is a primitive object with an associated L-value.IThe pointer itself is an R-value.IThe type of a pointer is the type of the value that can bestored at the associated L-value, followed by *IExample: If y is a simple integer variable, then the type of apointer to y is int*.IWe say the pointer references y.CPSC 427a 16/31Outline Functions and Methods Simple Variables Pointers ReferencesPointer creationIPointers are created by applying the unary operator & to anL-value.IExample: If y has type int, then the expression &y is apointer of type int* that references y.IMore generally, if x has type T, then the expression &x yields apointer (R-value) of type T* that references x.CPSC 427a 17/31Outline Functions and Methods Simple Variables Pointers ReferencesPointer variablesVariables into which pointers can be stored are called (notsurprisingly) pointer variables.A pointer variable is no different from any other variable except forthe types of values that can be stored in it.Iint* q declares q


View Full Document

Yale CPSC 427 - Lecture 6

Download Lecture 6
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 6 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 6 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?