SlideShare ist ein Scribd-Unternehmen logo
1 von 14
h
 By

default : functions and data of a class are
private to that class
 Only the public members are accessible
outside the class
 Protected members can be inherited along
with public members
 No such condition where private members
can be accessed from outside the class
 So Friend Functions and Friend Classes may
be used where our application requires the
access all the members of a class
 Not

a member of the class
 Invoked like normal function without any object
 Full access to private and protected members
Of the class
 But can use the members for one or more
specific objects
 Called without the use dot operator(does not
need to be qualified with object’s name)


Include its prototype in the class , preceding
it with keyword friend

Syntax: friend ret_type func_name(arguments);
 Can

be declared anywhere (in public,
protected or private section) in the class
 May have no arguments
 Objects of the class or their pointers can be
passed as arguments to the friend function
Class myclass
{ int a,b;
Public:
myclass(int x,int y)
{ a=x; b=y; }
friend int sum(myclass m); // declaration
}
int sum(myclass m)
{
return m.a+ m.b; }
void main()
{
myclass my(10,20);
cout<<sum(my); //calling the friend function
}
 As

a function can be friend of more than one
class, it can be used for message passing
between the classes.
 As it is not a member of the class ,it does not
have a this pointer. So can be used for
Operator overloading. The operands are
passed explicitly to the overloaded friend
operator function.
 Make I/O functions easier
class A; // forward declaration
class B
{
int b;
friend int sum(A a1, B b1);
};
class A
{
int a;
friend int sum(A a1, B b1);
};
int sum (A a1,B b1)
{
return a1.a + b1.b;
}
Class overload
{
int i,j;
public:
overload(int a,int b)
{
i=a;
j=b;
}
void disp()
{
cout<< i<<“ “<<j;
}
friend overload operator +(int ,overload );
};
//overloading binary operator
overload operator+(int a, overload obj)
{
overload obj1;
obj1.i= a + obj.i;
obj1.j= a + obj.j;
return obj1;
}
main()
{
overload ov(40,76) ;
overload o =10+ov;
o.disp();
//output: 50 86
}
Class one
{
int x;
void func(two & );
};
Class two
{
int y;
friend void one:: func(two & );
};
void one:: func(two & t)
{ t.y=this->x; //called with an object of “one”
}
 One

class friend of another class

Syntax: friend class class_name;
 The

friend class and all of its member
functions have access to the private
members defined within the other class
 Provides additional functionality from
outside the class
Class one
{ int a;
friend class two;
};
Class two
{ void disp(one o1)
cout<<o1.a;
};
main()
{
two t;
one o;
t.disp(o);
}
 Friend

functions and classes are not inherited
 Friend function cannot have storage-class
specifier i.e. cannot be declared as static or
extern
 Friend classes are not corresponded i.e. if
class A is a friend of B it does not imply that
class B is a friend of A
 Friend classes are not transitive: i.e. friend
of a friend is not considered to be a friend
unless explicitly specified
Friend functions

Weitere ähnliche Inhalte

Was ist angesagt?

Object Oriented Programming Using C++
Object Oriented Programming Using C++Object Oriented Programming Using C++
Object Oriented Programming Using C++Muhammad Waqas
 
Inheritance in c++
Inheritance in c++Inheritance in c++
Inheritance in c++Vineeta Garg
 
Virtual function in C++ Pure Virtual Function
Virtual function in C++ Pure Virtual Function Virtual function in C++ Pure Virtual Function
Virtual function in C++ Pure Virtual Function Kamlesh Makvana
 
sSCOPE RESOLUTION OPERATOR.pptx
sSCOPE RESOLUTION OPERATOR.pptxsSCOPE RESOLUTION OPERATOR.pptx
sSCOPE RESOLUTION OPERATOR.pptxNidhi Mehra
 
Classes and objects
Classes and objectsClasses and objects
Classes and objectsNilesh Dalvi
 
Inheritance in c++
Inheritance in c++Inheritance in c++
Inheritance in c++Paumil Patel
 
