SlideShare uma empresa Scribd logo
1 de 27
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

Mais conteúdo relacionado

Semelhante a OOP - Friend Functions

CSCI 238 Chapter 07 - Classes
CSCI 238 Chapter 07 - ClassesCSCI 238 Chapter 07 - Classes
CSCI 238 Chapter 07 - Classes
DanWooster1
 

Semelhante a OOP - Friend Functions (20)

Joget Workflow v6 Training Slides - 9 - Hash Variable
Joget Workflow v6 Training Slides - 9 - Hash VariableJoget Workflow v6 Training Slides - 9 - Hash Variable
Joget Workflow v6 Training Slides - 9 - Hash Variable
 
Joget Workflow v5 Training Slides - Module 9 - Hash variable
Joget Workflow v5 Training Slides - Module 9 - Hash variableJoget Workflow v5 Training Slides - Module 9 - Hash variable
Joget Workflow v5 Training Slides - Module 9 - Hash variable
 
Intro To C++ - Class #19: Functions
Intro To C++ - Class #19: FunctionsIntro To C++ - Class #19: Functions
Intro To C++ - Class #19: Functions
 
Optimizing your use of react life cycles by shedrack akintayo
Optimizing your use of react life cycles by shedrack akintayoOptimizing your use of react life cycles by shedrack akintayo
Optimizing your use of react life cycles by shedrack akintayo
 
Joget Workflow v6 Training Slides - 19 - Doing More with your Process Design
Joget Workflow v6 Training Slides - 19 - Doing More with your Process DesignJoget Workflow v6 Training Slides - 19 - Doing More with your Process Design
Joget Workflow v6 Training Slides - 19 - Doing More with your Process Design
 
Joget Workflow v5 Training Slides - Module 19 - Doing More With Your Process ...
Joget Workflow v5 Training Slides - Module 19 - Doing More With Your Process ...Joget Workflow v5 Training Slides - Module 19 - Doing More With Your Process ...
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
EJB 3.2/JPA 2.1 Best Practices with Real-Life Examples - CON7535EJB 3.2/JPA 2.1 Best Practices with Real-Life Examples - CON7535
EJB 3.2/JPA 2.1 Best Practices with Real-Life Examples - CON7535
 
Lightning Web Components- Ep 1 - Decorators, Life Cycle Hooks and Compositions
Lightning Web Components- Ep 1 - Decorators, Life Cycle Hooks and CompositionsLightning Web Components- Ep 1 - Decorators, Life Cycle Hooks and Compositions
Lightning Web Components- Ep 1 - Decorators, Life Cycle Hooks and Compositions
 
Automation in Jira for beginners
Automation in Jira for beginnersAutomation in Jira for beginners
Automation in Jira for beginners
 
Module Architecture of React-Redux Applications
Module Architecture of React-Redux ApplicationsModule Architecture of React-Redux Applications
Module Architecture of React-Redux Applications
 
Joget Workflow v6 Training Slides - 17 - Building Plugins
Joget Workflow v6 Training Slides - 17 - Building PluginsJoget Workflow v6 Training Slides - 17 - Building Plugins
Joget Workflow v6 Training Slides - 17 - Building Plugins
 
Why I am hooked on the future of React
Why I am hooked on the future of ReactWhy I am hooked on the future of React
Why I am hooked on the future of React
 
walkmod - JUG talk
walkmod - JUG talkwalkmod - JUG talk
walkmod - JUG talk
 
React js
React jsReact js
React js
 
CSCI 238 Chapter 07 - Classes
CSCI 238 Chapter 07 - ClassesCSCI 238 Chapter 07 - Classes
CSCI 238 Chapter 07 - Classes
 
Application Architecture
Application ArchitectureApplication Architecture
Application Architecture
 
Joget Workflow v6 Training Slides - 8 - Designing your First Userview
Joget Workflow v6 Training Slides - 8 - Designing your First UserviewJoget Workflow v6 Training Slides - 8 - Designing your First Userview
Joget Workflow v6 Training Slides - 8 - Designing your First Userview
 
Understanding React hooks | Walkingtree Technologies
Understanding React hooks | Walkingtree TechnologiesUnderstanding React hooks | Walkingtree Technologies
Understanding React hooks | Walkingtree Technologies
 
C- language Lecture 4
C- language Lecture 4C- language Lecture 4
C- language Lecture 4
 
5 hs mpostcustomizationrenefonseca
5 hs mpostcustomizationrenefonseca5 hs mpostcustomizationrenefonseca
5 hs mpostcustomizationrenefonseca
 

Mais de Mohammad Shaker (9)

Android Development - Session 5
Android Development - Session 5Android Development - Session 5
Android Development - Session 5
 
Android Development - Session 4
Android Development - Session 4Android Development - Session 4
Android Development - Session 4
 
Android Development - Session 2
Android Development - Session 2Android Development - Session 2
Android Development - Session 2
 
Android Development - Session 1
Android Development - Session 1Android Development - Session 1
Android Development - Session 1
 
Introduction to Qt
Introduction to QtIntroduction to Qt
Introduction to Qt
 
OOP - STL
OOP - STLOOP - STL
OOP - STL
 
OOP - Templates
OOP - TemplatesOOP - Templates
OOP - Templates
 
NoSQL - A Closer Look to Couchbase
NoSQL - A Closer Look to CouchbaseNoSQL - A Closer Look to Couchbase
NoSQL - A Closer Look to Couchbase
 
Introduction to Couchbase
Introduction to CouchbaseIntroduction to Couchbase
Introduction to Couchbase
 

Último

%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
masabamasaba
 
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
masabamasaba
 
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
masabamasaba
 
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
masabamasaba
 
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
Medical / Health Care (+971588192166) Mifepristone and Misoprostol tablets 200mg
 
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
masabamasaba
 

Último (20)

%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
 
%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrand%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrand
 
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
 
%in Harare+277-882-255-28 abortion pills for sale in Harare
%in Harare+277-882-255-28 abortion pills for sale in Harare%in Harare+277-882-255-28 abortion pills for sale in Harare
%in Harare+277-882-255-28 abortion pills for sale in Harare
 
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdf
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdfPayment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdf
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdf
 
Introducing Microsoft’s new Enterprise Work Management (EWM) Solution
Introducing Microsoft’s new Enterprise Work Management (EWM) SolutionIntroducing Microsoft’s new Enterprise Work Management (EWM) Solution
Introducing Microsoft’s new Enterprise Work Management (EWM) Solution
 
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
 
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
 
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
 
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
 
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
 
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
 
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
 
tonesoftg
tonesoftgtonesoftg
tonesoftg
 
8257 interfacing 2 in microprocessor for btech students
8257 interfacing 2 in microprocessor for btech students8257 interfacing 2 in microprocessor for btech students
8257 interfacing 2 in microprocessor for btech students
 
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
 
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
 
Direct Style Effect Systems - The Print[A] Example - A Comprehension Aid
Direct Style Effect Systems -The Print[A] Example- A Comprehension AidDirect Style Effect Systems -The Print[A] Example- A Comprehension Aid
Direct Style Effect Systems - The Print[A] Example - A Comprehension Aid
 
Microsoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdfMicrosoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdf
 
%in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park %in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park
 

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