SlideShare a Scribd company logo
2
Most read
9
Most read
12
Most read
Friend functions
Access privileges in C++.
You have access privileges in C++ such as public, protected
and private that helps in encapsulation of data at various
level.
Private
If data are declared as private in a class then it is accessible by the
member functions of the class where they are declared. The private
member functions can be accessed only by the members of the class.
By default, any member of the class is considered as private by the C+
+ compiler, if no specifier is declared for the member.
Public
The member functions with public access specifier can be accessed
outside of the class. This kind of members is accessed by creating
instance of the cass.
Protected
Protected members are accessible by the class itself and it's sub-
classes. The members with protected specifier act exactly like
private as long as they are referenced within the class or from the
instance of the class. This specifier specially used when you need
to use inheritance facility of C++. The protected members
become private of a child class in case of private inheritance,
public in case of public inheritance, and stay protected in case of
protected inheritance.
Access specifier
Visible to own
class members
Visible to
objects of
same/other
class
public Yes Yes
private Yes No
protected Yes No
When we want our private data to be shared by a non
member function
Then:
Basically, we declare something as a friend, you give it access
to your private data members.
Single functions or entire classes may be declared as friends
of a class.
• A Friend function is a non-member function of the class that
has been granted access to all private members of the class.
• We simply declare the function within the class by a prefixing
its declaration with keyword friend.
• Function definition must not use keyword friend.
• Definition of friend function is specified outside the class body
and is not treated as a part of the class.
• The major difference b/w member function and friend
function is that the member function is accessed through the
object while friend function requires object to be passed as
parameter.
Syntax:
class ABC
{
………….
public:
friend void xyz(object of class);
};
Friend function characterstics
It is not in scope of class.
It cannot be called using object of that class.
It can be invoked like a normal function.
It should use a dot operator for accessing members.
It can be public or private.
It has objects as arguments.
Perhaps the most common use of friend functions is
overloading << and >> for I/O.
Example
class demo
{
int x;
public:
demo(int a)
{
x=a;
}
friend void display(demo);
};
void display(demo d1)
{
cout<<d1.x;
}
void main()
{
demo d2(5);
display(d2);
}
 class sample
{
int a;
int b;
public:
void setval(){ a=25,b=40}
friend float mean(sample s);
};
float mean(sample s)
{ return (s.a+s.b)/2.0;
}
void main()
{ sample X;
cout<<mean(X);
}
Friend class
In previous section of class we declared only one function as a
friend of another class.but it is possible that all member of the
one class can be friend of another class.this is friend class
Friends (a few gory details)
Friendship is not inherited, transitive, or reciprocal.
Derived classes don’t receive the privileges of friendship (more
on this when we get to inheritance in a few classes).
The privileges of friendship aren’t transitive. If class A declares
class B as a friend, and class B declares class C as a friend, class
C doesn’t necessarily have any special access rights to class A.
If class A declares class B as a friend (so class B can see class A’s
private members), class A is not automatically a friend of class
B (so class A cannot necessarily see the private data members of
class B).
Friendship is not inherited, transitive, or reciprocal.
Example
class demo
{
private:
int x,y;
public:
demo(int a,int b)
{
x=a;
y=b;
}
friend class frnd;
};
class frnd
{
public:
void display(demo d1)
{
cout<<“x is=”d1.x;
cout<<“y is=”d1.y;
}
};
void main()
{
demo d2(10,40);
frnd f1;
f1.display(d2);
getch();
}
This pointer
Every object in C++ has access to its own address
through an important pointer called this pointer. The
this pointer is an implicit parameter to all member
functions. Therefore, inside a member function, this may
be used to refer to the invoking object.
Friend functions do not have a this pointer, because
friends are not members of a class. Only member
functions have a this pointer.

More Related Content

PPT
C++ classes tutorials
Mayank Jain
 
PDF
Constructor and Destructor
Kamal Acharya
 
PDF
Friend function in c++
University of Madras
 
PPTX
Friend function
zindadili
 
PDF
Classes and objects
Nilesh Dalvi
 
PPTX
OOPS Basics With Example
Thooyavan Venkatachalam
 
PPTX
Polymorphism In c++
Vishesh Jha
 
PDF
Operator overloading
Pranali Chaudhari
 
C++ classes tutorials
Mayank Jain
 
Constructor and Destructor
Kamal Acharya
 
Friend function in c++
University of Madras
 
Friend function
zindadili
 
