66
YHL Reference 1 ECE 462 Object-Oriented Programming using C++ and Java Reference in C++ Yung-Hsiang Lu [email protected]

ECE 462 Object-Oriented Programming using C++ and Java ...using C++ and Java Parameter Passing in Functions Yung-Hsiang Lu [email protected]. YHL Parameter Passing 2 Parameter Passing

  • Upload
    others

  • View
    22

  • Download
    0

Embed Size (px)

Citation preview

Page 1: ECE 462 Object-Oriented Programming using C++ and Java ...using C++ and Java Parameter Passing in Functions Yung-Hsiang Lu yunglu@purdue.edu. YHL Parameter Passing 2 Parameter Passing

YHL Reference 1

ECE 462Object-Oriented Programming

using C++ and Java

Reference in C++

Yung-Hsiang [email protected]

Page 2: ECE 462 Object-Oriented Programming using C++ and Java ...using C++ and Java Parameter Passing in Functions Yung-Hsiang Lu yunglu@purdue.edu. YHL Parameter Passing 2 Parameter Passing

YHL Reference 2

Reference (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 100

ir

Page 3: ECE 462 Object-Oriented Programming using C++ and Java ...using C++ and Java Parameter Passing in Functions Yung-Hsiang Lu yunglu@purdue.edu. YHL Parameter Passing 2 Parameter Passing

YHL Reference 3

modified from ReferenceClassType.cc in the textbook

& means reference

& means address

Page 4: ECE 462 Object-Oriented Programming using C++ and Java ...using C++ and Java Parameter Passing in Functions Yung-Hsiang Lu yunglu@purdue.edu. YHL Parameter Passing 2 Parameter Passing

YHL Reference 4

User* p = &u1; u1p

p = u2; u2p (u1 is not affected.)

Page 5: ECE 462 Object-Oriented Programming using C++ and Java ...using C++ and Java Parameter Passing in Functions Yung-Hsiang Lu yunglu@purdue.edu. YHL Parameter Passing 2 Parameter Passing

YHL Reference 5

Self Test

Page 6: ECE 462 Object-Oriented Programming using C++ and Java ...using C++ and Java Parameter Passing in Functions Yung-Hsiang Lu yunglu@purdue.edu. YHL Parameter Passing 2 Parameter Passing

YHL Parameter Passing 1

ECE 462Object-Oriented Programming

using C++ and Java

Parameter Passing in Functions

Yung-Hsiang [email protected]

Page 7: ECE 462 Object-Oriented Programming using C++ and Java ...using C++ and Java Parameter Passing in Functions Yung-Hsiang Lu yunglu@purdue.edu. YHL Parameter Passing 2 Parameter Passing

YHL Parameter Passing 2

Parameter Passing in C++ and Java

pass by reference, primitive typefunc(int & x) { x = 94; }func(y); // calling// change seen by caller (y is 94)

none

pass by pointer, primitive typefunc(int * x) { *x = -1; }func(& y); // calling// change seen by caller (y is -1)

none

samefunc(int x) { x = 0; }y = 6;func(y); // callingy is still 6

pass by value, primitive types (int, char, double ...)

func(int x) { x = new_value; }// change not seen by caller

C++Java

Page 8: ECE 462 Object-Oriented Programming using C++ and Java ...using C++ and Java Parameter Passing in Functions Yung-Hsiang Lu yunglu@purdue.edu. YHL Parameter Passing 2 Parameter Passing

YHL Parameter Passing 3

C++Java

pass by reference, objectfunc(AClass & obj)

{ obj.att = new_value; }// change seen by caller// reference = alias

none

pass by pointer, objectfunc(AClass * obj) { obj ->att = new_value; }func(& x); // calling// change seen by caller

none

pass by value, objectAClass x;func(AClass obj) { obj.att = new_value; }func(x); // calling// object copied before passing// change not seen by caller

same syntaxbut change seen by callerthe behavior, however, is similar to “pointer”

Page 9: ECE 462 Object-Oriented Programming using C++ and Java ...using C++ and Java Parameter Passing in Functions Yung-Hsiang Lu yunglu@purdue.edu. YHL Parameter Passing 2 Parameter Passing

YHL Parameter Passing 4

Pass by Value

Page 10: ECE 462 Object-Oriented Programming using C++ and Java ...using C++ and Java Parameter Passing in Functions Yung-Hsiang Lu yunglu@purdue.edu. YHL Parameter Passing 2 Parameter Passing

YHL 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 seen

Page 11: ECE 462 Object-Oriented Programming using C++ and Java ...using C++ and Java Parameter Passing in Functions Yung-Hsiang Lu yunglu@purdue.edu. YHL Parameter Passing 2 Parameter Passing

YHL 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)

}

