SlideShare a Scribd company logo
Friend Functions
Object Oriented Programming Copyright © 2012 IM
Group. All rights
reserved
Trainig
• Make class called DayOfYear has variables:
▫ Month
▫ Day
• And functions:
▫ Default Constructor
▫ Parameterized Constructor
▫ inputDate
Copyright © 2012 IM
Group. All rights
reserved
Friend Function
• Class operations are typically implemented
as member functions
• Some operations are better implemented as
ordinary (nonmember) functions
Copyright © 2012 IM
Group. All rights
reserved
Program Example:
An Equality Function
• The DayOfYear class can be enhanced to include
an equality function
▫ An equality function tests two objects of
type DayOfYear to see if their values represent
the same date
▫ Two dates are equal if they represent the same
day and month
Copyright © 2012 IM
Group. All rights
reserved
Declaration of
The equality Function
• We want the equality function to return a value
of type bool that is true if the dates are the same
• The equality function requires a parameter for
each of the two dates to compare
• The declaration is
bool equal(DayOfYear date1, DayOfYear date2);
▫ Notice that equal is not a member of the class
DayOfYear
Copyright © 2012 IM
Group. All rights
reserved
Defining Function equal
•The function equal, is not a member function
▫It must use public accessor functions to obtain the
day and month from a DayOfYear object
•equal can be defined in this way:
bool equal(DayOfYear date1, DayOfYear date2)
{
return ( date1.get_month( ) == date2.get_month( )
&&
date1.get_day( ) == date2.get_day( ) );
}
Copyright © 2012 IM
Group. All rights
reserved
• The equal function can be used to compare dates
in this manner
if ( equal( today, my_birthday) )
cout << "It's My birthday!";
Using The Function equal
Copyright © 2012 IM
Group. All rights
reserved
Is equal Efficient?
• Function equal could be made more efficient
▫ Equal uses member function calls to obtain the
private data values
▫ Direct access of the member variables would be
more efficient (faster)
Copyright © 2012 IM
Group. All rights
reserved
A More Efficient equal
• As defined here, equal is more efficient,
but not legal
bool equal(DayOfYear date1, DayOfYear date2)
{
return (date1.month = = date2.month
&&
date1.day = = date2.day );
}
▫ The code is simpler and more efficient
▫ Direct access of private member variables is not legal!
Copyright © 2012 IM
Group. All rights
reserved
Friend Functions
• Friend functions are not members of a class, but
can access private member variables of the class
▫ A friend function is declared using the keyword
friend in the class definition
 A friend function is not a member function
▫ As a friend function, the more efficient version of
equal is legal
Copyright © 2012 IM
Group. All rights
reserved
Declaring A Friend
• The function equal is declared a friend in the
abbreviated class definition here
class DayOfYear
{
public:
friend bool equal(DayOfYear date1,
DayOfYear date2);
// The rest of the public members
private:
// the private members
};
Copyright © 2012 IM
Group. All rights
reserved
• A friend function is declared as a friend in the
class definition
• A friend function is defined as a nonmember
function without using the "::" operator
• A friend function is called without using the
'.' operator
Using A Friend Function
Copyright © 2012 IM
Group. All rights
reserved
Are Friends Needed?
• Friend functions can be written as non-friend
functions using the normal accessor and mutator
functions that should be part of the class
• The code of a friend function is simpler and it is
more efficient
Copyright © 2012 IM
Group. All rights
reserved
Choosing Friends
• How do you know when a function should be
a friend or a member function?
▫ In general, use a member function if the task
performed by the function involves only one object
▫ In general, use a nonmember function if the task
performed by the function involves more than
one object
 Choosing to make the nonmember function a friend is
a decision of efficiency and personal taste
Copyright © 2012 IM
Group. All rights
reserved
Parameter Passing Efficiency
• A call-by-value parameter less efficient than a
call-by-reference parameter
▫ The parameter is a local variable initialized to the
value of the argument
 This results in two copies of the argument
• A call-by-reference parameter is more efficient
▫ The parameter is a placeholder replaced by the
argument
 There is only one copy of the argument