Classes and objects
Nilesh Dalvi
 
OOPS Basics With Example
Thooyavan Venkatachalam
 
Polymorphism In c++
Vishesh Jha
 
Operator overloading
Pranali Chaudhari
 

What's hot (20)

PPTX
classes and objects in C++
HalaiHansaika
 
PPTX
Static Data Members and Member Functions
MOHIT AGARWAL
 
PPTX
Inheritance in java
RahulAnanda1
 
PPTX
[OOP - Lec 19] Static Member Functions
Muhammad Hammad Waseem
 
PPTX
Constructor and Types of Constructors
Dhrumil Panchal
 
PPT
Class and object in C++
rprajat007
 
PPTX
Pure virtual function and abstract class
Amit Trivedi
 
PPTX
Oop c++class(final).ppt
Alok Kumar
 
PDF
C++ Files and Streams
Ahmed Farag
 
PPTX
Constructors and Destructor in C++
International Institute of Information Technology (I²IT)
 
PPTX
Inheritance in c++
Vineeta Garg
 
PPTX
Inheritance in c++
Vishal Patil
 
PPSX
Files in c++
Selvin Josy Bai Somu
 
PPTX
Type casting in java
Farooq Baloch
 
PPTX
Polymorphism in c++(ppt)
Sanjit Shaw
 
PPTX
Data types in c++
Venkata.Manish Reddy
 
PPTX
Type conversion
PreethaPreetha5
 
PDF
Object oriented programming c++
Ankur Pandey
 
PDF
Constructors and Destructors
Dr Sukhpal Singh Gill
 
PPTX
Inheritance in java
Tech_MX
 
classes and objects in C++
HalaiHansaika
 
Static Data Members and Member Functions
MOHIT AGARWAL
 
Inheritance in java
RahulAnanda1
 
[OOP - Lec 19] Static Member Functions
Muhammad Hammad Waseem
 
Constructor and Types of Constructors
Dhrumil Panchal
 
Class and object in C++
rprajat007
 
Pure virtual function and abstract class
Amit Trivedi
 
Oop c++class(final).ppt
Alok Kumar
 
C++ Files and Streams
Ahmed Farag
 
Inheritance in c++
Vineeta Garg
 
Inheritance in c++
Vishal Patil
 
Files in c++
Selvin Josy Bai Somu
 
Type casting in java
Farooq Baloch
 
Polymorphism in c++(ppt)
Sanjit Shaw
 
Data types in c++
Venkata.Manish Reddy
 
Type conversion
PreethaPreetha5
 
Object oriented programming c++
Ankur Pandey
 
Constructors and Destructors
Dr Sukhpal Singh Gill
 
Inheritance in java
Tech_MX
 
Ad

Similar to friend function(c++) (20)

PPT
12-170211073924.ppthihello heoobchdhdhdhhd
SanskritiGupta39
 
PPTX
Friend function
Heet Patel
 
PPTX
Friend functions
Megha Singh
 
PPTX
cse l 5.pptx
KeshavKumar395652
 
PPTX
Friend_Function_in_CPP its use and examples.
ZakiAnwer7
 
PPTX
friend class & friend function by Komal V Rokade.pptx
komalrokade4
 
PPT
Friends function and_classes
asadsardar
 
PPT
static member and static member fumctions.ppt
poojitsaid2021
 
PPTX
424570636-friend-function-1-pptx.hiipptx
SanskritiGupta39
 
PPTX
L9_Friend Function.pptxyeyeyeyeyyeyeyeye
DhruvBharadwaj4
 
PPTX
2.6 Friend Function which helps to learn how the friend function works
giri08n07
 
PDF
Lec_15_OOP_ObjectsAsParametersFriendClasses.pdf
AneesAbbasi14
 
PPTX
OOP Week 3 Lecture 2.pptx
whoiam36
 
PDF
22 scheme OOPs with C++ BCS306B_module1.pdf
sindhus795217
 
PPTX
Friend Function
Mehak Tawakley
 
PPTX
friend_derivedclasspresentation1234.pptx
soniasharmafdp
 
PPT
DS Unit 6.ppt
JITTAYASHWANTHREDDY
 
PPTX
Friend function in c++
Jagrati Mehra
 
PDF
Chapter23 friend-function-friend-class
Deepak Singh
 
PPTX
Friend function & friend class
Abhishek Wadhwa
 
12-170211073924.ppthihello heoobchdhdhdhhd
SanskritiGupta39
 
