SlideShare a Scribd company logo
Friend Function in C++
Understanding Access Privileges
Beyond Class Boundaries
Presented by: [Your Name]
Introduction
• C++ supports Encapsulation by restricting
access to class members.
• But sometimes, we need external functions to
access private or protected members.
• This is where friend functions come in.
What is a Friend Function?
• A friend function is a non-member function
that is granted access to the private and
protected members of a class.
• Declared using the keyword 'friend' inside the
class.
Syntax
• class ClassName {
• friend returnType
functionName(arguments);
• };
• Declared inside the class with 'friend'
keyword.
• Defined outside the class like a regular
function.
Example Code
• class Box {
• private:
• int length;
• public:
• Box(int l) : length(l) {}
• friend void printLength(Box b);
• };
• void printLength(Box b) {
Key Points
• Friend function is not a member of the class.
• Can be declared in one or more classes.
• Can be a normal function, a friend class, or an
operator overload.
Use Cases
• When two or more classes need to access
each other's private data.
• For operator overloading (especially <<, >>, +,
etc.)
• When a non-member needs special access
privileges.
Friend Function vs Member
Function
• Aspect | Friend Function | Member
Function
• ----------------|----------------------|--------------------
----
• Belongs to | Outside class | Inside class
• Access private | Yes (if friend) | Yes
• Called using | Object as argument | Object
directly
Pros and Cons
• Advantages:
• • Increases flexibility
• • Enables external helper functions
• Disadvantages:
• • Breaks encapsulation
• • Reduces data hiding
Summary
• Friend functions provide controlled access to
private data.
• Must be used sparingly to maintain
encapsulation.
• Useful for operator overloading and external
utilities.
Questions?
• Any doubts or clarifications?

More Related Content

PPTX
Friend function
PPT
12-170211073924.ppthihello heoobchdhdhdhhd
PPT
friend function(c++)
PPTX
Friend function
PDF
Friend function in c++
PPTX
Study of Friend Function in C++ with examples
PPTX
424570636-friend-function-1-pptx.hiipptx
PPTX
friend class & friend function by Komal V Rokade.pptx
Friend function
12-170211073924.ppthihello heoobchdhdhdhhd
friend function(c++)
Friend function
Friend function in c++
Study of Friend Function in C++ with examples
424570636-friend-function-1-pptx.hiipptx
friend class & friend function by Komal V Rokade.pptx

Similar to Friend_Function_in_CPP its use and examples. (20)

PPTX
2.6 Friend Function which helps to learn how the friend function works
PPTX
L9_Friend Function.pptxyeyeyeyeyyeyeyeye
PDF
Lec_15_OOP_ObjectsAsParametersFriendClasses.pdf
PPTX
friend_derivedclasspresentation1234.pptx
PPTX
Friend Function
PPTX
Friend function & friend class
PPTX
Friend functions
PPT
Friends function and_classes
PPTX
OOP Week 3 Lecture 2.pptx
PPTX
cse l 5.pptx
PPT
static member and static member fumctions.ppt
PPTX
Friend function OOPC
PPTX
Friend function in c++
PPTX
Inheritance, friend function, virtual function, polymorphism
PDF
Functions in C++
PDF
Chapter23 friend-function-friend-class
PDF
Function overloading
PPTX
Class and object
PPTX
Classes and objects 21 aug
PDF
2nd puc computer science chapter 8 function overloading
2.6 Friend Function which helps to learn how the friend function works
L9_Friend Function.pptxyeyeyeyeyyeyeyeye
Lec_15_OOP_ObjectsAsParametersFriendClasses.pdf
friend_derivedclasspresentation1234.pptx
Friend Function
Friend function & friend class
Friend functions
Friends function and_classes
OOP Week 3 Lecture 2.pptx
cse l 5.pptx
static member and static member fumctions.ppt
Friend function OOPC
Friend function in c++
Inheritance, friend function, virtual function, polymorphism
Functions in C++
Chapter23 friend-function-friend-class
Function overloading
Class and object
Classes and objects 21 aug
2nd puc computer science chapter 8 function overloading
Ad

Recently uploaded (20)

PDF
RMMM.pdf make it easy to upload and study
PDF
GENETICS IN BIOLOGY IN SECONDARY LEVEL FORM 3
PPTX
human mycosis Human fungal infections are called human mycosis..pptx
PDF
Computing-Curriculum for Schools in Ghana
PPTX
Lesson notes of climatology university.
PDF
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
PDF
2.FourierTransform-ShortQuestionswithAnswers.pdf
PPTX
GDM (1) (1).pptx small presentation for students
PDF
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
PDF
01-Introduction-to-Information-Management.pdf
PDF
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
PPTX
master seminar digital applications in india
PDF
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
PDF
Chinmaya Tiranga quiz Grand Finale.pdf
PDF
VCE English Exam - Section C Student Revision Booklet
PDF
OBE - B.A.(HON'S) IN INTERIOR ARCHITECTURE -Ar.MOHIUDDIN.pdf
PPTX
Final Presentation General Medicine 03-08-2024.pptx
PPTX
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
PDF
Module 4: Burden of Disease Tutorial Slides S2 2025
PDF
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
RMMM.pdf make it easy to upload and study
GENETICS IN BIOLOGY IN SECONDARY LEVEL FORM 3
human mycosis Human fungal infections are called human mycosis..pptx
Computing-Curriculum for Schools in Ghana
Lesson notes of climatology university.
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
2.FourierTransform-ShortQuestionswithAnswers.pdf
GDM (1) (1).pptx small presentation for students
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
01-Introduction-to-Information-Management.pdf
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
master seminar digital applications in india
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
Chinmaya Tiranga quiz Grand Finale.pdf
VCE English Exam - Section C Student Revision Booklet
OBE - B.A.(HON'S) IN INTERIOR ARCHITECTURE -Ar.MOHIUDDIN.pdf
Final Presentation General Medicine 03-08-2024.pptx
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
Module 4: Burden of Disease Tutorial Slides S2 2025
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
Ad

Friend_Function_in_CPP its use and examples.

  • 1. Friend Function in C++ Understanding Access Privileges Beyond Class Boundaries Presented by: [Your Name]
  • 2. Introduction • C++ supports Encapsulation by restricting access to class members. • But sometimes, we need external functions to access private or protected members. • This is where friend functions come in.
  • 3. What is a Friend Function? • A friend function is a non-member function that is granted access to the private and protected members of a class. • Declared using the keyword 'friend' inside the class.
  • 4. Syntax • class ClassName { • friend returnType functionName(arguments); • }; • Declared inside the class with 'friend' keyword. • Defined outside the class like a regular function.
  • 5. Example Code • class Box { • private: • int length; • public: • Box(int l) : length(l) {} • friend void printLength(Box b); • }; • void printLength(Box b) {
  • 6. Key Points • Friend function is not a member of the class. • Can be declared in one or more classes. • Can be a normal function, a friend class, or an operator overload.
  • 7. Use Cases • When two or more classes need to access each other's private data. • For operator overloading (especially <<, >>, +, etc.) • When a non-member needs special access privileges.
  • 8. Friend Function vs Member Function • Aspect | Friend Function | Member Function • ----------------|----------------------|-------------------- ---- • Belongs to | Outside class | Inside class • Access private | Yes (if friend) | Yes • Called using | Object as argument | Object directly
  • 9. Pros and Cons • Advantages: • • Increases flexibility • • Enables external helper functions • Disadvantages: • • Breaks encapsulation • • Reduces data hiding
  • 10. Summary • Friend functions provide controlled access to private data. • Must be used sparingly to maintain encapsulation. • Useful for operator overloading and external utilities.
  • 11. Questions? • Any doubts or clarifications?