Page 12: ECE 462 Object-Oriented Programming using C++ and Java ...using C++ and Java Parameter Passing in Functions Yung-Hsiang Lu yunglu@purdue.edu. YHL Parameter Passing 2 Parameter Passing

YHL Parameter Passing 7

Pass by Pointer

Page 13: ECE 462 Object-Oriented Programming using C++ and Java ...using C++ and Java Parameter Passing in Functions Yung-Hsiang Lu yunglu@purdue.edu. YHL Parameter Passing 2 Parameter Passing

YHL Parameter Passing 8

no * in front of q

* in front of q

Page 14: ECE 462 Object-Oriented Programming using C++ and Java ...using C++ and Java Parameter Passing in Functions Yung-Hsiang Lu yunglu@purdue.edu. YHL Parameter Passing 2 Parameter Passing

YHL Parameter Passing 9

Why doesn't g change x?

g( int* q ) xq

int* p = &x; xp

p

int y = 200; q = &y; yq

Changing q assigns the pointer to another location.To change the content, we need to use * q (in function h).

Page 15: ECE 462 Object-Oriented Programming using C++ and Java ...using C++ and Java Parameter Passing in Functions Yung-Hsiang Lu yunglu@purdue.edu. YHL Parameter Passing 2 Parameter Passing

YHL Parameter Passing 10

Pass by Reference

Page 16: ECE 462 Object-Oriented Programming using C++ and Java ...using C++ and Java Parameter Passing in Functions Yung-Hsiang Lu yunglu@purdue.edu. YHL Parameter Passing 2 Parameter Passing

YHL 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)

Page 17: ECE 462 Object-Oriented Programming using C++ and Java ...using C++ and Java Parameter Passing in Functions Yung-Hsiang Lu yunglu@purdue.edu. YHL Parameter Passing 2 Parameter Passing

YHL Parameter Passing 12

not changed

C++

Page 18: ECE 462 Object-Oriented Programming using C++ and Java ...using C++ and Java Parameter Passing in Functions Yung-Hsiang Lu yunglu@purdue.edu. YHL Parameter Passing 2 Parameter Passing

YHL Parameter Passing 13

Java

changed

Page 19: ECE 462 Object-Oriented Programming using C++ and Java ...using C++ and Java Parameter Passing in Functions Yung-Hsiang Lu yunglu@purdue.edu. YHL Parameter Passing 2 Parameter Passing

YHL Parameter Passing 14

C++

changed

Page 20: ECE 462 Object-Oriented Programming using C++ and Java ...using C++ and Java Parameter Passing in Functions Yung-Hsiang Lu yunglu@purdue.edu. YHL Parameter Passing 2 Parameter Passing

YHL Parameter Passing 15

Self Test

Page 21: ECE 462 Object-Oriented Programming using C++ and Java ...using C++ and Java Parameter Passing in Functions Yung-Hsiang Lu yunglu@purdue.edu. YHL Parameter Passing 2 Parameter Passing

YHL Copy Constructor 1

ECE 462Object-Oriented Programming

using C++ and Java

Copy Constructor