Copyright © 2012 IM
Group. All rights
reserved
Class Parameters
• It can be much more efficient to use
call-by-reference parameters when the
parameter is of a class type
• When using a call-by-reference parameter
▫ If the function does not change the value of the
parameter, mark the parameter so the compiler
knows it should not be changed
Copyright © 2012 IM
Group. All rights
reserved
const Parameter Modifier
• To mark a call-by-reference parameter so it
cannot be changed:
▫ Use the modifier const before the parameter type
▫ The parameter becomes a constant parameter
▫ const used in the function declaration and
definition
Copyright © 2012 IM
Group. All rights
reserved
const Parameter Example
• Example (from DayOfYear class):
▫ A function declaration with constant parameters
 friend bool equal(const DayOfYear& date1,
const DayOfYear & date2);
▫ A function definition with constant parameters
 friend bool equal(const DayOfYear& date1,
const DayOfYear & date2)
{
…
}
Copyright © 2012 IM
Group. All rights
reserved
const Considerations
• When a function has a constant parameter,
the compiler will make certain the parameter
cannot be changed by the function
▫ What if the parameter calls a member function?
friend bool equal(const DayOfYear& date1,
const DayOfYear & date2)
{ …
date1.input();
}
▫ The call to input will change the value of date1!
Copyright © 2012 IM
Group. All rights
reserved
const
And Accessor Functions
• Will the compiler accept an accessor function
call from the constant parameter?
friend bool equal(const DayOfYear& date1,
const DayOfYear & date2)
{ …
date1.output();
}
▫ The compiler will not accept this code
 There is no guarantee that output will not change the
