際際滷

際際滷Share a Scribd company logo
FRIEND
FUNCTION
NAME: HUMMU SALMA H
ROLL NO:2010510
SUBJECT:OBJECT ORIENTED PROGRAMMING
CONCEPTS USING C++
Introduction
 If a function is defined as a friend function in C++, then
the protected and private data of a class can be accessed
using the function.
 By using the keyword friend compiler knows the given
function is a friend function.
 For accessing the data, the declaration of a friend
function should be done inside the body of a class
starting with the keyword friend.
Declaration of friend function
SYNTAX :
class className
{
 .. .
friend returnType functionName
(arguments)
 .. .
}
Characteristics of friend function
 The function definition does not use either the keyword friend or scope
resolution operator.
 A friend function is not in the scope of the class, in which it has been declared as
friend.
 It cannot be called using the object of that class.
 It can be invoked like a normal function without any object.
 Unlike member functions, it cannot use the member names directly.
 It can be declared in public or private part without affecting its meaning.
Whats the use of friend function?
Data hiding is a fundamental concept of object-oriented
programming. It restricts the access of private members
from outside of the class.
Similarly, protected members can only be accessed by
derived classes and are inaccessible from outside. For
example,
Example & Explanation
class MyClass
{
private:
int member 1 ;
};
int main()
{
MyClass obj;
// Error! Cannot access private
members.
obj.member 1=5;
}
Here we cannot access the
member 1. Since it is a
private member of the
class.Hence, to avoid this
we use FRIEND
FUNCTONS.It allow us to
access member functions
from outside the class.
EXAMPLE FOR
FRIEND
FUNCTION
CODING:
#include <iostream>
Using namespace std;
class Temperature
{
int celsius;
public:
Temperature( )
{
celsius=0
}
friend int temp(Temperature);
};
int temp(Temperature t)
{
t.celsius=40;
return t.celsius;
}
int main( )
{
Temperature tm;
cout <<Temperature in celsius:<<temp(tm)
return 0;
}
CODING ,OUTPUT AND
EXPLANATION
Output & Explanation
*OUTPUT:
Temperature in celsius : 40
*EXPLANATION:
Here we declared a function 'temp' as the friend function of the class
'Temperature'. In the friend function, we directly accessed the private member celsius
of the class 'Temperature'.When the first statement of the main function created an
object 'tm' of the class 'Temperature' thus calling its constructor and assigning a value
0 to its data member celsius.The second statement of the main function called the
function 'temp' which assigned a value 40 to celsius
Friend function in c++

More Related Content

What's hot (20)