Yung-Hsiang [email protected]

Page 22: ECE 462 Object-Oriented Programming using C++ and Java ...using C++ and Java Parameter Passing in Functions Yung-Hsiang Lu yunglu@purdue.edu. YHL Parameter Passing 2 Parameter Passing

YHL Copy Constructor 2

C++ Copy Constructor

class 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

}}

Page 23: ECE 462 Object-Oriented Programming using C++ and Java ...using C++ and Java Parameter Passing in Functions Yung-Hsiang Lu yunglu@purdue.edu. YHL Parameter Passing 2 Parameter Passing

YHL Copy Constructor 3

Assignment 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;

}}

Page 24: ECE 462 Object-Oriented Programming using C++ and Java ...using C++ and Java Parameter Passing in Functions Yung-Hsiang Lu yunglu@purdue.edu. YHL Parameter Passing 2 Parameter Passing

YHL Copy Constructor 4

Page 25: ECE 462 Object-Oriented Programming using C++ and Java ...using C++ and Java Parameter Passing in Functions Yung-Hsiang Lu yunglu@purdue.edu. YHL Parameter Passing 2 Parameter Passing

YHL Copy Constructor 5

Page 26: ECE 462 Object-Oriented Programming using C++ and Java ...using C++ and Java Parameter Passing in Functions Yung-Hsiang Lu yunglu@purdue.edu. YHL Parameter Passing 2 Parameter Passing

YHL Copy Constructor 6

Page 27: ECE 462 Object-Oriented Programming using C++ and Java ...using C++ and Java Parameter Passing in Functions Yung-Hsiang Lu yunglu@purdue.edu. YHL Parameter Passing 2 Parameter Passing

YHL Copy Constructor 7

Shallow 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.

object1

data_array

object2

Page 28: ECE 462 Object-Oriented Programming using C++ and Java ...using C++ and Java Parameter Passing in Functions Yung-Hsiang Lu yunglu@purdue.edu. YHL Parameter Passing 2 Parameter Passing

YHL Copy Constructor 8

put copy constructor in a comment

put operator = in a comment

Page 29: ECE 462 Object-Oriented Programming using C++ and Java ...using C++ and Java Parameter Passing in Functions Yung-Hsiang Lu yunglu@purdue.edu. YHL Parameter Passing 2 Parameter Passing

YHL Copy Constructor 9

Page 30: ECE 462 Object-Oriented Programming using C++ and Java ...using C++ and Java Parameter Passing in Functions Yung-Hsiang Lu yunglu@purdue.edu. YHL Parameter Passing 2 Parameter Passing

YHL Copy Constructor 10

Page 31: ECE 462 Object-Oriented Programming using C++ and Java ...using C++ and Java Parameter Passing in Functions Yung-Hsiang Lu yunglu@purdue.edu. YHL Parameter Passing 2 Parameter Passing

YHL Copy Constructor 11

make the attributes public

Page 32: ECE 462 Object-Oriented Programming using C++ and Java ...using C++ and Java Parameter Passing in Functions Yung-Hsiang Lu yunglu@purdue.edu. YHL Parameter Passing 2 Parameter Passing

YHL Copy Constructor 12

change a value in x1

Page 33: ECE 462 Object-Oriented Programming using C++ and Java ...using C++ and Java Parameter Passing in Functions Yung-Hsiang Lu yunglu@purdue.edu. YHL Parameter Passing 2 Parameter Passing

YHL Copy Constructor 13

value in x2 not changed

Page 34: ECE 462 Object-Oriented Programming using C++ and Java ...using C++ and Java Parameter Passing in Functions Yung-Hsiang Lu yunglu@purdue.edu. YHL Parameter Passing 2 Parameter Passing

YHL Copy Constructor 14

Page 35: ECE 462 Object-Oriented Programming using C++ and Java ...using C++ and Java Parameter Passing in Functions Yung-Hsiang Lu yunglu@purdue.edu. YHL Parameter Passing 2 Parameter Passing

