ๅฐŠๆ•ฌ็š„ ๅพฎไฟกๆฑ‡็Ž‡๏ผš1ๅ†† โ‰ˆ 0.046166 ๅ…ƒ ๆ”ฏไป˜ๅฎๆฑ‡็Ž‡๏ผš1ๅ†† โ‰ˆ 0.046257ๅ…ƒ [้€€ๅ‡บ็™ปๅฝ•]
SlideShare a Scribd company logo
G.H PATEL COLLEGE OF ENGINEERING & TECHNOLOGY
ALA OF OOPC(2140705)
COMPUTER DEPARTMENT
ACADEMIC YEAR:2017-18
TOPIC: FRIEND FUNCTION
โ€ข Prepared By:
NAME: ENROLLMENT NO:
HEET PATEL 160110107038
UMANG KUNDARIA 160110107023
WHAT IS FRIEND FUNCTION ?
โ€ข 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.
WHY IT IS NEEDED ?
โ€ข 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 subclasses.
โ€ข 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.
โ€ข 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.
โ€ข Syntax:
โ€ข class ABC
โ€ข {
โ€ฆโ€ฆโ€ฆโ€ฆ.
public:
friend void xyz(object of class);
};
CHARACTERICS OF FRIEND FUNCTION:
โ€ข 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:
โ€ข If we want to add two values using friend function, then here is code for it:
#include<iostream>
using namespace std;
class B;
class A
{
int a;
public:
void setdata(int c)
{
a=c;
}
friend void sum();
};
class B
{
int b;
public:
void getdata(int d)
{
b=d;
}
friend void sum();
};
void sum(A c1,B c2)
{
int c;
c=c1.a+c2.b;
cout<<โ€œSum of two values is:<<c;
}
int main()
{
A a;
B b;
a.setdata(4);
b.getdata(5);
sum(a,b);
return 0;
}
THANK YOU..!!

More Related Content

What's hot

Friends function and_classes
Friends function and_classesFriends function and_classes
Friends function and_classes
asadsardar
ย 
Friend function & friend class
Friend function & friend classFriend function & friend class
Friend function & friend class
Abhishek Wadhwa
ย 
Classes and objects
Classes and objectsClasses and objects
Classes and objects
Anil Kumar
ย 
Object oriented programming
Object oriented programmingObject oriented programming
Object oriented programming
umairrajpoot6
ย 
object oriented programming using c++
 object oriented programming using c++ object oriented programming using c++
object oriented programming using c++
fasalsial1fasalsial1
ย 
Lecture 10
Lecture 10Lecture 10
Lecture 10
Debasish Pratihari
ย 
Learn C# Programming - Classes & Inheritance
Learn C# Programming - Classes & InheritanceLearn C# Programming - Classes & Inheritance
Learn C# Programming - Classes & Inheritance
Eng Teong Cheah
ย 
Java interface
Java interface Java interface
Java interface
HoneyChintal
ย 
What is Interface in Java | How to implement Multiple Inheritance Using Inter...
What is Interface in Java | How to implement Multiple Inheritance Using Inter...What is Interface in Java | How to implement Multiple Inheritance Using Inter...
What is Interface in Java | How to implement Multiple Inheritance Using Inter...
Edureka!
ย 
More oop in java
More oop in javaMore oop in java
More oop in java
SAGARDAVE29
ย 
Object oriented programming
Object oriented programmingObject oriented programming
Object oriented programming
Saiful Islam Sany
ย 
Assigment 2
Assigment 2Assigment 2
Data types IN JAVA
Data types IN JAVAData types IN JAVA
Data types IN JAVA
garishma bhatia
ย 
Principles and advantages of oop ppt
Principles and advantages of oop pptPrinciples and advantages of oop ppt
Principles and advantages of oop ppt
daxesh chauhan
ย 
[OOP - Lec 01] Introduction to OOP
[OOP - Lec 01] Introduction to OOP[OOP - Lec 01] Introduction to OOP
[OOP - Lec 01] Introduction to OOP
Muhammad Hammad Waseem
ย 
Classes and Nested Classes in Java
Classes and Nested Classes in JavaClasses and Nested Classes in Java
Classes and Nested Classes in Java
Ravi_Kant_Sahu
ย 
Interface
InterfaceInterface
Interface
Shantilal Bhayal
ย 
Total oop in c# dot net
Total oop in c# dot netTotal oop in c# dot net
Total oop in c# dot net
Muhammad Naveed
ย 
Classes and objects
Classes and objectsClasses and objects

