Upload
beverly-harrington
View
212
Download
0
Embed Size (px)
Citation preview
Friend FunctionsFriend Functions
04/12/1004/12/10
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.
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
Friend FunctionsFriend Functions
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
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
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
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
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.
Overloaded OperatorsOverloaded Operators
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
Overloaded OperatorsOverloaded Operators
SyntaxSyntaxreturntype returntype operatoroperator op op ((parametersparameters););
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);
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
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
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.