SlideShare une entreprise Scribd logo
1  sur  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

Contenu connexe

Tendances

Inheritance
InheritanceInheritance
Inheritance
Tech_MX
 

Tendances (20)

Classes and objects
Classes and objectsClasses and objects
Classes and objects
 
Object Oriented Programming Using C++
Object Oriented Programming Using C++Object Oriented Programming Using C++
Object Oriented Programming Using C++
 
Constructor and Types of Constructors
Constructor and Types of ConstructorsConstructor and Types of Constructors
Constructor and Types of Constructors
 
Pointers,virtual functions and polymorphism cpp
Pointers,virtual functions and polymorphism cppPointers,virtual functions and polymorphism cpp
Pointers,virtual functions and polymorphism cpp
 
This pointer
This pointerThis pointer
This pointer
 
Basic Concepts of OOPs (Object Oriented Programming in Java)
Basic Concepts of OOPs (Object Oriented Programming in Java)Basic Concepts of OOPs (Object Oriented Programming in Java)
Basic Concepts of OOPs (Object Oriented Programming in Java)
 
Friend function
Friend functionFriend function
Friend function
 
Class and object in C++
Class and object in C++Class and object in C++
Class and object in C++
 
Static Data Members and Member Functions
Static Data Members and Member FunctionsStatic Data Members and Member Functions
Static Data Members and Member Functions
 
Function overloading(c++)
Function overloading(c++)Function overloading(c++)
Function overloading(c++)
 
Constructor in java
Constructor in javaConstructor in java
Constructor in java
 
Polymorphism in C++
Polymorphism in C++Polymorphism in C++
Polymorphism in C++
 
Inheritance
InheritanceInheritance
Inheritance
 
Operators and expressions in C++
Operators and expressions in C++Operators and expressions in C++
Operators and expressions in C++
 
Operators in C++
Operators in C++Operators in C++
Operators in C++
 
Inline Functions and Default arguments
Inline Functions and Default argumentsInline Functions and Default arguments
Inline Functions and Default arguments
 
Interface in java
Interface in javaInterface in java
Interface in java
 
Inheritance in c++
Inheritance in c++Inheritance in c++
Inheritance in c++
 
[OOP - Lec 07] Access Specifiers
[OOP - Lec 07] Access Specifiers[OOP - Lec 07] Access Specifiers
[OOP - Lec 07] Access Specifiers
 
Constructor and destructor in oop
Constructor and destructor in oop Constructor and destructor in oop
Constructor and destructor in oop
 

En vedette

Friends function and_classes
Friends function and_classesFriends function and_classes
Friends function and_classes
asadsardar
 
Chapter23 friend-function-friend-class
Chapter23 friend-function-friend-classChapter23 friend-function-friend-class
Chapter23 friend-function-friend-class
Deepak Singh
 
Inline function
Inline functionInline function
Inline function
Tech_MX
 
Oop c++class(final).ppt
Oop c++class(final).pptOop c++class(final).ppt
Oop c++class(final).ppt
Alok Kumar
 
Friends!!!!
Friends!!!!Friends!!!!
Friends!!!!
merua7
 
Multi level hierarchy
Multi level hierarchyMulti level hierarchy
Multi level hierarchy
myrajendra
 
Introduction to object oriented programming
Introduction to object oriented programmingIntroduction to object oriented programming
Introduction to object oriented programming
Abzetdin Adamov
 
This pointer .17
This pointer .17This pointer .17
This pointer .17
myrajendra
 
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
 

En vedette (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
 
Inline function
Inline functionInline function
Inline function
 
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
 
07. Virtual Functions
07. Virtual Functions07. Virtual Functions
07. Virtual Functions
 
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
 
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
 

Similaire à Friend functions

Lecture 5 Inheritance
Lecture 5 InheritanceLecture 5 Inheritance
Lecture 5 Inheritance
bunnykhan
 
A COMPLETE FILE FOR C++
A COMPLETE FILE FOR C++A COMPLETE FILE FOR C++
A COMPLETE FILE FOR C++
M Hussnain Ali
 
3 functions and class
3   functions and class3   functions and class
3 functions and class
trixiacruz
 
Classes, objects and methods
Classes, objects and methodsClasses, objects and methods
Classes, objects and methods
farhan amjad
 

Similaire à 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
 
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
 
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)
 

Dernier

Salient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsSalient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functions
KarakKing
 

Dernier (20)

Plant propagation: Sexual and Asexual propapagation.pptx
Plant propagation: Sexual and Asexual propapagation.pptxPlant propagation: Sexual and Asexual propapagation.pptx
Plant propagation: Sexual and Asexual propapagation.pptx
 
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptx
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptxExploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptx
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptx
 
Salient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsSalient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functions
 
Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibit
 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and Modifications
 
How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17
 
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
 
Philosophy of china and it's charactistics
Philosophy of china and it's charactisticsPhilosophy of china and it's charactistics
Philosophy of china and it's charactistics
 
Single or Multiple melodic lines structure
Single or Multiple melodic lines structureSingle or Multiple melodic lines structure
Single or Multiple melodic lines structure
 
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
 
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
 
Fostering Friendships - Enhancing Social Bonds in the Classroom
Fostering Friendships - Enhancing Social Bonds  in the ClassroomFostering Friendships - Enhancing Social Bonds  in the Classroom
Fostering Friendships - Enhancing Social Bonds in the Classroom
 
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
 
How to Add New Custom Addons Path in Odoo 17
How to Add New Custom Addons Path in Odoo 17How to Add New Custom Addons Path in Odoo 17
How to Add New Custom Addons Path in Odoo 17
 
Graduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - EnglishGraduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - English
 
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
 
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
 
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptxHMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
 
Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)
 
21st_Century_Skills_Framework_Final_Presentation_2.pptx
21st_Century_Skills_Framework_Final_Presentation_2.pptx21st_Century_Skills_Framework_Final_Presentation_2.pptx
21st_Century_Skills_Framework_Final_Presentation_2.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