17
Friend Functions Friend Functions 04/12/10 04/12/10

Friend Functions 04/12/10. Today Use reference parameters when passing an object. Use friend functions with a class

Embed Size (px)

Citation preview

Page 1: Friend Functions 04/12/10. Today  Use reference parameters when passing an object.  Use friend functions with a class

Friend FunctionsFriend Functions

04/12/1004/12/10

Page 2: Friend Functions 04/12/10. Today  Use reference parameters when passing an object.  Use friend functions with a class

TodayToday

Use reference parameters when passing an Use reference parameters when passing an object.object.

Use friend functions with a class.Use friend functions with a class.

Page 3: Friend Functions 04/12/10. Today  Use reference parameters when passing an object.  Use friend functions with a class

Pass Objects By-referencePass Objects By-reference

More efficientMore efficient Opens possibility of changing ObjectOpens possibility of changing Object const const in front to protect in front to protect

Sec6.4/point/point1.cppSec6.4/point/point1.cpp Equal function compares two pointsEqual function compares two points

Page 4: Friend Functions 04/12/10. Today  Use reference parameters when passing an object.  Use friend functions with a class

Friend FunctionsFriend Functions

Page 5: Friend Functions 04/12/10. Today  Use reference parameters when passing an object.  Use friend functions with a class

Use of Accessors AwkwardUse of Accessors Awkward

An equal function is in the point program.An equal function is in the point program. sec6.4/point1.cppsec6.4/point1.cpp

Used accessors, but awkwardUsed accessors, but awkward Instead use friend functions.Instead use friend functions.

Sec6.4/point1a.cppSec6.4/point1a.cpp

Page 6: Friend Functions 04/12/10. Today  Use reference parameters when passing an object.  Use friend functions with a class

Friend functionFriend function

Allows access to private member variablesAllows access to private member variables Doesn't have to use accessorsDoesn't have to use accessors Placed in class definitionPlaced in class definition "friend" in front of definition"friend" in front of definition Is Is not not a member functiona member function

Definition does not need class nameDefinition does not need class name

Page 7: Friend Functions 04/12/10. Today  Use reference parameters when passing an object.  Use friend functions with a class

When to Use friend FunctionWhen to Use friend Function

Function involving two objectsFunction involving two objects Objects equally importantObjects equally important Not appropriate to make it a member Not appropriate to make it a member

function of either objectfunction of either object

Page 8: Friend Functions 04/12/10. Today  Use reference parameters when passing an object.  Use friend functions with a class

Midpoint ExampleMidpoint Example

Want to add a function to the point class Want to add a function to the point class that finds the midpoint of two points.that finds the midpoint of two points.

Why should it be a friend function.Why should it be a friend function.

Example of a function that returns an Example of a function that returns an

object.object.

point1b.cpppoint1b.cpp

Page 9: Friend Functions 04/12/10. Today  Use reference parameters when passing an object.  Use friend functions with a class

Another exampleAnother example

Sec6.4/timer/TIMERSec6.4/timer/TIMER Make a copy of timer.cpp to work on.Make a copy of timer.cpp to work on.

Page 10: Friend Functions 04/12/10. Today  Use reference parameters when passing an object.  Use friend functions with a class

Overloaded OperatorsOverloaded Operators

Page 11: Friend Functions 04/12/10. Today  Use reference parameters when passing an object.  Use friend functions with a class

Operator OverloadOperator Overload

Would like to be able to use == to compare Would like to be able to use == to compare points.points.

Instead of a friend functionInstead of a friend function example sec6.4/point/point2.cppexample sec6.4/point/point2.cpp

Page 12: Friend Functions 04/12/10. Today  Use reference parameters when passing an object.  Use friend functions with a class

Overloaded OperatorsOverloaded Operators

SyntaxSyntaxreturntype returntype operatoroperator op op ((parametersparameters););

Page 13: Friend Functions 04/12/10. Today  Use reference parameters when passing an object.  Use friend functions with a class

Overloaded OperatorsOverloaded Operators//friend function//friend functionfriend bool equal(Point C, Point D);friend bool equal(Point C, Point D);

//operator overload//operator overloadfriend bool operator==(Point C, Point D);friend bool operator==(Point C, Point D);

Page 14: Friend Functions 04/12/10. Today  Use reference parameters when passing an object.  Use friend functions with a class

Overload of *Overload of *

Multiplication of fractionsMultiplication of fractions sec6.4/fracOps.cppsec6.4/fracOps.cpp

Finish the friend functionFinish the friend function Change the friend function to operator Change the friend function to operator

overloadoverload

Page 15: Friend Functions 04/12/10. Today  Use reference parameters when passing an object.  Use friend functions with a class

On Your OwnOn Your Own

Write an overload of == to compare the Write an overload of == to compare the volume of two Cylinders like those onvolume of two Cylinders like those onp. 227 #3p. 227 #3

Page 16: Friend Functions 04/12/10. Today  Use reference parameters when passing an object.  Use friend functions with a class

Inline FunctionInline Function

Can give the function definition within the Can give the function definition within the class definition.class definition.

Point class on next slide has in-line Point class on next slide has in-line functions. One of them is the default functions. One of them is the default constructor.constructor.

Page 17: Friend Functions 04/12/10. Today  Use reference parameters when passing an object.  Use friend functions with a class