Constructors and Destructor in C++
Constructors and Destructor in C++Constructors and Destructor in C++
Constructors and Destructor in C++
International Institute of Information Technology (I族IT)
friend function(c++)
friend function(c++)friend function(c++)
friend function(c++)
Ritika Sharma
Operator Overloading
Operator OverloadingOperator Overloading
Operator Overloading
Nilesh Dalvi
Inheritance in c++
Inheritance in c++Inheritance in c++
Inheritance in c++
Vineeta Garg
Member Function in C++
Member Function in C++ Member Function in C++
Member Function in C++
NikitaKaur10
Functions in c++
Functions in c++Functions in c++
Functions in c++
Rokonuzzaman Rony
Functions in C
Functions in CFunctions in C
Functions in C
Kamal Acharya
Data types in java
Data types in javaData types in java
Data types in java
HarshitaAshwani
Inline function
Inline functionInline function
Inline function
Tech_MX
RECURSION IN C
RECURSION IN C RECURSION IN C
RECURSION IN C
v_jk
Programming in C Presentation upto FILE
Programming in C Presentation upto FILEProgramming in C Presentation upto FILE
Programming in C Presentation upto FILE
Dipta Saha
C++ presentation
C++ presentationC++ presentation
C++ presentation
SudhanshuVijay3
C++ Overview PPT
C++ Overview PPTC++ Overview PPT
C++ Overview PPT
Thooyavan Venkatachalam
Function in c program
Function in c programFunction in c program
Function in c program
umesh patil
FUNCTIONS IN c++ PPT
FUNCTIONS IN c++ PPTFUNCTIONS IN c++ PPT
FUNCTIONS IN c++ PPT
03062679929
Getters_And_Setters.pptx
Getters_And_Setters.pptxGetters_And_Setters.pptx
Getters_And_Setters.pptx
Kavindu Sachinthe
Static Data Members and Member Functions
Static Data Members and Member FunctionsStatic Data Members and Member Functions
Static Data Members and Member Functions
MOHIT AGARWAL
Friend Function
Friend FunctionFriend Function
Friend Function
Mehak Tawakley
Operator Overloading in C++
Operator Overloading in C++Operator Overloading in C++
Operator Overloading in C++
Mohammed Sikander
INLINE FUNCTION IN C++
INLINE FUNCTION IN C++INLINE FUNCTION IN C++
INLINE FUNCTION IN C++
Vraj Patel
friend function(c++)
friend function(c++)friend function(c++)
friend function(c++)
Ritika Sharma
Operator Overloading
Operator OverloadingOperator Overloading
Operator Overloading
Nilesh Dalvi
Inheritance in c++
Inheritance in c++Inheritance in c++
Inheritance in c++
Vineeta Garg
Member Function in C++
Member Function in C++ Member Function in C++
Member Function in C++
NikitaKaur10
Inline function
Inline functionInline function
Inline function
Tech_MX
RECURSION IN C
RECURSION IN C RECURSION IN C
RECURSION IN C
v_jk
Programming in C Presentation upto FILE
Programming in C Presentation upto FILEProgramming in C Presentation upto FILE
Programming in C Presentation upto FILE
Dipta Saha
Function in c program
Function in c programFunction in c program
Function in c program
umesh patil
FUNCTIONS IN c++ PPT
FUNCTIONS IN c++ PPTFUNCTIONS IN c++ PPT
FUNCTIONS IN c++ PPT
03062679929
Static Data Members and Member Functions
Static Data Members and Member FunctionsStatic Data Members and Member Functions
Static Data Members and Member Functions
MOHIT AGARWAL
Operator Overloading in C++
Operator Overloading in C++Operator Overloading in C++
Operator Overloading in C++
Mohammed Sikander
INLINE FUNCTION IN C++
INLINE FUNCTION IN C++INLINE FUNCTION IN C++
INLINE FUNCTION IN C++
Vraj Patel

Similar to Friend function in c++ (20)

