DOC PREVIEW
Purdue ECE 462 - Reference in C++

This preview shows page 1-2-3-4-31-32-33-34-35-63-64-65-66 out of 66 pages.

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

Unformatted text preview:

Reference in C++Parameter Passing in FunctionsPass by ValuePass by PointerPass by ReferenceCopy ConstructorC++ Copy ConstructorAssignment Operator =Shallow or Deep CopyHow about Java?Parameter Passing in Functions IIReturn Object and CopyingOverload ResolutionDifferences between Java and C++C++ Overload usingPrimitive TypesC++ Overload with ObjectsYHL Reference 1ECE 462Object-Oriented Programmingusing C++ and JavaReference in C++Yung-Hsiang [email protected] Reference 2Reference (alias) in C++• Reference is alias.– int i = 2;–int& r = i;–r = 3; ⇒ i is 3 now.• Reference is similar to a pointer but reference, once assigned, cannot be reassigned.–r ++; ⇒ i is 4 now. // different from pointer arithmetics– int j = 100;–r = j; ⇒ i is 100irYHL Reference 3modified from ReferenceClassType.cc in the textbook& means reference& means addressYHL Reference 4User* p = &u1; u1pp = u2; u2p(u1 is not affected.)YHL Reference 5Self TestYHL Parameter Passing 1ECE 462Object-Oriented Programmingusing C++ and JavaParameter Passing in FunctionsYung-Hsiang [email protected] Parameter Passing 2Parameter Passing in C++ and Javapass by reference, primitive typefunc(int & x) { x = 94; }func(y); // calling// change seen by caller (y is 94)nonepass by pointer, primitive typefunc(int * x) { *x = -1; }func(& y); // calling// change seen by caller (y is -1)nonesamefunc(int x) { x = 0; }y = 6;func(y); // callingy is still 6pass by value, primitive types (int, char, double ...)func(int x) { x = new_value; }// change not seen by callerC++JavaYHL Parameter Passing 3C++Javapass by reference, objectfunc(AClass & obj) { obj.att = new_value; }// change seen by caller// reference = aliasnonepass by pointer, objectfunc(AClass * obj) { obj ->att = new_value; }func(& x); // calling// change seen by callernonepass by value, objectAClass x;func(AClass obj) { obj.att = new_value; }func(x); // calling// object copied before passing// change not seen by callersame syntaxbut change seen by callerthe behavior, however, is similar to “pointer”YHL Parameter Passing 4Pass by ValueYHL Parameter Passing 5//PassPrimByValue.cc#include <iostream> using namespace std; void g( int ); int main() { int x = 100; g(x); cout << x << endl; // 100 return 0; } void g( int y ) { y++; } // change will not been seenYHL Parameter Passing 6//PassPrimByValue.javaclass Test { public static void main( String[] args ) { int x = 100; //(A) g(x); //(B) System.out.println( x ); // outputs 100 } static void g( int y ) { y++; } //(C) }YHL Parameter Passing 7Pass by PointerYHL Parameter Passing 8no * in front of q* in front of qYHL Parameter Passing 9Why doesn't g change x?g( int* q )xqint* p = &x; xppint y = 200; q = &y; yqChanging q assigns the pointer to another location.To change the content, we need to use * q (in function h).YHL Parameter Passing 10Pass by ReferenceYHL Parameter Passing 11//PassPrimByRef.cc#include <iostream>using namespace std;void g( int& );int main(){int x = 100;g(x); //(A)cout << x << endl; // 101 //(B)return 0;}void g( int& y ) { y++; } //(C)YHL Parameter Passing 12not changedC++YHL Parameter Passing 13JavachangedYHL Parameter Passing 14C++changedYHL Parameter Passing 15Self TestYHL Copy Constructor 1ECE 462Object-Oriented Programmingusing C++ and JavaCopy ConstructorYung-Hsiang [email protected] Copy Constructor 2C++ Copy Constructorclass NameOfClass{NameOfClass(const NameOfClass & origobj) {// a constructor with special syntax// If a programmer does not provide a copy constructor// C++ compiler automatically creates one// allocate memory for attributes, if necessary// copy each attributes from origobj to this new object}}YHL Copy Constructor 3Assignment Operator = class NameOfClass{NameOfClass& operator = (const NameOfClass & origobj) {// C++ compiler automatically creates one, if not by a programmerif (this != & origobj){// release memory, if necessary// allocate memory for attributes, if necessary// copy each attribute from origobj to this new object}return * this;}}YHL Copy Constructor 4YHL Copy Constructor 5YHL Copy Constructor 6YHL Copy Constructor 7Shallow or Deep Copy• If you do not provide a copy constructor (or an assignment operator), C++ will create one for you.• The one created by the compiler copies the values of attributes.• When an attribute is a pointer, two objects' attributes point to the same location.• If one object releases the memory (for example, by the destructor), the second cannot access the memory any more. object1data_arrayobject2YHL Copy Constructor 8put copy constructor in a commentput operator = in a commentYHL Copy Constructor 9YHL Copy Constructor 10YHL Copy Constructor 11make the attributes publicYHL Copy Constructor 12change a value in x1YHL Copy Constructor 13value in x2 not changedYHL Copy Constructor 14YHL Copy Constructor 15value in x2 changed, then the program crashesYHL Copy Constructor 16How about Java?• Java does not need (neither allow) copy constructor.• Java does not allow programmer-defined operator overloading. • Java performs garbage collection. Destructors are not needed (and not allowed).YHL Copy Constructor 17Self TestYHL Parameter Passing 2 1ECE 462Object-Oriented Programmingusing C++ and JavaParameter Passing in Functions IIYung-Hsiang [email protected] Parameter Passing 2 2C++ Parameter Passing and Copyingpass by reference, objectfunc(AClass & obj) { ... }// object not copied, change seen by callerpass by pointer, objectfunc(AClass * x) { ... }// object not copied, change seen by callerpass by value, objectfunc(AClass obj) { ... }// object copied before passing, change not seen by callerYHL Parameter Passing 2 3Return Object and Copyingreturn referenceAClass & func(...) { ... }// object not copied, cannot return a local objectreturn pointer AClass * func(...) { ... } // object not copied, cannot return a local objectreturn objectAClass func(...) { ... }// object copied before returnYHL Parameter Passing 2 4YHL Parameter Passing 2 5YHL Parameter Passing 2 6YHL Parameter Passing 2 7YHL Parameter Passing 2 8YHL Parameter Passing 2 9YHL Parameter Passing 2 10YHL Parameter Passing 2 11Self TestYHL Overload Resolution 1ECE 462Object-Oriented Programmingusing C++ and JavaOverload ResolutionYung-Hsiang [email protected] Overload Resolution 2Overloading (by


View Full Document

Purdue ECE 462 - Reference in C++

Download Reference in C++
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 Reference in C++ 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 Reference in C++ 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?