Top Banner
Objects with Functions and Arrays
21

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

Jan 18, 2018

Download

Documents

Clyde Gray

Returning an Object To return object: – Make desired object – Return it Use function:
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
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