Virtual base class
Virtual base classVirtual base class
Virtual base classTech_MX
 
Functions in c language
Functions in c language Functions in c language
Functions in c language tanmaymodi4
 
Operator overloading
Operator overloadingOperator overloading
Operator overloadingBurhan Ahmed
 
Templates in C++
Templates in C++Templates in C++
Templates in C++Tech_MX
 
Pointers,virtual functions and polymorphism cpp
Pointers,virtual functions and polymorphism cppPointers,virtual functions and polymorphism cpp
Pointers,virtual functions and polymorphism cpprajshreemuthiah
 
Constructors and destructors
Constructors and destructorsConstructors and destructors
Constructors and destructorsNilesh Dalvi
 
Friend function & friend class
Friend function & friend classFriend function & friend class
Friend function & friend classAbhishek Wadhwa
 

Was ist angesagt? (20)

Object Oriented Programming Using C++
Object Oriented Programming Using C++Object Oriented Programming Using C++
Object Oriented Programming Using C++
 
Inheritance in c++
Inheritance in c++Inheritance in c++
Inheritance in c++
 
Virtual function in C++ Pure Virtual Function
Virtual function in C++ Pure Virtual Function Virtual function in C++ Pure Virtual Function
Virtual function in C++ Pure Virtual Function
 
sSCOPE RESOLUTION OPERATOR.pptx
sSCOPE RESOLUTION OPERATOR.pptxsSCOPE RESOLUTION OPERATOR.pptx
sSCOPE RESOLUTION OPERATOR.pptx
 
07. Virtual Functions
07. Virtual Functions07. Virtual Functions
07. Virtual Functions
 
Classes and objects
Classes and objectsClasses and objects
Classes and objects
 
Operator overloading
Operator overloadingOperator overloading
Operator overloading
 
Constructors and Destructor in C++
Constructors and Destructor in C++Constructors and Destructor in C++
Constructors and Destructor in C++
 
Inheritance in c++
Inheritance in c++Inheritance in c++
Inheritance in c++
 
Classes and objects
Classes and objectsClasses and objects
Classes and objects
 
Pointers in c++
Pointers in c++Pointers in c++
Pointers in c++
 
Virtual base class
Virtual base classVirtual base class
Virtual base class
 
This pointer
This pointerThis pointer
This pointer
 
Functions in c language
Functions in c language Functions in c language
Functions in c language
 
Operator overloading
Operator overloadingOperator overloading
Operator overloading
 
Templates in C++
Templates in C++Templates in C++
Templates in C++
 
Pointers,virtual functions and polymorphism cpp
Pointers,virtual functions and polymorphism cppPointers,virtual functions and polymorphism cpp
Pointers,virtual functions and polymorphism cpp
 
Constructors and destructors
Constructors and destructorsConstructors and destructors
Constructors and destructors
 
Friend function & friend class
Friend function & friend classFriend function & friend class
Friend function & friend class
 
Friend Function
Friend FunctionFriend Function
Friend Function
 

Andere mochten auch

Friends function and_classes
Friends function and_classesFriends function and_classes
Friends function and_classesasadsardar
 
Chapter23 friend-function-friend-class
Chapter23 friend-function-friend-classChapter23 friend-function-friend-class
Chapter23 friend-function-friend-classDeepak Singh
 
Inline function in C++
Inline function in C++Inline function in C++
Inline function in C++Learn By Watch
 
[OOP - Lec 19] Static Member Functions
[OOP - Lec 19] Static Member Functions[OOP - Lec 19] Static Member Functions
[OOP - Lec 19] Static Member FunctionsMuhammad Hammad Waseem
 
Oop c++class(final).ppt
Oop c++class(final).pptOop c++class(final).ppt
Oop c++class(final).pptAlok Kumar
 
Friends!!!!
Friends!!!!Friends!!!!
Friends!!!!merua7
 
02 functions, variables, basic input and output of c++
02   functions, variables, basic input and output of c++02   functions, variables, basic input and output of c++
02 functions, variables, basic input and output of c++Manzoor ALam
 