What's hot (19)

Friends function and_classes
Friends function and_classesFriends function and_classes
Friends function and_classes
ย 
Friend function & friend class
Friend function & friend classFriend function & friend class
Friend function & friend class
ย 
Classes and objects
Classes and objectsClasses and objects
Classes and objects
ย 
Object oriented programming
Object oriented programmingObject oriented programming
Object oriented programming
ย 
object oriented programming using c++
 object oriented programming using c++ object oriented programming using c++
object oriented programming using c++
ย 
Lecture 10
Lecture 10Lecture 10
Lecture 10
ย 
Learn C# Programming - Classes & Inheritance
Learn C# Programming - Classes & InheritanceLearn C# Programming - Classes & Inheritance
Learn C# Programming - Classes & Inheritance
ย 
Java interface
Java interface Java interface
Java interface
ย 
What is Interface in Java | How to implement Multiple Inheritance Using Inter...
What is Interface in Java | How to implement Multiple Inheritance Using Inter...What is Interface in Java | How to implement Multiple Inheritance Using Inter...
What is Interface in Java | How to implement Multiple Inheritance Using Inter...
ย 
More oop in java
More oop in javaMore oop in java
More oop in java
ย 
Object oriented programming
Object oriented programmingObject oriented programming
Object oriented programming
ย 
Assigment 2
Assigment 2Assigment 2
Assigment 2
ย 
Data types IN JAVA
Data types IN JAVAData types IN JAVA
Data types IN JAVA
ย 
Principles and advantages of oop ppt
Principles and advantages of oop pptPrinciples and advantages of oop ppt
Principles and advantages of oop ppt
ย 
[OOP - Lec 01] Introduction to OOP
[OOP - Lec 01] Introduction to OOP[OOP - Lec 01] Introduction to OOP
[OOP - Lec 01] Introduction to OOP
ย 
Classes and Nested Classes in Java
Classes and Nested Classes in JavaClasses and Nested Classes in Java
Classes and Nested Classes in Java
ย 
Interface
InterfaceInterface
Interface
ย 
Total oop in c# dot net
Total oop in c# dot netTotal oop in c# dot net
Total oop in c# dot net
ย 
Classes and objects
Classes and objectsClasses and objects
Classes and objects
ย 

Similar to Friend function

friend function(c++)
friend function(c++)friend function(c++)
friend function(c++)
Ritika Sharma
ย 
cse l 5.pptx
cse l 5.pptxcse l 5.pptx
cse l 5.pptx
KeshavKumar395652
ย 
class and object in c++.pptx
class and object in c++.pptxclass and object in c++.pptx
class and object in c++.pptx
Adarsh College, Hingoli
ย 
Class and objects
Class and objectsClass and objects
Class and objects
nafisa rahman
ย 
static member and static member fumctions.ppt
static member and static member fumctions.pptstatic member and static member fumctions.ppt
static member and static member fumctions.ppt
poojitsaid2021
ย 
Data members and member functions
Data members and member functionsData members and member functions
Data members and member functions
Harsh Patel
ย 
27c
27c27c
27c
Sireesh K
ย 
27csharp
27csharp27csharp
27csharp
Sireesh K
ย 
APL-2-classes and objects.ppt
APL-2-classes and objects.pptAPL-2-classes and objects.ppt
APL-2-classes and objects.ppt
srividyal2
ย 
2CPP09 - Encapsulation
2CPP09 - Encapsulation2CPP09 - Encapsulation
2CPP09 - Encapsulation
Michael Heron
ย 
Classes and objects
Classes and objectsClasses and objects
Classes and objects
Lovely Professional University
ย 
Introduction to C++ Class & Objects. Book Notes
Introduction to C++ Class & Objects. Book NotesIntroduction to C++ Class & Objects. Book Notes
Introduction to C++ Class & Objects. Book Notes
DSMS Group of Institutes
ย 
4 Classes & Objects
4 Classes & Objects4 Classes & Objects
4 Classes & Objects
Praveen M Jigajinni
ย 
Classes and objects
Classes and objectsClasses and objects
Classes and objects
Shailendra Veeru
ย 
Inheritance, friend function, virtual function, polymorphism
Inheritance, friend function, virtual function, polymorphismInheritance, friend function, virtual function, polymorphism
Inheritance, friend function, virtual function, polymorphism
Jawad 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
ย 
OOP Unit 2 - Classes and Object
OOP Unit 2 - Classes and ObjectOOP Unit 2 - Classes and Object
OOP Unit 2 - Classes and Object
dkpawar
ย 
oopm 2.pdf
oopm 2.pdfoopm 2.pdf
oopm 2.pdf
jayeshsoni49
ย 
Object Oriented Programming Constructors & Destructors
Object Oriented Programming  Constructors &  DestructorsObject Oriented Programming  Constructors &  Destructors
Object Oriented Programming Constructors & Destructors
anitashinde33
ย 
Class&objects
Class&objectsClass&objects
Class&objects
harivng
ย 