Friend function
Heet Patel
 
Friend functions
Megha Singh
 
cse l 5.pptx
KeshavKumar395652
 
Friend_Function_in_CPP its use and examples.
ZakiAnwer7
 
friend class & friend function by Komal V Rokade.pptx
komalrokade4
 
Friends function and_classes
asadsardar
 
static member and static member fumctions.ppt
poojitsaid2021
 
424570636-friend-function-1-pptx.hiipptx
SanskritiGupta39
 
L9_Friend Function.pptxyeyeyeyeyyeyeyeye
DhruvBharadwaj4
 
2.6 Friend Function which helps to learn how the friend function works
giri08n07
 
Lec_15_OOP_ObjectsAsParametersFriendClasses.pdf
AneesAbbasi14
 
OOP Week 3 Lecture 2.pptx
whoiam36
 
22 scheme OOPs with C++ BCS306B_module1.pdf
sindhus795217
 
Friend Function
Mehak Tawakley
 
friend_derivedclasspresentation1234.pptx
soniasharmafdp
 
DS Unit 6.ppt
JITTAYASHWANTHREDDY
 
Friend function in c++
Jagrati Mehra
 
Chapter23 friend-function-friend-class
Deepak Singh
 
Friend function & friend class
Abhishek Wadhwa
 
Ad

Recently uploaded (20)

PDF
FLEX-LNG-Company-Presentation-Nov-2017.pdf
jbloggzs
 
PPTX
FUNDAMENTALS OF ELECTRIC VEHICLES UNIT-1
MikkiliSuresh
 
PDF
Packaging Tips for Stainless Steel Tubes and Pipes
heavymetalsandtubes
 
PDF
2010_Book_EnvironmentalBioengineering (1).pdf
EmilianoRodriguezTll
 
PDF
settlement FOR FOUNDATION ENGINEERS.pdf
Endalkazene
 
PDF
Natural_Language_processing_Unit_I_notes.pdf
sanguleumeshit
 
PPTX
22PCOAM21 Session 2 Understanding Data Source.pptx
Guru Nanak Technical Institutions
 
PDF
20ME702-Mechatronics-UNIT-1,UNIT-2,UNIT-3,UNIT-4,UNIT-5, 2025-2026
Mohanumar S
 
PPTX
sunil mishra pptmmmmmmmmmmmmmmmmmmmmmmmmm
singhamit111
 
PPTX
quantum computing transition from classical mechanics.pptx
gvlbcy
 
PPTX
Information Retrieval and Extraction - Module 7
premSankar19
 
PDF
2025 Laurence Sigler - Advancing Decision Support. Content Management Ecommer...
Francisco Javier Mora Serrano
 
PDF
Introduction to Ship Engine Room Systems.pdf
Mahmoud Moghtaderi
 
PDF
Unit I Part II.pdf : Security Fundamentals
Dr. Madhuri Jawale
 
PPTX
Civil Engineering Practices_BY Sh.JP Mishra 23.09.pptx
bineetmishra1990
 
PDF
Construction of a Thermal Vacuum Chamber for Environment Test of Triple CubeS...
2208441
 
PDF
Chad Ayach - A Versatile Aerospace Professional
Chad Ayach
 
PPTX
IoT_Smart_Agriculture_Presentations.pptx
poojakumari696707
 
PPTX
Online Cab Booking and Management System.pptx
diptipaneri80
 
PPTX
22PCOAM21 Session 1 Data Management.pptx
Guru Nanak Technical Institutions
 
FLEX-LNG-Company-Presentation-Nov-2017.pdf
jbloggzs
 
FUNDAMENTALS OF ELECTRIC VEHICLES UNIT-1
MikkiliSuresh
 
Packaging Tips for Stainless Steel Tubes and Pipes
heavymetalsandtubes
 
2010_Book_EnvironmentalBioengineering (1).pdf
EmilianoRodriguezTll
 
settlement FOR FOUNDATION ENGINEERS.pdf
Endalkazene
 
Natural_Language_processing_Unit_I_notes.pdf
sanguleumeshit
 
22PCOAM21 Session 2 Understanding Data Source.pptx
Guru Nanak Technical Institutions
 
20ME702-Mechatronics-UNIT-1,UNIT-2,UNIT-3,UNIT-4,UNIT-5, 2025-2026
Mohanumar S
 
sunil mishra pptmmmmmmmmmmmmmmmmmmmmmmmmm
singhamit111
 