Object and class presentation
Object and class presentationObject and class presentation
Object and class presentation
nafisa rahman
Friend function
Friend functionFriend function
Friend function
Heet Patel
OOPs & C++ UNIT 3
OOPs & C++ UNIT 3OOPs & C++ UNIT 3
OOPs & C++ UNIT 3
Dr. SURBHI SAROHA
Access specifier
Access specifierAccess specifier
Access specifier
zindadili
Unit_2_oop By Alfiya Sayyed Maam from AIARKP
Unit_2_oop By Alfiya Sayyed Maam from  AIARKPUnit_2_oop By Alfiya Sayyed Maam from  AIARKP
Unit_2_oop By Alfiya Sayyed Maam from AIARKP
mradeen946
Lecture 2 (1)
Lecture 2 (1)Lecture 2 (1)
Lecture 2 (1)
zahid khan
class c++
class c++class c++
class c++
vinay chauhan
Presentation on class and object in Object Oriented programming.
Presentation on class and object in Object Oriented programming.Presentation on class and object in Object Oriented programming.
Presentation on class and object in Object Oriented programming.
Enam Khan
22 scheme OOPs with C++ BCS306B_module1.pdf
22 scheme  OOPs with C++ BCS306B_module1.pdf22 scheme  OOPs with C++ BCS306B_module1.pdf
22 scheme OOPs with C++ BCS306B_module1.pdf
sindhus795217
C++ Notes
C++ NotesC++ Notes
C++ Notes
MOHAMED RIYAZUDEEN
constructor , operator overloading ,friend function
constructor , operator overloading ,friend function constructor , operator overloading ,friend function
constructor , operator overloading ,friend function
Tanzina Suma
Object Oriented Programming using C++ - Part 2
Object Oriented Programming using C++ - Part 2Object Oriented Programming using C++ - Part 2
Object Oriented Programming using C++ - Part 2
University College of Engineering Kakinada, JNTUK - Kakinada, India
CLASSES AND OBJECTS IN C++ +2 COMPUTER SCIENCE
CLASSES AND OBJECTS IN C++ +2 COMPUTER SCIENCECLASSES AND OBJECTS IN C++ +2 COMPUTER SCIENCE
CLASSES AND OBJECTS IN C++ +2 COMPUTER SCIENCE
Venugopalavarma Raja
00ps inheritace using c++
00ps inheritace using c++00ps inheritace using c++
00ps inheritace using c++
sushamaGavarskar1
cse l 5.pptx
cse l 5.pptxcse l 5.pptx
cse l 5.pptx
KeshavKumar395652
Classes, objects and methods
Classes, objects and methodsClasses, objects and methods
Classes, objects and methods
farhan amjad
chapter-7-classes-and-objects.pdf
chapter-7-classes-and-objects.pdfchapter-7-classes-and-objects.pdf
chapter-7-classes-and-objects.pdf
study material
Chapter23 friend-function-friend-class
Chapter23 friend-function-friend-classChapter23 friend-function-friend-class
Chapter23 friend-function-friend-class
Deepak Singh
Access controlaspecifier and visibilty modes
Access controlaspecifier and visibilty modesAccess controlaspecifier and visibilty modes
Access controlaspecifier and visibilty modes
Vinay Kumar
Member Function in C++
Member Function in C++Member Function in C++
Member Function in C++
AbhishekSaini132328
Object and class presentation
Object and class presentationObject and class presentation
Object and class presentation
nafisa rahman
Friend function
Friend functionFriend function
Friend function
Heet Patel
Access specifier
Access specifierAccess specifier
Access specifier
zindadili
Unit_2_oop By Alfiya Sayyed Maam from AIARKP
Unit_2_oop By Alfiya Sayyed Maam from  AIARKPUnit_2_oop By Alfiya Sayyed Maam from  AIARKP
Unit_2_oop By Alfiya Sayyed Maam from AIARKP
mradeen946
Lecture 2 (1)
Lecture 2 (1)Lecture 2 (1)
Lecture 2 (1)
zahid khan
Presentation on class and object in Object Oriented programming.
Presentation on class and object in Object Oriented programming.Presentation on class and object in Object Oriented programming.
Presentation on class and object in Object Oriented programming.
Enam Khan
22 scheme OOPs with C++ BCS306B_module1.pdf
22 scheme  OOPs with C++ BCS306B_module1.pdf22 scheme  OOPs with C++ BCS306B_module1.pdf
22 scheme OOPs with C++ BCS306B_module1.pdf
sindhus795217
constructor , operator overloading ,friend function
constructor , operator overloading ,friend function constructor , operator overloading ,friend function
constructor , operator overloading ,friend function
Tanzina Suma
CLASSES AND OBJECTS IN C++ +2 COMPUTER SCIENCE
CLASSES AND OBJECTS IN C++ +2 COMPUTER SCIENCECLASSES AND OBJECTS IN C++ +2 COMPUTER SCIENCE
CLASSES AND OBJECTS IN C++ +2 COMPUTER SCIENCE
Venugopalavarma Raja
00ps inheritace using c++
00ps inheritace using c++00ps inheritace using c++
00ps inheritace using c++
sushamaGavarskar1
Classes, objects and methods
Classes, objects and methodsClasses, objects and methods
Classes, objects and methods
farhan amjad
chapter-7-classes-and-objects.pdf
chapter-7-classes-and-objects.pdfchapter-7-classes-and-objects.pdf
chapter-7-classes-and-objects.pdf
study material
Chapter23 friend-function-friend-class
Chapter23 friend-function-friend-classChapter23 friend-function-friend-class
Chapter23 friend-function-friend-class
Deepak Singh
Access controlaspecifier and visibilty modes
Access controlaspecifier and visibilty modesAccess controlaspecifier and visibilty modes
Access controlaspecifier and visibilty modes
Vinay Kumar

