SlideShare a Scribd company logo
1 of 9
Download to read offline
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.
What’s 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)

[OOP - Lec 19] Static Member Functions
[OOP - Lec 19] Static Member Functions[OOP - Lec 19] Static Member Functions
[OOP - Lec 19] Static Member Functions
 
Function overloading ppt
Function overloading pptFunction overloading ppt
Function overloading ppt
 
Operator overloading
Operator overloadingOperator overloading
Operator overloading
 
Inheritance in JAVA PPT
Inheritance  in JAVA PPTInheritance  in JAVA PPT
Inheritance in JAVA PPT
 
Encapsulation C++
Encapsulation C++Encapsulation C++
Encapsulation C++
 
Inheritance ppt
Inheritance pptInheritance ppt
Inheritance ppt
 
Operator Overloading
Operator OverloadingOperator Overloading
Operator Overloading
 
Functions in c language
Functions in c language Functions in c language
Functions in c language
 
Friend Function
Friend FunctionFriend Function
Friend Function
 
class and objects
class and objectsclass and objects
class and objects
 
Inheritance in c++
Inheritance in c++Inheritance in c++
Inheritance in c++
 
Types of Constructor in C++
Types of Constructor in C++Types of Constructor in C++
Types of Constructor in C++
 
Inheritance
InheritanceInheritance
Inheritance
 
Strings in c++
Strings in c++Strings in c++
Strings in c++
 
C# classes objects
C#  classes objectsC#  classes objects
C# classes objects
 
Interface
InterfaceInterface
Interface
 
inheritance
inheritanceinheritance
inheritance
 
Abstraction java
Abstraction javaAbstraction java
Abstraction java
 
Interface in java
Interface in javaInterface in java
Interface in java
 
Inheritance in c++
Inheritance in c++Inheritance in c++
Inheritance in c++
 

Similar to Friend function in c++

Object and class presentation
Object and class presentationObject and class presentation
Object and class presentationnafisa rahman
 
Friend function
Friend functionFriend function
Friend functionHeet Patel
 
Access specifier
Access specifierAccess specifier
Access specifierzindadili
 
Data members and member functions
Data members and member functionsData members and member functions
Data members and member functionsMarlom46
 
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
 
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 SCIENCEVenugopalavarma Raja
 
Classes, objects and methods
Classes, objects and methodsClasses, objects and methods
Classes, objects and methodsfarhan amjad
 
chapter-7-classes-and-objects.pdf
chapter-7-classes-and-objects.pdfchapter-7-classes-and-objects.pdf
chapter-7-classes-and-objects.pdfstudy material
 
Chapter23 friend-function-friend-class
Chapter23 friend-function-friend-classChapter23 friend-function-friend-class
Chapter23 friend-function-friend-classDeepak Singh
 
Access controlaspecifier and visibilty modes
Access controlaspecifier and visibilty modesAccess controlaspecifier and visibilty modes
Access controlaspecifier and visibilty modesVinay Kumar
 
Member Function in C++
Member Function in C++ Member Function in C++
Member Function in C++ NikitaKaur10
 

Similar to Friend function in c++ (20)

Object and class presentation
Object and class presentationObject and class presentation
Object and class presentation
 
Friend function
Friend functionFriend function
Friend function
 
OOPs & C++ UNIT 3
OOPs & C++ UNIT 3OOPs & C++ UNIT 3
OOPs & C++ UNIT 3
 
Access specifier
Access specifierAccess specifier
Access specifier
 
Data members and member functions
Data members and member functionsData members and member functions
Data members and member functions
 
Lecture 2 (1)
Lecture 2 (1)Lecture 2 (1)
Lecture 2 (1)
 
class c++
class c++class c++
class c++
 
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.
 
C++ Notes
C++ NotesC++ Notes
C++ Notes
 
constructor , operator overloading ,friend function
constructor , operator overloading ,friend function constructor , operator overloading ,friend function
constructor , operator overloading ,friend function
 
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
 
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
 
00ps inheritace using c++
00ps inheritace using c++00ps inheritace using c++
00ps inheritace using c++
 
cse l 5.pptx
cse l 5.pptxcse l 5.pptx
cse l 5.pptx
 
Classes, objects and methods
Classes, objects and methodsClasses, objects and methods
Classes, objects and methods
 
chapter-7-classes-and-objects.pdf
chapter-7-classes-and-objects.pdfchapter-7-classes-and-objects.pdf
chapter-7-classes-and-objects.pdf
 
Chapter23 friend-function-friend-class
Chapter23 friend-function-friend-classChapter23 friend-function-friend-class
Chapter23 friend-function-friend-class
 
Access controlaspecifier and visibilty modes
Access controlaspecifier and visibilty modesAccess controlaspecifier and visibilty modes
Access controlaspecifier and visibilty modes
 
Member Function in C++
Member Function in C++ Member Function in C++
Member Function in C++
 
Member Function in C++
Member Function in C++Member Function in C++
Member Function in C++
 

Recently uploaded

Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)
Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)
Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)dollysharma2066
 
GDSC ASEB Gen AI study jams presentation
GDSC ASEB Gen AI study jams presentationGDSC ASEB Gen AI study jams presentation
GDSC ASEB Gen AI study jams presentationGDSCAESB
 