Similar to Friend function (20)

friend function(c++)
friend function(c++)friend function(c++)
friend function(c++)
ย 
cse l 5.pptx
cse l 5.pptxcse l 5.pptx
cse l 5.pptx
ย 
class and object in c++.pptx
class and object in c++.pptxclass and object in c++.pptx
class and object in c++.pptx
ย 
Class and objects
Class and objectsClass and objects
Class and objects
ย 
static member and static member fumctions.ppt
static member and static member fumctions.pptstatic member and static member fumctions.ppt
static member and static member fumctions.ppt
ย 
Data members and member functions
Data members and member functionsData members and member functions
Data members and member functions
ย 
27c
27c27c
27c
ย 
27csharp
27csharp27csharp
27csharp
ย 
APL-2-classes and objects.ppt
APL-2-classes and objects.pptAPL-2-classes and objects.ppt
APL-2-classes and objects.ppt
ย 
2CPP09 - Encapsulation
2CPP09 - Encapsulation2CPP09 - Encapsulation
2CPP09 - Encapsulation
ย 
Classes and objects
Classes and objectsClasses and objects
Classes and objects
ย 
Introduction to C++ Class & Objects. Book Notes
Introduction to C++ Class & Objects. Book NotesIntroduction to C++ Class & Objects. Book Notes
Introduction to C++ Class & Objects. Book Notes
ย 
4 Classes & Objects
4 Classes & Objects4 Classes & Objects
4 Classes & Objects
ย 
Classes and objects
Classes and objectsClasses and objects
Classes and objects
ย 
Inheritance, friend function, virtual function, polymorphism
Inheritance, friend function, virtual function, polymorphismInheritance, friend function, virtual function, polymorphism
Inheritance, friend function, virtual function, polymorphism
ย 
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.
ย 
OOP Unit 2 - Classes and Object
OOP Unit 2 - Classes and ObjectOOP Unit 2 - Classes and Object
OOP Unit 2 - Classes and Object
ย 
oopm 2.pdf
oopm 2.pdfoopm 2.pdf
oopm 2.pdf
ย 
Object Oriented Programming Constructors & Destructors
Object Oriented Programming  Constructors &  DestructorsObject Oriented Programming  Constructors &  Destructors
Object Oriented Programming Constructors & Destructors
ย 
Class&objects
Class&objectsClass&objects
Class&objects
ย 

Recently uploaded