11 constructors in derived classes
11 constructors in derived classes11 constructors in derived classes
11 constructors in derived classesDocent Education
 
Multi level hierarchy
Multi level hierarchyMulti level hierarchy
Multi level hierarchymyrajendra
 
Introduction to object oriented programming
Introduction to object oriented programmingIntroduction to object oriented programming
Introduction to object oriented programmingAbzetdin Adamov
 
This pointer .17
This pointer .17This pointer .17
This pointer .17myrajendra
 
Inline Functions and Default arguments
Inline Functions and Default argumentsInline Functions and Default arguments
Inline Functions and Default argumentsNikhil Pandit
 
2CPP06 - Arrays and Pointers
2CPP06 - Arrays and Pointers2CPP06 - Arrays and Pointers
2CPP06 - Arrays and PointersMichael Heron
 
Classes and Nested Classes in Java
Classes and Nested Classes in JavaClasses and Nested Classes in Java
Classes and Nested Classes in JavaRavi_Kant_Sahu
 
Console Io Operations
Console Io OperationsConsole Io Operations
Console Io Operationsarchikabhatia
 
Data members and member functions
Data members and member functionsData members and member functions
Data members and member functionsMarlom46
 
Object as function argument , friend and static function by shahzad younas
Object as function argument , friend and static function by shahzad younasObject as function argument , friend and static function by shahzad younas
Object as function argument , friend and static function by shahzad younasShahzad Younas
 

Andere mochten auch (20)

Friends function and_classes
Friends function and_classesFriends function and_classes
Friends function and_classes
 
Friend function 6
Friend function 6Friend function 6
Friend function 6
 
Chapter23 friend-function-friend-class
Chapter23 friend-function-friend-classChapter23 friend-function-friend-class
Chapter23 friend-function-friend-class
 
Inline function in C++
Inline function in C++Inline function in C++
Inline function in C++
 
[OOP - Lec 19] Static Member Functions
[OOP - Lec 19] Static Member Functions[OOP - Lec 19] Static Member Functions
[OOP - Lec 19] Static Member Functions
 
Constructors & destructors
Constructors & destructorsConstructors & destructors
Constructors & destructors
 
Oop c++class(final).ppt
Oop c++class(final).pptOop c++class(final).ppt
Oop c++class(final).ppt
 
Friends!!!!
Friends!!!!Friends!!!!
Friends!!!!
 
02 functions, variables, basic input and output of c++
02   functions, variables, basic input and output of c++02   functions, variables, basic input and output of c++
02 functions, variables, basic input and output of c++
 
Overloading
OverloadingOverloading
Overloading
 
11 constructors in derived classes
11 constructors in derived classes11 constructors in derived classes
11 constructors in derived classes
 
Multi level hierarchy
Multi level hierarchyMulti level hierarchy
Multi level hierarchy
 
Introduction to object oriented programming
Introduction to object oriented programmingIntroduction to object oriented programming
Introduction to object oriented programming
 
This pointer .17
This pointer .17This pointer .17
This pointer .17
 
Inline Functions and Default arguments
Inline Functions and Default argumentsInline Functions and Default arguments
Inline Functions and Default arguments
 
2CPP06 - Arrays and Pointers
2CPP06 - Arrays and Pointers2CPP06 - Arrays and Pointers
2CPP06 - Arrays and Pointers
 
Classes and Nested Classes in Java
Classes and Nested Classes in JavaClasses and Nested Classes in Java
Classes and Nested Classes in Java
 
Console Io Operations
Console Io OperationsConsole Io Operations
Console Io Operations
 
Data members and member functions
Data members and member functionsData members and member functions
Data members and member functions
 
Object as function argument , friend and static function by shahzad younas
Object as function argument , friend and static function by shahzad younasObject as function argument , friend and static function by shahzad younas
Object as function argument , friend and static function by shahzad younas
 

Ähnlich wie Friend functions

Lecture 5 Inheritance
Lecture 5 InheritanceLecture 5 Inheritance
Lecture 5 Inheritancebunnykhan
 