YHL Copy Constructor 15

value in x2 changed, then the program crashes

Page 36: ECE 462 Object-Oriented Programming using C++ and Java ...using C++ and Java Parameter Passing in Functions Yung-Hsiang Lu yunglu@purdue.edu. YHL Parameter Passing 2 Parameter Passing

YHL Copy Constructor 16

How 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).

Page 37: ECE 462 Object-Oriented Programming using C++ and Java ...using C++ and Java Parameter Passing in Functions Yung-Hsiang Lu yunglu@purdue.edu. YHL Parameter Passing 2 Parameter Passing

YHL Copy Constructor 17

Self Test

Page 38: ECE 462 Object-Oriented Programming using C++ and Java ...using C++ and Java Parameter Passing in Functions Yung-Hsiang Lu yunglu@purdue.edu. YHL Parameter Passing 2 Parameter Passing

YHL Parameter Passing 2 1

ECE 462Object-Oriented Programming

using C++ and Java

Parameter Passing in Functions II

Yung-Hsiang [email protected]

Page 39: ECE 462 Object-Oriented Programming using C++ and Java ...using C++ and Java Parameter Passing in Functions Yung-Hsiang Lu yunglu@purdue.edu. YHL Parameter Passing 2 Parameter Passing

YHL Parameter Passing 2 2

C++ Parameter Passing and Copying

pass by reference, objectfunc(AClass & obj) { ... }// object not copied, change seen by caller

pass by pointer, objectfunc(AClass * x) { ... }

// object not copied, change seen by caller

pass by value, objectfunc(AClass obj) { ... }// object copied before passing, change not seen by caller

Page 40: ECE 462 Object-Oriented Programming using C++ and Java ...using C++ and Java Parameter Passing in Functions Yung-Hsiang Lu yunglu@purdue.edu. YHL Parameter Passing 2 Parameter Passing

YHL Parameter Passing 2 3

Return Object and Copying

return referenceAClass & func(...) { ... }// object not copied, cannot return a local object

return pointer AClass * func(...) { ... } // object not copied, cannot return a local object

return objectAClass func(...) { ... }// object copied before return

Page 41: ECE 462 Object-Oriented Programming using C++ and Java ...using C++ and Java Parameter Passing in Functions Yung-Hsiang Lu yunglu@purdue.edu. YHL Parameter Passing 2 Parameter Passing

YHL Parameter Passing 2 4

Page 42: ECE 462 Object-Oriented Programming using C++ and Java ...using C++ and Java Parameter Passing in Functions Yung-Hsiang Lu yunglu@purdue.edu. YHL Parameter Passing 2 Parameter Passing

YHL Parameter Passing 2 5

Page 43: ECE 462 Object-Oriented Programming using C++ and Java ...using C++ and Java Parameter Passing in Functions Yung-Hsiang Lu yunglu@purdue.edu. YHL Parameter Passing 2 Parameter Passing

YHL Parameter Passing 2 6

Page 44: ECE 462 Object-Oriented Programming using C++ and Java ...using C++ and Java Parameter Passing in Functions Yung-Hsiang Lu yunglu@purdue.edu. YHL Parameter Passing 2 Parameter Passing

YHL Parameter Passing 2 7

Page 45: ECE 462 Object-Oriented Programming using C++ and Java ...using C++ and Java Parameter Passing in Functions Yung-Hsiang Lu yunglu@purdue.edu. YHL Parameter Passing 2 Parameter Passing

YHL Parameter Passing 2 8

Page 46: ECE 462 Object-Oriented Programming using C++ and Java ...using C++ and Java Parameter Passing in Functions Yung-Hsiang Lu yunglu@purdue.edu. YHL Parameter Passing 2 Parameter Passing

YHL Parameter Passing 2 9