nutrition in plants chapter 1 class 7...
nutrition in plants chapter 1 class 7...nutrition in plants chapter 1 class 7...
nutrition in plants chapter 1 class 7...
chaudharyreet2244
ย 
Diversity Quiz Finals by Quiz Club, IIT Kanpur
Diversity Quiz Finals by Quiz Club, IIT KanpurDiversity Quiz Finals by Quiz Club, IIT Kanpur
Diversity Quiz Finals by Quiz Club, IIT Kanpur
Quiz Club IIT Kanpur
ย 
Ethiopia and Eritrea Eritrea's journey has been marked by resilience and dete...
Ethiopia and Eritrea Eritrea's journey has been marked by resilience and dete...Ethiopia and Eritrea Eritrea's journey has been marked by resilience and dete...
Ethiopia and Eritrea Eritrea's journey has been marked by resilience and dete...
biruktesfaye27
ย 
Get Success with the Latest UiPath UIPATH-ADPV1 Exam Dumps (V11.02) 2024
Get Success with the Latest UiPath UIPATH-ADPV1 Exam Dumps (V11.02) 2024Get Success with the Latest UiPath UIPATH-ADPV1 Exam Dumps (V11.02) 2024
Get Success with the Latest UiPath UIPATH-ADPV1 Exam Dumps (V11.02) 2024
yarusun
ย 
How to Create a Stage or a Pipeline in Odoo 17 CRM
How to Create a Stage or a Pipeline in Odoo 17 CRMHow to Create a Stage or a Pipeline in Odoo 17 CRM
How to Create a Stage or a Pipeline in Odoo 17 CRM
Celine George
ย 
Science-9-Lesson-1-The Bohr Model-NLC.pptx pptx
Science-9-Lesson-1-The Bohr Model-NLC.pptx pptxScience-9-Lesson-1-The Bohr Model-NLC.pptx pptx
Science-9-Lesson-1-The Bohr Model-NLC.pptx pptx
Catherine Dela Cruz
ย 
Interprofessional Education Platform Introduction.pdf
Interprofessional Education Platform Introduction.pdfInterprofessional Education Platform Introduction.pdf
Interprofessional Education Platform Introduction.pdf
Ben Aldrich
ย 
Post init hook in the odoo 17 ERP Module
Post init hook in the  odoo 17 ERP ModulePost init hook in the  odoo 17 ERP Module
Post init hook in the odoo 17 ERP Module
Celine George
ย 
Keynote given on June 24 for MASSP at Grand Traverse City
Keynote given on June 24 for MASSP at Grand Traverse CityKeynote given on June 24 for MASSP at Grand Traverse City
Keynote given on June 24 for MASSP at Grand Traverse City
PJ Caposey
ย 
Decolonizing Universal Design for Learning
Decolonizing Universal Design for LearningDecolonizing Universal Design for Learning
Decolonizing Universal Design for Learning
Frederic Fovet
ย 
Brand Guideline of Bashundhara A4 Paper - 2024
Brand Guideline of Bashundhara A4 Paper - 2024Brand Guideline of Bashundhara A4 Paper - 2024
Brand Guideline of Bashundhara A4 Paper - 2024
khabri85
ย 
Talking Tech through Compelling Visual Aids
Talking Tech through Compelling Visual AidsTalking Tech through Compelling Visual Aids
Talking Tech through Compelling Visual Aids
MattVassar1
ย 
Information and Communication Technology in Education
Information and Communication Technology in EducationInformation and Communication Technology in Education
Information and Communication Technology in Education
MJDuyan
ย 
How to Create User Notification in Odoo 17
How to Create User Notification in Odoo 17How to Create User Notification in Odoo 17
How to Create User Notification in Odoo 17
Celine George
ย 
Bแป˜ Bร€I TแบฌP TEST THEO UNIT - FORM 2025 - TIแบพNG ANH 12 GLOBAL SUCCESS - KรŒ 1 (B...
Bแป˜ Bร€I TแบฌP TEST THEO UNIT - FORM 2025 - TIแบพNG ANH 12 GLOBAL SUCCESS - KรŒ 1 (B...Bแป˜ Bร€I TแบฌP TEST THEO UNIT - FORM 2025 - TIแบพNG ANH 12 GLOBAL SUCCESS - KรŒ 1 (B...
Bแป˜ Bร€I TแบฌP TEST THEO UNIT - FORM 2025 - TIแบพNG ANH 12 GLOBAL SUCCESS - KรŒ 1 (B...
Nguyen Thanh Tu Collection
ย 
CapTechTalks Webinar Slides June 2024 Donovan Wright.pptx
CapTechTalks Webinar Slides June 2024 Donovan Wright.pptxCapTechTalks Webinar Slides June 2024 Donovan Wright.pptx
CapTechTalks Webinar Slides June 2024 Donovan Wright.pptx
CapitolTechU
ย 
220711130095 Tanu Pandey message currency, communication speed & control EPC ...
220711130095 Tanu Pandey message currency, communication speed & control EPC ...220711130095 Tanu Pandey message currency, communication speed & control EPC ...
220711130095 Tanu Pandey message currency, communication speed & control EPC ...
Kalna College
ย 
IoT (Internet of Things) introduction Notes.pdf
IoT (Internet of Things) introduction Notes.pdfIoT (Internet of Things) introduction Notes.pdf
IoT (Internet of Things) introduction Notes.pdf
roshanranjit222
ย 
How to stay relevant as a cyber professional: Skills, trends and career paths...
How to stay relevant as a cyber professional: Skills, trends and career paths...How to stay relevant as a cyber professional: Skills, trends and career paths...
How to stay relevant as a cyber professional: Skills, trends and career paths...
Infosec
ย 
220711130097 Tulip Samanta Concept of Information and Communication Technology
220711130097 Tulip Samanta Concept of Information and Communication Technology220711130097 Tulip Samanta Concept of Information and Communication Technology
220711130097 Tulip Samanta Concept of Information and Communication Technology
Kalna College
ย 

Recently uploaded (20)

nutrition in plants chapter 1 class 7...
nutrition in plants chapter 1 class 7...nutrition in plants chapter 1 class 7...
nutrition in plants chapter 1 class 7...
ย 
Diversity Quiz Finals by Quiz Club, IIT Kanpur
Diversity Quiz Finals by Quiz Club, IIT KanpurDiversity Quiz Finals by Quiz Club, IIT Kanpur
Diversity Quiz Finals by Quiz Club, IIT Kanpur
ย 
Ethiopia and Eritrea Eritrea's journey has been marked by resilience and dete...
Ethiopia and Eritrea Eritrea's journey has been marked by resilience and dete...Ethiopia and Eritrea Eritrea's journey has been marked by resilience and dete...
Ethiopia and Eritrea Eritrea's journey has been marked by resilience and dete...
ย 
Get Success with the Latest UiPath UIPATH-ADPV1 Exam Dumps (V11.02) 2024
Get Success with the Latest UiPath UIPATH-ADPV1 Exam Dumps (V11.02) 2024Get Success with the Latest UiPath UIPATH-ADPV1 Exam Dumps (V11.02) 2024
Get Success with the Latest UiPath UIPATH-ADPV1 Exam Dumps (V11.02) 2024
ย 
How to Create a Stage or a Pipeline in Odoo 17 CRM
How to Create a Stage or a Pipeline in Odoo 17 CRMHow to Create a Stage or a Pipeline in Odoo 17 CRM
How to Create a Stage or a Pipeline in Odoo 17 CRM
ย 
Science-9-Lesson-1-The Bohr Model-NLC.pptx pptx
Science-9-Lesson-1-The Bohr Model-NLC.pptx pptxScience-9-Lesson-1-The Bohr Model-NLC.pptx pptx
Science-9-Lesson-1-The Bohr Model-NLC.pptx pptx
ย 
Interprofessional Education Platform Introduction.pdf
Interprofessional Education Platform Introduction.pdfInterprofessional Education Platform Introduction.pdf
Interprofessional Education Platform Introduction.pdf
ย 
Post init hook in the odoo 17 ERP Module
Post init hook in the  odoo 17 ERP ModulePost init hook in the  odoo 17 ERP Module
Post init hook in the odoo 17 ERP Module
ย 
Keynote given on June 24 for MASSP at Grand Traverse City
Keynote given on June 24 for MASSP at Grand Traverse CityKeynote given on June 24 for MASSP at Grand Traverse City
Keynote given on June 24 for MASSP at Grand Traverse City
ย 
Decolonizing Universal Design for Learning
Decolonizing Universal Design for LearningDecolonizing Universal Design for Learning
Decolonizing Universal Design for Learning
ย 
Brand Guideline of Bashundhara A4 Paper - 2024
Brand Guideline of Bashundhara A4 Paper - 2024Brand Guideline of Bashundhara A4 Paper - 2024
Brand Guideline of Bashundhara A4 Paper - 2024
ย 
Talking Tech through Compelling Visual Aids
Talking Tech through Compelling Visual AidsTalking Tech through Compelling Visual Aids
Talking Tech through Compelling Visual Aids
ย 
Information and Communication Technology in Education
Information and Communication Technology in EducationInformation and Communication Technology in Education
Information and Communication Technology in Education
ย 
How to Create User Notification in Odoo 17
How to Create User Notification in Odoo 17How to Create User Notification in Odoo 17
How to Create User Notification in Odoo 17
ย 
Bแป˜ Bร€I TแบฌP TEST THEO UNIT - FORM 2025 - TIแบพNG ANH 12 GLOBAL SUCCESS - KรŒ 1 (B...
Bแป˜ Bร€I TแบฌP TEST THEO UNIT - FORM 2025 - TIแบพNG ANH 12 GLOBAL SUCCESS - KรŒ 1 (B...Bแป˜ Bร€I TแบฌP TEST THEO UNIT - FORM 2025 - TIแบพNG ANH 12 GLOBAL SUCCESS - KรŒ 1 (B...
Bแป˜ Bร€I TแบฌP TEST THEO UNIT - FORM 2025 - TIแบพNG ANH 12 GLOBAL SUCCESS - KรŒ 1 (B...
ย 
CapTechTalks Webinar Slides June 2024 Donovan Wright.pptx
CapTechTalks Webinar Slides June 2024 Donovan Wright.pptxCapTechTalks Webinar Slides June 2024 Donovan Wright.pptx
CapTechTalks Webinar Slides June 2024 Donovan Wright.pptx
ย 
220711130095 Tanu Pandey message currency, communication speed & control EPC ...
220711130095 Tanu Pandey message currency, communication speed & control EPC ...220711130095 Tanu Pandey message currency, communication speed & control EPC ...
220711130095 Tanu Pandey message currency, communication speed & control EPC ...
ย 
IoT (Internet of Things) introduction Notes.pdf
IoT (Internet of Things) introduction Notes.pdfIoT (Internet of Things) introduction Notes.pdf
IoT (Internet of Things) introduction Notes.pdf
ย 
How to stay relevant as a cyber professional: Skills, trends and career paths...
How to stay relevant as a cyber professional: Skills, trends and career paths...How to stay relevant as a cyber professional: Skills, trends and career paths...
How to stay relevant as a cyber professional: Skills, trends and career paths...
ย 
220711130097 Tulip Samanta Concept of Information and Communication Technology
220711130097 Tulip Samanta Concept of Information and Communication Technology220711130097 Tulip Samanta Concept of Information and Communication Technology
220711130097 Tulip Samanta Concept of Information and Communication Technology
ย 

Friend function

  • 1. G.H PATEL COLLEGE OF ENGINEERING & TECHNOLOGY ALA OF OOPC(2140705) COMPUTER DEPARTMENT ACADEMIC YEAR:2017-18
  • 2. TOPIC: FRIEND FUNCTION โ€ข Prepared By: NAME: ENROLLMENT NO: HEET PATEL 160110107038 UMANG KUNDARIA 160110107023
  • 3. WHAT IS FRIEND FUNCTION ? โ€ข 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.
  • 4. WHY IT IS NEEDED ? โ€ข 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.
  • 5. 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 subclasses. โ€ข 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.
  • 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. โ€ข Syntax: โ€ข class ABC โ€ข { โ€ฆโ€ฆโ€ฆโ€ฆ. public: friend void xyz(object of class); };
  • 7. CHARACTERICS OF FRIEND FUNCTION: โ€ข 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.
  • 8. EXAMPLE: โ€ข If we want to add two values using friend function, then here is code for it: #include<iostream> using namespace std; class B; class A { int a; public: void setdata(int c) { a=c; }
  • 9. friend void sum(); }; class B { int b; public: void getdata(int d) { b=d; } friend void sum(); };
  • 10. void sum(A c1,B c2) { int c; c=c1.a+c2.b; cout<<โ€œSum of two values is:<<c; } int main() { A a; B b; a.setdata(4); b.getdata(5); sum(a,b); return 0; }
  ็ฟป่ฏ‘๏ผš