Implementation of oop concept in c++
Implementation of oop concept in c++Implementation of oop concept in c++
Implementation of oop concept in c++Swarup Boro
 
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
 
Unit vi(dsc++)
Unit vi(dsc++)Unit vi(dsc++)
Unit vi(dsc++)Durga Devi
 
.NET F# Inheritance and operator overloading
.NET F# Inheritance and operator overloading.NET F# Inheritance and operator overloading
.NET F# Inheritance and operator overloadingDrRajeshreeKhande
 
A COMPLETE FILE FOR C++
A COMPLETE FILE FOR C++A COMPLETE FILE FOR C++
A COMPLETE FILE FOR C++M Hussnain Ali
 
Keyword of java
Keyword of javaKeyword of java
Keyword of javaJani Harsh
 
3 functions and class
3   functions and class3   functions and class
3 functions and classtrixiacruz
 
Classes, objects and methods
Classes, objects and methodsClasses, objects and methods
Classes, objects and methodsfarhan amjad
 
Inheritance, polymorphisam, abstract classes and composition)
Inheritance, polymorphisam, abstract classes and composition)Inheritance, polymorphisam, abstract classes and composition)
Inheritance, polymorphisam, abstract classes and composition)farhan amjad
 
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
 

Ähnlich wie Friend functions (20)

Lecture 5 Inheritance
Lecture 5 InheritanceLecture 5 Inheritance
Lecture 5 Inheritance
 
ccc
cccccc
ccc
 
class c++
class c++class c++
class c++
 
Implementation of oop concept in c++
Implementation of oop concept in c++Implementation of oop concept in c++
Implementation of oop concept in c++
 
Java Basic day-2
Java Basic day-2Java Basic day-2
Java Basic day-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
 
DS Unit 6.ppt
DS Unit 6.pptDS Unit 6.ppt
DS Unit 6.ppt
 
Unit vi(dsc++)
Unit vi(dsc++)Unit vi(dsc++)
Unit vi(dsc++)
 
Inheritance
InheritanceInheritance
Inheritance
 
.NET F# Inheritance and operator overloading
.NET F# Inheritance and operator overloading.NET F# Inheritance and operator overloading
.NET F# Inheritance and operator overloading
 
A COMPLETE FILE FOR C++
A COMPLETE FILE FOR C++A COMPLETE FILE FOR C++
A COMPLETE FILE FOR C++
 
OOPs & C++ UNIT 3
OOPs & C++ UNIT 3OOPs & C++ UNIT 3
OOPs & C++ UNIT 3
 
Keyword of java
Keyword of javaKeyword of java
Keyword of java
 
3 functions and class
3   functions and class3   functions and class
3 functions and class
 
Functions in C++
Functions in C++Functions in C++
Functions in C++
 
Lecture 2 (1)
Lecture 2 (1)Lecture 2 (1)
Lecture 2 (1)
 
Class and object in C++ By Pawan Thakur
Class and object in C++ By Pawan ThakurClass and object in C++ By Pawan Thakur
Class and object in C++ By Pawan Thakur
 
Classes, objects and methods
Classes, objects and methodsClasses, objects and methods
Classes, objects and methods
 
Inheritance, polymorphisam, abstract classes and composition)
Inheritance, polymorphisam, abstract classes and composition)Inheritance, polymorphisam, abstract classes and composition)
Inheritance, polymorphisam, abstract classes and composition)
 
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.
 

Kürzlich hochgeladen

Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...Shubhangi Sonawane
 
psychiatric nursing HISTORY COLLECTION .docx
psychiatric  nursing HISTORY  COLLECTION  .docxpsychiatric  nursing HISTORY  COLLECTION  .docx
psychiatric nursing HISTORY COLLECTION .docxPoojaSen20
 
Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibitjbellavia9
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhikauryashika82
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxDenish Jangid
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdfQucHHunhnh
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingTechSoup
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxheathfieldcps1
 
