+ All Categories
Home > Documents > Objects with Functions and Arrays. Objects can be Passed Class defines type Can use as type of...

Objects with Functions and Arrays. Objects can be Passed Class defines type Can use as type of...

Date post: 18-Jan-2018
Category:
Upload: clyde-gray
View: 218 times
Download: 0 times
Share this document with a friend
Description:
Returning an Object To return object: – Make desired object – Return it Use function:
21
Objects with Functions and Arrays
Transcript
Page 1: Objects with Functions and Arrays. Objects can be Passed Class defines type  Can use as type of function or parameter.

Objects with Functionsand Arrays

Page 2: Objects with Functions and Arrays. Objects can be Passed Class defines type  Can use as type of function or parameter.

Objects can be Passed

• Class defines type– Can use as type of function or parameter

Page 3: Objects with Functions and Arrays. Objects can be Passed Class defines type  Can use as type of function or parameter.

Returning an Object

• To return object:– Make desired object– Return it

• Use function:

Page 4: Objects with Functions and Arrays. Objects can be Passed Class defines type  Can use as type of function or parameter.

Passing An Object

• Passing object copies the object:

• Main:

c1

radius: 5

Page 5: Objects with Functions and Arrays. Objects can be Passed Class defines type  Can use as type of function or parameter.

Passing An Object

• Passing object copies the object:

• Main:

c1

radius: 5

c

radius: 5

Page 6: Objects with Functions and Arrays. Objects can be Passed Class defines type  Can use as type of function or parameter.

Copies Generally Bad

• Copies– Are inefficient– Prevent functions from modifying an object

• Main:c1

radius: 5

Page 7: Objects with Functions and Arrays. Objects can be Passed Class defines type  Can use as type of function or parameter.

Copies Generally Bad

• Copies– Are inefficient– Prevent functions from modifying an object

• Main:c1

radius: 5

c

radius: 5

Page 8: Objects with Functions and Arrays. Objects can be Passed Class defines type  Can use as type of function or parameter.

Copies Generally Bad

• Copies– Are inefficient– Prevent functions from modifying an object

• Main:c1

radius: 5

c

radius: 10

Page 9: Objects with Functions and Arrays. Objects can be Passed Class defines type  Can use as type of function or parameter.

Copies Generally Bad

• Copies– Are inefficient– Prevent functions from modifying an object

• Main:c1

radius: 5

Page 10: Objects with Functions and Arrays. Objects can be Passed Class defines type  Can use as type of function or parameter.

Pass By Reference

• Unless you need copy, pass by reference

• Main:c1

radius: 5

Page 11: Objects with Functions and Arrays. Objects can be Passed Class defines type  Can use as type of function or parameter.

Pass By Reference

• Unless you need copy, pass by reference

• Main:c1

radius: 5

c

Page 12: Objects with Functions and Arrays. Objects can be Passed Class defines type  Can use as type of function or parameter.

Pass By Reference

• Unless you need copy, pass by reference

• Main:c1

radius: 10

c

Page 13: Objects with Functions and Arrays. Objects can be Passed Class defines type  Can use as type of function or parameter.

Pass By Reference

• Unless you need copy, pass by reference

• Main:c1

radius: 10

Page 14: Objects with Functions and Arrays. Objects can be Passed Class defines type  Can use as type of function or parameter.

Const Reference

• To prevent function from modifying, use const reference:

Link back to original object

Make changing it a compile error

Page 15: Objects with Functions and Arrays. Objects can be Passed Class defines type  Can use as type of function or parameter.

Arrays

• Can have an array of objectsCircleList

0 radius: 2

1 radius: 2

2 radius: 2

3 radius: 2

4 radius: 2

Page 16: Objects with Functions and Arrays. Objects can be Passed Class defines type  Can use as type of function or parameter.

Arrays

• Reference individual objectsby index CircleList

0 radius: 4

1 radius: 2

2 radius: 2

3 radius: 2

4 radius: 2

Page 17: Objects with Functions and Arrays. Objects can be Passed Class defines type  Can use as type of function or parameter.

Arrays

• Arrays initialized with defaultconstructor CircleList

0 radius: 2

1 radius: 2

2 radius: 2

3 radius: 2

4 radius: 2

Page 18: Objects with Functions and Arrays. Objects can be Passed Class defines type  Can use as type of function or parameter.

Arrays

• This makes 10 circles:

Page 19: Objects with Functions and Arrays. Objects can be Passed Class defines type  Can use as type of function or parameter.

Arrays

• This makes 10 circles:CircleList

0 radius: 2

1 radius: 2

2 radius: 2

3 radius: 2

4 radius: 2

Page 20: Objects with Functions and Arrays. Objects can be Passed Class defines type  Can use as type of function or parameter.

Arrays

• This makes 10 circles:CircleList

0 radius: 1

1 radius: 2

2 radius: 3

3 radius: 4

4 radius: 5

Page 21: Objects with Functions and Arrays. Objects can be Passed Class defines type  Can use as type of function or parameter.

Arrays

• Use initialization list to prevent filling with default objects:

CircleList

0 radius: 1

1 radius: 2

2 radius: 3

3 radius: 4

4 radius: 5Call 1-arg constructor to make anonymous object


Recommended