quantum computing transition from classical mechanics.pptx
gvlbcy
 
Information Retrieval and Extraction - Module 7
premSankar19
 
2025 Laurence Sigler - Advancing Decision Support. Content Management Ecommer...
Francisco Javier Mora Serrano
 
Introduction to Ship Engine Room Systems.pdf
Mahmoud Moghtaderi
 
Unit I Part II.pdf : Security Fundamentals
Dr. Madhuri Jawale
 
Civil Engineering Practices_BY Sh.JP Mishra 23.09.pptx
bineetmishra1990
 
Construction of a Thermal Vacuum Chamber for Environment Test of Triple CubeS...
2208441
 
Chad Ayach - A Versatile Aerospace Professional
Chad Ayach
 
IoT_Smart_Agriculture_Presentations.pptx
poojakumari696707
 
Online Cab Booking and Management System.pptx
diptipaneri80
 
22PCOAM21 Session 1 Data Management.pptx
Guru Nanak Technical Institutions
 

friend function(c++)

  • 2. Access privileges in C++. You have access privileges in C++ such as public, protected and private that helps in encapsulation of data at various level.
  • 3. Private If data are declared as private in a class then it is accessible by the member functions of the class where they are declared. The private member functions can be accessed only by the members of the class. By default, any member of the class is considered as private by the C+ + compiler, if no specifier is declared for the member. Public The member functions with public access specifier can be accessed outside of the class. This kind of members is accessed by creating instance of the cass.
  • 4. Protected Protected members are accessible by the class itself and it's sub- classes. The members with protected specifier act exactly like private as long as they are referenced within the class or from the instance of the class. This specifier specially used when you need to use inheritance facility of C++. The protected members become private of a child class in case of private inheritance, public in case of public inheritance, and stay protected in case of protected inheritance.
  • 5. Access specifier Visible to own class members Visible to objects of same/other class public Yes Yes private Yes No protected Yes No
  • 6. When we want our private data to be shared by a non member function Then: Basically, we declare something as a friend, you give it access to your private data members. Single functions or entire classes may be declared as friends of a class.
  • 7. • A Friend function is a non-member function of the class that has been granted access to all private members of the class. • We simply declare the function within the class by a prefixing its declaration with keyword friend. • Function definition must not use keyword friend. • Definition of friend function is specified outside the class body and is not treated as a part of the class. • The major difference b/w member function and friend function is that the member function is accessed through the object while friend function requires object to be passed as parameter.
  • 9. Friend function characterstics It is not in scope of class. It cannot be called using object of that class. It can be invoked like a normal function. It should use a dot operator for accessing members. It can be public or private. It has objects as arguments. Perhaps the most common use of friend functions is overloading << and >> for I/O.
  • 10. Example class demo { int x; public: demo(int a) { x=a; } friend void display(demo); };
  • 11. void display(demo d1) { cout<<d1.x; } void main() { demo d2(5); display(d2); }
  • 12.  class sample { int a; int b; public: void setval(){ a=25,b=40} friend float mean(sample s); }; float mean(sample s) { return (s.a+s.b)/2.0; } void main() { sample X; cout<<mean(X); }
  • 13. Friend class In previous section of class we declared only one function as a friend of another class.but it is possible that all member of the one class can be friend of another class.this is friend class
  • 14. Friends (a few gory details) Friendship is not inherited, transitive, or reciprocal. Derived classes don’t receive the privileges of friendship (more on this when we get to inheritance in a few classes). The privileges of friendship aren’t transitive. If class A declares class B as a friend, and class B declares class C as a friend, class C doesn’t necessarily have any special access rights to class A. If class A declares class B as a friend (so class B can see class A’s private members), class A is not automatically a friend of class B (so class A cannot necessarily see the private data members of class B). Friendship is not inherited, transitive, or reciprocal.
  • 15. Example class demo { private: int x,y; public: demo(int a,int b) { x=a; y=b; } friend class frnd; };
  • 16. class frnd { public: void display(demo d1) { cout<<“x is=”d1.x; cout<<“y is=”d1.y; } };
  • 17. void main() { demo d2(10,40); frnd f1; f1.display(d2); getch(); }
  • 18. This pointer Every object in C++ has access to its own address through an important pointer called this pointer. The this pointer is an implicit parameter to all member functions. Therefore, inside a member function, this may be used to refer to the invoking object. Friend functions do not have a this pointer, because friends are not members of a class. Only member functions have a this pointer.