SlideShare uma empresa Scribd logo
1 de 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

Mais conteúdo relacionado

Mais procurados

Mais procurados (20)

Constructors and Destructor in C++
Constructors and Destructor in C++Constructors and Destructor in C++
Constructors and Destructor 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
 
Type casting
Type castingType casting
Type casting
 
Pure virtual function and abstract class
Pure virtual function and abstract classPure virtual function and abstract class
Pure virtual function and abstract class
 
Strings in c++
Strings in c++Strings in c++
Strings in c++
 
Friend Function
Friend FunctionFriend Function
Friend Function
 
Introduction to class in java
Introduction to class in javaIntroduction to class in java
Introduction to class in java
 
Function overloading
Function overloadingFunction overloading
Function overloading
 
Java input
Java inputJava input
Java input
 
Introduction to method overloading &amp; method overriding in java hdm
Introduction to method overloading &amp; method overriding  in java  hdmIntroduction to method overloading &amp; method overriding  in java  hdm
Introduction to method overloading &amp; method overriding in java hdm
 
Function overloading and overriding
Function overloading and overridingFunction overloading and overriding
Function overloading and overriding
 
Interface in java
Interface in javaInterface in java
Interface in java
 
07. Virtual Functions
07. Virtual Functions07. Virtual Functions
07. Virtual Functions
 
Command line-arguments-in-java-tutorial
Command line-arguments-in-java-tutorialCommand line-arguments-in-java-tutorial
Command line-arguments-in-java-tutorial
 
Stream classes in C++
Stream classes in C++Stream classes in C++
Stream classes in C++
 
Function overloading ppt
Function overloading pptFunction overloading ppt
Function overloading ppt
 
Access specifier
Access specifierAccess specifier
Access specifier
 
Constructor and destructor in oop
Constructor and destructor in oop Constructor and destructor in oop
Constructor and destructor in oop
 
Function C programming
Function C programmingFunction C programming
Function C programming
 
This pointer
This pointerThis pointer
This pointer
 

Destaque

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
 

Destaque (20)

Friend function & friend class
Friend function & friend classFriend function & friend class
Friend function & friend class
 
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
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
 
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
 

Semelhante a 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
 

Semelhante a 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.
 

Último

The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
heathfieldcps1
 
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
 

Último (20)

COMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptxCOMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
 
Graduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - EnglishGraduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - English
 
Interdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptxInterdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptx
 
REMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptxREMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptx
 
SOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning PresentationSOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning Presentation
 
Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibit
 
Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdf
 
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...
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.ppt
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.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
 
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
 
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Ă...
 
FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptx
 
General Principles of Intellectual Property: Concepts of Intellectual Proper...
General Principles of Intellectual Property: Concepts of Intellectual  Proper...General Principles of Intellectual Property: Concepts of Intellectual  Proper...
General Principles of Intellectual Property: Concepts of Intellectual Proper...
 
Google Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptxGoogle Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptx
 
Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)
 
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_...
 

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