Recently uploaded (20)

Water Industry Process Automation & Control Monthly - March 2025.pdf
Water Industry Process Automation & Control Monthly - March 2025.pdfWater Industry Process Automation & Control Monthly - March 2025.pdf
Water Industry Process Automation & Control Monthly - March 2025.pdf
Water Industry Process Automation & Control
Cloud Computing concepts and technologies
Cloud Computing concepts and technologiesCloud Computing concepts and technologies
Cloud Computing concepts and technologies
ssuser4c9444
CONTRACTOR ALL RISK INSURANCESAR (1).ppt
CONTRACTOR ALL RISK INSURANCESAR (1).pptCONTRACTOR ALL RISK INSURANCESAR (1).ppt
CONTRACTOR ALL RISK INSURANCESAR (1).ppt
suaktonny
Industrial Valves, Instruments Products Profile
Industrial Valves, Instruments Products ProfileIndustrial Valves, Instruments Products Profile
Industrial Valves, Instruments Products Profile
zebcoeng
Indian Soil Classification System in Geotechnical Engineering
Indian Soil Classification System in Geotechnical EngineeringIndian Soil Classification System in Geotechnical Engineering
Indian Soil Classification System in Geotechnical Engineering
Rajani Vyawahare
Mathematics_behind_machine_learning_INT255.pptx
Mathematics_behind_machine_learning_INT255.pptxMathematics_behind_machine_learning_INT255.pptx
Mathematics_behind_machine_learning_INT255.pptx
ppkmurthy2006
Multi objective genetic approach with Ranking
Multi objective genetic approach with RankingMulti objective genetic approach with Ranking
Multi objective genetic approach with Ranking
namisha18
Introduction to Safety, Health & Environment
Introduction to Safety, Health  & EnvironmentIntroduction to Safety, Health  & Environment
Introduction to Safety, Health & Environment
ssuserc606c7
Frankfurt University of Applied Science urkunde
Frankfurt University of Applied Science urkundeFrankfurt University of Applied Science urkunde
Frankfurt University of Applied Science urkunde
Lisa Emerson
CS3451-OPERATING-SYSTEM NOTES ALL123.pdf
CS3451-OPERATING-SYSTEM NOTES ALL123.pdfCS3451-OPERATING-SYSTEM NOTES ALL123.pdf
CS3451-OPERATING-SYSTEM NOTES ALL123.pdf
PonniS7
Lecture -3 Cold water supply system.pptx
Lecture -3 Cold water supply system.pptxLecture -3 Cold water supply system.pptx
Lecture -3 Cold water supply system.pptx
rabiaatif2
Turbocor Product and Technology Review.pdf
Turbocor Product and Technology Review.pdfTurbocor Product and Technology Review.pdf
Turbocor Product and Technology Review.pdf
Totok Sulistiyanto
Structural QA/QC Inspection in KRP 401600 | Copper Processing Plant-3 (MOF-3)...
Structural QA/QC Inspection in KRP 401600 | Copper Processing Plant-3 (MOF-3)...Structural QA/QC Inspection in KRP 401600 | Copper Processing Plant-3 (MOF-3)...
Structural QA/QC Inspection in KRP 401600 | Copper Processing Plant-3 (MOF-3)...
slayshadow705
Equipment for Gas Metal Arc Welding Process
Equipment for Gas Metal Arc Welding ProcessEquipment for Gas Metal Arc Welding Process
Equipment for Gas Metal Arc Welding Process
AhmadKamil87
US Patented ReGenX Generator, ReGen-X Quatum Motor EV Regenerative Accelerati...
US Patented ReGenX Generator, ReGen-X Quatum Motor EV Regenerative Accelerati...US Patented ReGenX Generator, ReGen-X Quatum Motor EV Regenerative Accelerati...
US Patented ReGenX Generator, ReGen-X Quatum Motor EV Regenerative Accelerati...
Thane Heins NOBEL PRIZE WINNING ENERGY RESEARCHER
GREEN BULIDING PPT FOR THE REFRENACE.PPT
GREEN BULIDING PPT FOR THE REFRENACE.PPTGREEN BULIDING PPT FOR THE REFRENACE.PPT
GREEN BULIDING PPT FOR THE REFRENACE.PPT
kamalkeerthan61
Power Point Presentation for Electrical Engineering 3-phase.ppt
Power Point Presentation for Electrical Engineering 3-phase.pptPower Point Presentation for Electrical Engineering 3-phase.ppt
Power Point Presentation for Electrical Engineering 3-phase.ppt
Aniket_1415
CFOT Fiber Optics FOA CERTIFICATION.pptx
CFOT Fiber Optics FOA CERTIFICATION.pptxCFOT Fiber Optics FOA CERTIFICATION.pptx
CFOT Fiber Optics FOA CERTIFICATION.pptx
MohamedShabana37
health safety and environment presentation
health safety and environment presentationhealth safety and environment presentation
health safety and environment presentation
ssuserc606c7
04 MAINTENANCE OF CONCRETE PAVEMENTS.ppt
04  MAINTENANCE OF CONCRETE PAVEMENTS.ppt04  MAINTENANCE OF CONCRETE PAVEMENTS.ppt
04 MAINTENANCE OF CONCRETE PAVEMENTS.ppt
sreenath seenu
Cloud Computing concepts and technologies
Cloud Computing concepts and technologiesCloud Computing concepts and technologies
Cloud Computing concepts and technologies
ssuser4c9444
CONTRACTOR ALL RISK INSURANCESAR (1).ppt
CONTRACTOR ALL RISK INSURANCESAR (1).pptCONTRACTOR ALL RISK INSURANCESAR (1).ppt
CONTRACTOR ALL RISK INSURANCESAR (1).ppt
suaktonny
Industrial Valves, Instruments Products Profile
Industrial Valves, Instruments Products ProfileIndustrial Valves, Instruments Products Profile
Industrial Valves, Instruments Products Profile
zebcoeng
Indian Soil Classification System in Geotechnical Engineering
Indian Soil Classification System in Geotechnical EngineeringIndian Soil Classification System in Geotechnical Engineering
Indian Soil Classification System in Geotechnical Engineering
Rajani Vyawahare
Mathematics_behind_machine_learning_INT255.pptx
Mathematics_behind_machine_learning_INT255.pptxMathematics_behind_machine_learning_INT255.pptx
Mathematics_behind_machine_learning_INT255.pptx
ppkmurthy2006
Multi objective genetic approach with Ranking
Multi objective genetic approach with RankingMulti objective genetic approach with Ranking
Multi objective genetic approach with Ranking
namisha18
Introduction to Safety, Health & Environment
Introduction to Safety, Health  & EnvironmentIntroduction to Safety, Health  & Environment
Introduction to Safety, Health & Environment
ssuserc606c7
Frankfurt University of Applied Science urkunde
Frankfurt University of Applied Science urkundeFrankfurt University of Applied Science urkunde
Frankfurt University of Applied Science urkunde
Lisa Emerson
CS3451-OPERATING-SYSTEM NOTES ALL123.pdf
CS3451-OPERATING-SYSTEM NOTES ALL123.pdfCS3451-OPERATING-SYSTEM NOTES ALL123.pdf
CS3451-OPERATING-SYSTEM NOTES ALL123.pdf
PonniS7
Lecture -3 Cold water supply system.pptx
Lecture -3 Cold water supply system.pptxLecture -3 Cold water supply system.pptx
Lecture -3 Cold water supply system.pptx
rabiaatif2
Turbocor Product and Technology Review.pdf
Turbocor Product and Technology Review.pdfTurbocor Product and Technology Review.pdf
Turbocor Product and Technology Review.pdf
Totok Sulistiyanto
Structural QA/QC Inspection in KRP 401600 | Copper Processing Plant-3 (MOF-3)...
Structural QA/QC Inspection in KRP 401600 | Copper Processing Plant-3 (MOF-3)...Structural QA/QC Inspection in KRP 401600 | Copper Processing Plant-3 (MOF-3)...
Structural QA/QC Inspection in KRP 401600 | Copper Processing Plant-3 (MOF-3)...
slayshadow705
Equipment for Gas Metal Arc Welding Process
Equipment for Gas Metal Arc Welding ProcessEquipment for Gas Metal Arc Welding Process
Equipment for Gas Metal Arc Welding Process
AhmadKamil87
GREEN BULIDING PPT FOR THE REFRENACE.PPT
GREEN BULIDING PPT FOR THE REFRENACE.PPTGREEN BULIDING PPT FOR THE REFRENACE.PPT
GREEN BULIDING PPT FOR THE REFRENACE.PPT
kamalkeerthan61
Power Point Presentation for Electrical Engineering 3-phase.ppt
Power Point Presentation for Electrical Engineering 3-phase.pptPower Point Presentation for Electrical Engineering 3-phase.ppt
Power Point Presentation for Electrical Engineering 3-phase.ppt
Aniket_1415
CFOT Fiber Optics FOA CERTIFICATION.pptx
CFOT Fiber Optics FOA CERTIFICATION.pptxCFOT Fiber Optics FOA CERTIFICATION.pptx
CFOT Fiber Optics FOA CERTIFICATION.pptx
MohamedShabana37
health safety and environment presentation
health safety and environment presentationhealth safety and environment presentation
health safety and environment presentation
ssuserc606c7
04 MAINTENANCE OF CONCRETE PAVEMENTS.ppt
04  MAINTENANCE OF CONCRETE PAVEMENTS.ppt04  MAINTENANCE OF CONCRETE PAVEMENTS.ppt
04 MAINTENANCE OF CONCRETE PAVEMENTS.ppt
sreenath seenu