Introduction to Microprocesso programming and interfacing.pptx
Introduction to Microprocesso programming and interfacing.pptxIntroduction to Microprocesso programming and interfacing.pptx
Introduction to Microprocesso programming and interfacing.pptxvipinkmenon1
 
Call Girls Narol 7397865700 Independent Call Girls
Call Girls Narol 7397865700 Independent Call GirlsCall Girls Narol 7397865700 Independent Call Girls
Call Girls Narol 7397865700 Independent Call Girlsssuser7cb4ff
 
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...Soham Mondal
 
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdfCCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdfAsst.prof M.Gokilavani
 
Call Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile serviceCall Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile servicerehmti665
 
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube ExchangerStudy on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube ExchangerAnamika Sarkar
 
power system scada applications and uses
power system scada applications and usespower system scada applications and uses
power system scada applications and usesDevarapalliHaritha
 
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...VICTOR MAESTRE RAMIREZ
 
complete construction, environmental and economics information of biomass com...
complete construction, environmental and economics information of biomass com...complete construction, environmental and economics information of biomass com...
complete construction, environmental and economics information of biomass com...asadnawaz62
 
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVHARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVRajaP95
 
Current Transformer Drawing and GTP for MSETCL
Current Transformer Drawing and GTP for MSETCLCurrent Transformer Drawing and GTP for MSETCL
Current Transformer Drawing and GTP for MSETCLDeelipZope
 
Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024hassan khalil
 
Biology for Computer Engineers Course Handout.pptx
Biology for Computer Engineers Course Handout.pptxBiology for Computer Engineers Course Handout.pptx
Biology for Computer Engineers Course Handout.pptxDeepakSakkari2
 
Introduction-To-Agricultural-Surveillance-Rover.pptx
Introduction-To-Agricultural-Surveillance-Rover.pptxIntroduction-To-Agricultural-Surveillance-Rover.pptx
Introduction-To-Agricultural-Surveillance-Rover.pptxk795866
 
Internship report on mechanical engineering
Internship report on mechanical engineeringInternship report on mechanical engineering
Internship report on mechanical engineeringmalavadedarshan25
 
HARMONY IN THE HUMAN BEING - Unit-II UHV-2
HARMONY IN THE HUMAN BEING - Unit-II UHV-2HARMONY IN THE HUMAN BEING - Unit-II UHV-2
HARMONY IN THE HUMAN BEING - Unit-II UHV-2RajaP95
 

Recently uploaded (20)

Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)
Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)
Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)
 
GDSC ASEB Gen AI study jams presentation
GDSC ASEB Gen AI study jams presentationGDSC ASEB Gen AI study jams presentation
GDSC ASEB Gen AI study jams presentation
 
Introduction to Microprocesso programming and interfacing.pptx
Introduction to Microprocesso programming and interfacing.pptxIntroduction to Microprocesso programming and interfacing.pptx
Introduction to Microprocesso programming and interfacing.pptx
 
Call Girls Narol 7397865700 Independent Call Girls
Call Girls Narol 7397865700 Independent Call GirlsCall Girls Narol 7397865700 Independent Call Girls
Call Girls Narol 7397865700 Independent Call Girls
 
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
 
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdfCCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
 
Call Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile serviceCall Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile service
 
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube ExchangerStudy on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
 
power system scada applications and uses
power system scada applications and usespower system scada applications and uses
power system scada applications and uses
 
young call girls in Rajiv Chowk🔝 9953056974 🔝 Delhi escort Service
young call girls in Rajiv Chowk🔝 9953056974 🔝 Delhi escort Serviceyoung call girls in Rajiv Chowk🔝 9953056974 🔝 Delhi escort Service
young call girls in Rajiv Chowk🔝 9953056974 🔝 Delhi escort Service
 
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...
 
complete construction, environmental and economics information of biomass com...
complete construction, environmental and economics information of biomass com...complete construction, environmental and economics information of biomass com...
complete construction, environmental and economics information of biomass com...
 
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVHARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
 
Current Transformer Drawing and GTP for MSETCL
Current Transformer Drawing and GTP for MSETCLCurrent Transformer Drawing and GTP for MSETCL
Current Transformer Drawing and GTP for MSETCL
 
Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024
 
Biology for Computer Engineers Course Handout.pptx
Biology for Computer Engineers Course Handout.pptxBiology for Computer Engineers Course Handout.pptx
Biology for Computer Engineers Course Handout.pptx
 
Introduction-To-Agricultural-Surveillance-Rover.pptx
Introduction-To-Agricultural-Surveillance-Rover.pptxIntroduction-To-Agricultural-Surveillance-Rover.pptx
Introduction-To-Agricultural-Surveillance-Rover.pptx
 
Internship report on mechanical engineering
Internship report on mechanical engineeringInternship report on mechanical engineering
Internship report on mechanical engineering
 
HARMONY IN THE HUMAN BEING - Unit-II UHV-2
HARMONY IN THE HUMAN BEING - Unit-II UHV-2HARMONY IN THE HUMAN BEING - Unit-II UHV-2
HARMONY IN THE HUMAN BEING - Unit-II UHV-2
 
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
 

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. What’s 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