value of the parameter
Copyright © 2012 IM
Group. All rights
reserved
const Modifies Functions
• If a constant parameter makes a member function
call…
▫ The member function called must be marked so
the compiler knows it will not change the parameter
▫ const is used to mark functions that will not change
the value of an object
▫ const is used in the function declaration and the
function definition
Copyright © 2012 IM
Group. All rights
reserved
Function Declarations
With const
• To declare a function that will not change the
value of any member variables:
▫ Use const after the parameter list and
just before the semicolon
class DayOfYear
{
public:
…
void output () const ;
…
Copyright © 2012 IM
Group. All rights
reserved
Function Definitions
With const
• To define a function that will not change the
value of any member variables:
▫ Use const in the same location as the function
declaration
void DayOfYear::output() const
{
// output statements
}
Copyright © 2012 IM
Group. All rights
reserved
const Problem Solved
• Now that output is declared and defined using
the const modifier, the compiler will accept
this code
• DayOfYear equal(const DayOfYear & date1,
const DayOfYear & date2)
{ …
amount1.output();
}
Copyright © 2012 IM
Group. All rights
reserved
• Using const to modify parameters of class types
improves program efficiency
• Member functions called by constant parameters
must also use const to let the compiler know
they do not change the value of the parameter
const Wrapup
Copyright © 2012 IM
Group. All rights
reserved
Use const Consistently
• Once a parameter is modified by using const to
make it a constant parameter
▫ Any member functions that are called by the
parameter must also be modified using const to
tell the compiler they will not change the parameter
▫ It is a good idea to modify, with const, every
member function that does not change a member
variable
Copyright © 2012 IM
Group. All rights
reserved
Any Questions
Session 2 Copyright © 2012 IM
Group. All rights
reserved

More Related Content

PPTX
OOP - Introduction
PPTX
Data weave 2.0 language fundamentals
ODP
Introduction to ReactJS
PPTX
OOP - Introduction to Inheritance
PPTX
Joget Workflow v4 Training - Module 9 - Hash Variable
PDF
React Interview Questions and Answers | React Tutorial | React Redux Online T...
PPT
chap5 functions.ppt
PDF
Joget Workflow Training – Basic & Advance for v3.1 – Module 10 – Hash Variables
OOP - Introduction
Data weave 2.0 language fundamentals
Introduction to ReactJS
OOP - Introduction to Inheritance
Joget Workflow v4 Training - Module 9 - Hash Variable
React Interview Questions and Answers | React Tutorial | React Redux Online T...
chap5 functions.ppt
Joget Workflow Training – Basic & Advance for v3.1 – Module 10 – Hash Variables

Similar to OOP - Friend Functions (20)

PPTX
Joget Workflow v6 Training Slides - 9 - Hash Variable
PPTX
Joget Workflow v5 Training Slides - Module 9 - Hash variable
PPTX
Intro To C++ - Class #19: Functions
PDF
Optimizing your use of react life cycles by shedrack akintayo
PPTX
Joget Workflow v6 Training Slides - 19 - Doing More with your Process Design
PPTX
Joget Workflow v5 Training Slides - Module 19 - Doing More With Your Process ...
PPT
EJB 3.2/JPA 2.1 Best Practices with Real-Life Examples - CON7535
PDF
Lightning Web Components- Ep 1 - Decorators, Life Cycle Hooks and Compositions
PPTX
Automation in Jira for beginners
PDF
Module Architecture of React-Redux Applications
PPTX
Joget Workflow v6 Training Slides - 17 - Building Plugins
PPTX
Why I am hooked on the future of React
PDF
walkmod - JUG talk
PPTX
React js
PPTX
CSCI 238 Chapter 07 - Classes
PPTX
Application Architecture
PPTX
Joget Workflow v6 Training Slides - 8 - Designing your First Userview
PDF
Understanding React hooks | Walkingtree Technologies
PDF
C- language Lecture 4
PDF
5 hs mpostcustomizationrenefonseca
Joget Workflow v6 Training Slides - 9 - Hash Variable
Joget Workflow v5 Training Slides - Module 9 - Hash variable
Intro To C++ - Class #19: Functions
Optimizing your use of react life cycles by shedrack akintayo
Joget Workflow v6 Training Slides - 19 - Doing More with your Process Design
Joget Workflow v5 Training Slides - Module 19 - Doing More With Your Process ...
EJB 3.2/JPA 2.1 Best Practices with Real-Life Examples - CON7535
Lightning Web Components- Ep 1 - Decorators, Life Cycle Hooks and Compositions
Automation in Jira for beginners
Module Architecture of React-Redux Applications
Joget Workflow v6 Training Slides - 17 - Building Plugins
Why I am hooked on the future of React
walkmod - JUG talk
React js
CSCI 238 Chapter 07 - Classes
Application Architecture
Joget Workflow v6 Training Slides - 8 - Designing your First Userview
Understanding React hooks | Walkingtree Technologies
C- language Lecture 4
5 hs mpostcustomizationrenefonseca
Ad

More from Mohammad Shaker (9)

PPTX
Android Development - Session 5
PPTX
Android Development - Session 4
PPTX
Android Development - Session 2
PPTX
Android Development - Session 1
PPTX
Introduction to Qt
PPTX
OOP - STL
PPTX
OOP - Templates
PPTX
NoSQL - A Closer Look to Couchbase
PPTX
Introduction to Couchbase
Android Development - Session 5
Android Development - Session 4
Android Development - Session 2
Android Development - Session 1
Introduction to Qt
OOP - STL
OOP - Templates
NoSQL - A Closer Look to Couchbase
Introduction to Couchbase
Ad

Recently uploaded (20)

PPTX
L1 - Introduction to python Backend.pptx
PPTX
Reimagine Home Health with the Power of Agentic AI​
PDF
System and Network Administration Chapter 2
PPTX
history of c programming in notes for students .pptx
PDF
Digital Systems & Binary Numbers (comprehensive )
PDF
Why TechBuilder is the Future of Pickup and Delivery App Development (1).pdf
PDF
Nekopoi APK 2025 free lastest update
PDF
Raksha Bandhan Grocery Pricing Trends in India 2025.pdf
PDF
Odoo Companies in India – Driving Business Transformation.pdf
PDF
Adobe Premiere Pro 2025 (v24.5.0.057) Crack free
PDF
Navsoft: AI-Powered Business Solutions & Custom Software Development
PPTX
Agentic AI Use Case- Contract Lifecycle Management (CLM).pptx
PDF
How to Choose the Right IT Partner for Your Business in Malaysia
PDF
PTS Company Brochure 2025 (1).pdf.......
PDF
Which alternative to Crystal Reports is best for small or large businesses.pdf
PDF
Designing Intelligence for the Shop Floor.pdf
PDF
T3DD25 TYPO3 Content Blocks - Deep Dive by André Kraus
PDF
Internet Downloader Manager (IDM) Crack 6.42 Build 41
PDF
medical staffing services at VALiNTRY
PPTX
Transform Your Business with a Software ERP System
L1 - Introduction to python Backend.pptx
Reimagine Home Health with the Power of Agentic AI​
System and Network Administration Chapter 2
history of c programming in notes for students .pptx
Digital Systems & Binary Numbers (comprehensive )
Why TechBuilder is the Future of Pickup and Delivery App Development (1).pdf
Nekopoi APK 2025 free lastest update
Raksha Bandhan Grocery Pricing Trends in India 2025.pdf
Odoo Companies in India – Driving Business Transformation.pdf
Adobe Premiere Pro 2025 (v24.5.0.057) Crack free
Navsoft: AI-Powered Business Solutions & Custom Software Development
Agentic AI Use Case- Contract Lifecycle Management (CLM).pptx
How to Choose the Right IT Partner for Your Business in Malaysia
PTS Company Brochure 2025 (1).pdf.......
Which alternative to Crystal Reports is best for small or large businesses.pdf
Designing Intelligence for the Shop Floor.pdf
T3DD25 TYPO3 Content Blocks - Deep Dive by André Kraus
Internet Downloader Manager (IDM) Crack 6.42 Build 41
medical staffing services at VALiNTRY
Transform Your Business with a Software ERP System

OOP - Friend Functions