Friend function in c++

  • 1. FRIEND FUNCTION NAME: HUMMU SALMA H ROLL NO:2010510 SUBJECT:OBJECT ORIENTED PROGRAMMING CONCEPTS USING C++
  • 2. Introduction If a function is defined as a friend function in C++, then the protected and private data of a class can be accessed using the function. By using the keyword friend compiler knows the given function is a friend function. For accessing the data, the declaration of a friend function should be done inside the body of a class starting with the keyword friend.
  • 3. Declaration of friend function SYNTAX : class className { .. . friend returnType functionName (arguments) .. . }
  • 4. Characteristics of friend function The function definition does not use either the keyword friend or scope resolution operator. A friend function is not in the scope of the class, in which it has been declared as friend. It cannot be called using the object of that class. It can be invoked like a normal function without any object. Unlike member functions, it cannot use the member names directly. It can be declared in public or private part without affecting its meaning.
  • 5. Whats the use of friend function? Data hiding is a fundamental concept of object-oriented programming. It restricts the access of private members from outside of the class. Similarly, protected members can only be accessed by derived classes and are inaccessible from outside. For example,
  • 6. Example & Explanation class MyClass { private: int member 1 ; }; int main() { MyClass obj; // Error! Cannot access private members. obj.member 1=5; } Here we cannot access the member 1. Since it is a private member of the class.Hence, to avoid this we use FRIEND FUNCTONS.It allow us to access member functions from outside the class.
  • 7. EXAMPLE FOR FRIEND FUNCTION CODING: #include <iostream> Using namespace std; class Temperature { int celsius; public: Temperature( ) { celsius=0 } friend int temp(Temperature); }; int temp(Temperature t) { t.celsius=40; return t.celsius; } int main( ) { Temperature tm; cout <<Temperature in celsius:<<temp(tm) return 0; } CODING ,OUTPUT AND EXPLANATION
  • 8. Output & Explanation *OUTPUT: Temperature in celsius : 40 *EXPLANATION: Here we declared a function 'temp' as the friend function of the class 'Temperature'. In the friend function, we directly accessed the private member celsius of the class 'Temperature'.When the first statement of the main function created an object 'tm' of the class 'Temperature' thus calling its constructor and assigning a value 0 to its data member celsius.The second statement of the main function called the function 'temp' which assigned a value 40 to celsius