Page 47: ECE 462 Object-Oriented Programming using C++ and Java ...using C++ and Java Parameter Passing in Functions Yung-Hsiang Lu yunglu@purdue.edu. YHL Parameter Passing 2 Parameter Passing

YHL Parameter Passing 2 10

Page 48: ECE 462 Object-Oriented Programming using C++ and Java ...using C++ and Java Parameter Passing in Functions Yung-Hsiang Lu yunglu@purdue.edu. YHL Parameter Passing 2 Parameter Passing

YHL Parameter Passing 2 11

Self Test

Page 49: ECE 462 Object-Oriented Programming using C++ and Java ...using C++ and Java Parameter Passing in Functions Yung-Hsiang Lu yunglu@purdue.edu. YHL Parameter Passing 2 Parameter Passing

YHL Overload Resolution 1

ECE 462Object-Oriented Programming

using C++ and Java

Overload Resolution

Yung-Hsiang [email protected]

Page 50: ECE 462 Object-Oriented Programming using C++ and Java ...using C++ and Java Parameter Passing in Functions Yung-Hsiang Lu yunglu@purdue.edu. YHL Parameter Passing 2 Parameter Passing

YHL Overload Resolution 2

Overloading (by Parameters Types)

Overloading: same function name, same return type, different input parameters (numbers, types, or both)

Overload cannot be resolved by the return type.

example: Java Color class has 7 constructors• Color (ColorSpace cspace, float[] components, float alpha)

Creates a color in the specified ColorSpace with the color components specified in the float array and the specified alpha.(alpha: transparency)

• Color(float r, float g, float b) Creates an opaque sRGB color with the specified red, green, and blue values in the range (0.0 - 1.0).

Page 51: ECE 462 Object-Oriented Programming using C++ and Java ...using C++ and Java Parameter Passing in Functions Yung-Hsiang Lu yunglu@purdue.edu. YHL Parameter Passing 2 Parameter Passing

YHL Overload Resolution 3

• Color(float r, float g, float b, float a) Creates an sRGB color with the specified red, green, blue, and alpha values in the range (0.0 - 1.0).

• Color(int rgb) Creates an opaque sRGB color with the specified combined RGB value consisting of the red component in bits 16-23, the green component in bits 8-15, and the blue component in bits 0-7.

• Color(int r, int g, int b) Creates an opaque sRGB color with the specified red, green, and blue values in the range (0 - 255).... 2 more constructors ... check

Why to overload? convenience: you may have different types of informationfrequently used in constructors

Page 52: ECE 462 Object-Oriented Programming using C++ and Java ...using C++ and Java Parameter Passing in Functions Yung-Hsiang Lu yunglu@purdue.edu. YHL Parameter Passing 2 Parameter Passing

YHL Overload Resolution 4

Page 53: ECE 462 Object-Oriented Programming using C++ and Java ...using C++ and Java Parameter Passing in Functions Yung-Hsiang Lu yunglu@purdue.edu. YHL Parameter Passing 2 Parameter Passing

YHL Overload Resolution 5

Similarly, QColor also hasoverloaded constructors.

Page 54: ECE 462 Object-Oriented Programming using C++ and Java ...using C++ and Java Parameter Passing in Functions Yung-Hsiang Lu yunglu@purdue.edu. YHL Parameter Passing 2 Parameter Passing

YHL Overload Resolution 6

overloaded functionsin Java Object

Page 55: ECE 462 Object-Oriented Programming using C++ and Java ...using C++ and Java Parameter Passing in Functions Yung-Hsiang Lu yunglu@purdue.edu. YHL Parameter Passing 2 Parameter Passing

YHL Overload Resolution 7

overloaded functionsin QHash

Page 56: ECE 462 Object-Oriented Programming using C++ and Java ...using C++ and Java Parameter Passing in Functions Yung-Hsiang Lu yunglu@purdue.edu. YHL Parameter Passing 2 Parameter Passing

YHL Overload Resolution 8

Overload Resolution(how to find the right function to call)