Unit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxUnit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxVishalSingh1417
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxRamakrishna Reddy Bijjam
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17Celine George
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfagholdier
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.pptRamjanShidvankar
 
ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701bronxfugly43
 
Role Of Transgenic Animal In Target Validation-1.pptx
Role Of Transgenic Animal In Target Validation-1.pptxRole Of Transgenic Animal In Target Validation-1.pptx
Role Of Transgenic Animal In Target Validation-1.pptxNikitaBankoti2
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsTechSoup
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfciinovamais
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsMebane Rash
 
Seal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxSeal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxnegromaestrong
 

Kürzlich hochgeladen (20)

Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
 
psychiatric nursing HISTORY COLLECTION .docx
psychiatric  nursing HISTORY  COLLECTION  .docxpsychiatric  nursing HISTORY  COLLECTION  .docx
psychiatric nursing HISTORY COLLECTION .docx
 
Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibit
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy Consulting
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
 
Unit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxUnit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptx
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docx
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.ppt
 
ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701
 
Role Of Transgenic Animal In Target Validation-1.pptx
Role Of Transgenic Animal In Target Validation-1.pptxRole Of Transgenic Animal In Target Validation-1.pptx
Role Of Transgenic Animal In Target Validation-1.pptx
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The Basics
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
 
Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan Fellows
 
Seal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxSeal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptx
 

Friend functions

  • 1. h
  • 2.  By default : functions and data of a class are private to that class  Only the public members are accessible outside the class  Protected members can be inherited along with public members  No such condition where private members can be accessed from outside the class  So Friend Functions and Friend Classes may be used where our application requires the access all the members of a class
  • 3.  Not a member of the class  Invoked like normal function without any object  Full access to private and protected members Of the class  But can use the members for one or more specific objects  Called without the use dot operator(does not need to be qualified with object’s name)
  • 4.  Include its prototype in the class , preceding it with keyword friend Syntax: friend ret_type func_name(arguments);  Can be declared anywhere (in public, protected or private section) in the class  May have no arguments  Objects of the class or their pointers can be passed as arguments to the friend function
  • 5. Class myclass { int a,b; Public: myclass(int x,int y) { a=x; b=y; } friend int sum(myclass m); // declaration } int sum(myclass m) { return m.a+ m.b; } void main() { myclass my(10,20); cout<<sum(my); //calling the friend function }
  • 6.  As a function can be friend of more than one class, it can be used for message passing between the classes.  As it is not a member of the class ,it does not have a this pointer. So can be used for Operator overloading. The operands are passed explicitly to the overloaded friend operator function.  Make I/O functions easier
  • 7. class A; // forward declaration class B { int b; friend int sum(A a1, B b1); }; class A { int a; friend int sum(A a1, B b1); }; int sum (A a1,B b1) { return a1.a + b1.b; }
  • 8. Class overload { int i,j; public: overload(int a,int b) { i=a; j=b; } void disp() { cout<< i<<“ “<<j; } friend overload operator +(int ,overload ); }; //overloading binary operator
  • 9. overload operator+(int a, overload obj) { overload obj1; obj1.i= a + obj.i; obj1.j= a + obj.j; return obj1; } main() { overload ov(40,76) ; overload o =10+ov; o.disp(); //output: 50 86 }
  • 10. Class one { int x; void func(two & ); }; Class two { int y; friend void one:: func(two & ); }; void one:: func(two & t) { t.y=this->x; //called with an object of “one” }
  • 11.  One class friend of another class Syntax: friend class class_name;  The friend class and all of its member functions have access to the private members defined within the other class  Provides additional functionality from outside the class
  • 12. Class one { int a; friend class two; }; Class two { void disp(one o1) cout<<o1.a; }; main() { two t; one o; t.disp(o); }
  • 13.  Friend functions and classes are not inherited  Friend function cannot have storage-class specifier i.e. cannot be declared as static or extern  Friend classes are not corresponded i.e. if class A is a friend of B it does not imply that class B is a friend of A  Friend classes are not transitive: i.e. friend of a friend is not considered to be a friend unless explicitly specified