  • 1. Friend Functions Object Oriented Programming Copyright © 2012 IM Group. All rights reserved
  • 2. Trainig • Make class called DayOfYear has variables: ▫ Month ▫ Day • And functions: ▫ Default Constructor ▫ Parameterized Constructor ▫ inputDate Copyright © 2012 IM Group. All rights reserved
  • 3. Friend Function • Class operations are typically implemented as member functions • Some operations are better implemented as ordinary (nonmember) functions Copyright © 2012 IM Group. All rights reserved
  • 4. Program Example: An Equality Function • The DayOfYear class can be enhanced to include an equality function ▫ An equality function tests two objects of type DayOfYear to see if their values represent the same date ▫ Two dates are equal if they represent the same day and month Copyright © 2012 IM Group. All rights reserved
  • 5. Declaration of The equality Function • We want the equality function to return a value of type bool that is true if the dates are the same • The equality function requires a parameter for each of the two dates to compare • The declaration is bool equal(DayOfYear date1, DayOfYear date2); ▫ Notice that equal is not a member of the class DayOfYear Copyright © 2012 IM Group. All rights reserved
  • 6. Defining Function equal •The function equal, is not a member function ▫It must use public accessor functions to obtain the day and month from a DayOfYear object •equal can be defined in this way: bool equal(DayOfYear date1, DayOfYear date2) { return ( date1.get_month( ) == date2.get_month( ) && date1.get_day( ) == date2.get_day( ) ); } Copyright © 2012 IM Group. All rights reserved
  • 7. • The equal function can be used to compare dates in this manner if ( equal( today, my_birthday) ) cout << "It's My birthday!"; Using The Function equal Copyright © 2012 IM Group. All rights reserved
  • 8. Is equal Efficient? • Function equal could be made more efficient ▫ Equal uses member function calls to obtain the private data values ▫ Direct access of the member variables would be more efficient (faster) Copyright © 2012 IM Group. All rights reserved
  • 9. A More Efficient equal • As defined here, equal is more efficient, but not legal bool equal(DayOfYear date1, DayOfYear date2) { return (date1.month = = date2.month && date1.day = = date2.day ); } ▫ The code is simpler and more efficient ▫ Direct access of private member variables is not legal! Copyright © 2012 IM Group. All rights reserved
  • 10. Friend Functions • Friend functions are not members of a class, but can access private member variables of the class ▫ A friend function is declared using the keyword friend in the class definition  A friend function is not a member function ▫ As a friend function, the more efficient version of equal is legal Copyright © 2012 IM Group. All rights reserved
  • 11. Declaring A Friend • The function equal is declared a friend in the abbreviated class definition here class DayOfYear { public: friend bool equal(DayOfYear date1, DayOfYear date2); // The rest of the public members private: // the private members }; Copyright © 2012 IM Group. All rights reserved
  • 12. • A friend function is declared as a friend in the class definition • A friend function is defined as a nonmember function without using the "::" operator • A friend function is called without using the '.' operator Using A Friend Function Copyright © 2012 IM Group. All rights reserved
  • 13. Are Friends Needed? • Friend functions can be written as non-friend functions using the normal accessor and mutator functions that should be part of the class • The code of a friend function is simpler and it is more efficient Copyright © 2012 IM Group. All rights reserved
  • 14. Choosing Friends • How do you know when a function should be a friend or a member function? ▫ In general, use a member function if the task performed by the function involves only one object ▫ In general, use a nonmember function if the task performed by the function involves more than one object  Choosing to make the nonmember function a friend is a decision of efficiency and personal taste Copyright © 2012 IM Group. All rights reserved
  • 15. Parameter Passing Efficiency • A call-by-value parameter less efficient than a call-by-reference parameter ▫ The parameter is a local variable initialized to the value of the argument  This results in two copies of the argument • A call-by-reference parameter is more efficient ▫ The parameter is a placeholder replaced by the argument  There is only one copy of the argument Copyright © 2012 IM Group. All rights reserved
  • 16. Class Parameters • It can be much more efficient to use call-by-reference parameters when the parameter is of a class type • When using a call-by-reference parameter ▫ If the function does not change the value of the parameter, mark the parameter so the compiler knows it should not be changed Copyright © 2012 IM Group. All rights reserved
  • 17. const Parameter Modifier • To mark a call-by-reference parameter so it cannot be changed: ▫ Use the modifier const before the parameter type ▫ The parameter becomes a constant parameter ▫ const used in the function declaration and definition Copyright © 2012 IM Group. All rights reserved
  • 18. const Parameter Example • Example (from DayOfYear class): ▫ A function declaration with constant parameters  friend bool equal(const DayOfYear& date1, const DayOfYear & date2); ▫ A function definition with constant parameters  friend bool equal(const DayOfYear& date1, const DayOfYear & date2) { … } Copyright © 2012 IM Group. All rights reserved
  • 19. const Considerations • When a function has a constant parameter, the compiler will make certain the parameter cannot be changed by the function ▫ What if the parameter calls a member function? friend bool equal(const DayOfYear& date1, const DayOfYear & date2) { … date1.input(); } ▫ The call to input will change the value of date1! Copyright © 2012 IM Group. All rights reserved
  • 20. const And Accessor Functions • Will the compiler accept an accessor function call from the constant parameter? friend bool equal(const DayOfYear& date1, const DayOfYear & date2) { … date1.output(); } ▫ The compiler will not accept this code  There is no guarantee that output will not change the value of the parameter Copyright © 2012 IM Group. All rights reserved
  • 21. const Modifies Functions • If a constant parameter makes a member function call… ▫ The member function called must be marked so the compiler knows it will not change the parameter ▫ const is used to mark functions that will not change the value of an object ▫ const is used in the function declaration and the function definition Copyright © 2012 IM Group. All rights reserved
  • 22. Function Declarations With const • To declare a function that will not change the value of any member variables: ▫ Use const after the parameter list and just before the semicolon class DayOfYear { public: … void output () const ; … Copyright © 2012 IM Group. All rights reserved
  • 23. Function Definitions With const • To define a function that will not change the value of any member variables: ▫ Use const in the same location as the function declaration void DayOfYear::output() const { // output statements } Copyright © 2012 IM Group. All rights reserved
  • 24. const Problem Solved • Now that output is declared and defined using the const modifier, the compiler will accept this code • DayOfYear equal(const DayOfYear & date1, const DayOfYear & date2) { … amount1.output(); } Copyright © 2012 IM Group. All rights reserved
  • 25. • Using const to modify parameters of class types improves program efficiency • Member functions called by constant parameters must also use const to let the compiler know they do not change the value of the parameter const Wrapup Copyright © 2012 IM Group. All rights reserved
  • 26. Use const Consistently • Once a parameter is modified by using const to make it a constant parameter ▫ Any member functions that are called by the parameter must also be modified using const to tell the compiler they will not change the parameter ▫ It is a good idea to modify, with const, every member function that does not change a member variable Copyright © 2012 IM Group. All rights reserved
  • 27. Any Questions Session 2 Copyright © 2012 IM Group. All rights reserved