• If there is an exact match, the function is called.– (int, double, User) → f(int, double, User)– (String, User, Color) → f(String, User, Color)

• If there is no exact match, the compiler will find the "closest" match by– type conversion through promotion (no information

loss), eg. short → int, char → int, float → double – not const → const– C++: standard conversion (information may be lost),

eg . double → int, int → double– derived class → base class

Page 57: ECE 462 Object-Oriented Programming using C++ and Java ...using C++ and Java Parameter Passing in Functions Yung-Hsiang Lu yunglu@purdue.edu. YHL Parameter Passing 2 Parameter Passing

YHL Overload Resolution 9

Overload Resolution

• If two (or more) matches are equally close ⇒ error• determined at compilation time, not run-time• C++ allows default values, as one format of overloading

func(int x, int y = 8) { ... }func(5); // same as calling func(5,8);func(5, -11); // x = 5 and y = -11// always start from the last parameterfunc(int x = 3, int y) { ... } // errorfunc(int x, double y = 0.0, int z = -10) { ... } // OK

Page 58: ECE 462 Object-Oriented Programming using C++ and Java ...using C++ and Java Parameter Passing in Functions Yung-Hsiang Lu yunglu@purdue.edu. YHL Parameter Passing 2 Parameter Passing

YHL Overload Resolution 10

Differences between Java and C++

• Java allows conversions without losing information (6.7.2):– byte → short, int, long, float, or double– int → long, float, or double– long → float or double– ...

• Java does not allow programmer-defined conversion.• Java does not allow ellipsis (undefined number of

parameters, should also be avoided in C++)

Page 59: ECE 462 Object-Oriented Programming using C++ and Java ...using C++ and Java Parameter Passing in Functions Yung-Hsiang Lu yunglu@purdue.edu. YHL Parameter Passing 2 Parameter Passing

YHL Overload Resolution 11

C++ Overload using Primitive Types

Page 60: ECE 462 Object-Oriented Programming using C++ and Java ...using C++ and Java Parameter Passing in Functions Yung-Hsiang Lu yunglu@purdue.edu. YHL Parameter Passing 2 Parameter Passing

YHL Overload Resolution 12

Page 61: ECE 462 Object-Oriented Programming using C++ and Java ...using C++ and Java Parameter Passing in Functions Yung-Hsiang Lu yunglu@purdue.edu. YHL Parameter Passing 2 Parameter Passing

YHL Overload Resolution 13

Page 62: ECE 462 Object-Oriented Programming using C++ and Java ...using C++ and Java Parameter Passing in Functions Yung-Hsiang Lu yunglu@purdue.edu. YHL Parameter Passing 2 Parameter Passing

YHL Overload Resolution 14

C++ Overload with Objects

Page 63: ECE 462 Object-Oriented Programming using C++ and Java ...using C++ and Java Parameter Passing in Functions Yung-Hsiang Lu yunglu@purdue.edu. YHL Parameter Passing 2 Parameter Passing

YHL Overload Resolution 15

Page 64: ECE 462 Object-Oriented Programming using C++ and Java ...using C++ and Java Parameter Passing in Functions Yung-Hsiang Lu yunglu@purdue.edu. YHL Parameter Passing 2 Parameter Passing

YHL Overload Resolution 16

error

f1(CD1, CD2) and f1(CD2, CD1)equally close

Page 65: ECE 462 Object-Oriented Programming using C++ and Java ...using C++ and Java Parameter Passing in Functions Yung-Hsiang Lu yunglu@purdue.edu. YHL Parameter Passing 2 Parameter Passing

YHL Overload Resolution 17

no errorf1(CD1, CD2) is the closest

Page 66: ECE 462 Object-Oriented Programming using C++ and Java ...using C++ and Java Parameter Passing in Functions Yung-Hsiang Lu yunglu@purdue.edu. YHL Parameter Passing 2 Parameter Passing

YHL Overload Resolution 18

Self Test