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

08 c++ Operator Overloading.ppt
08 c++ Operator Overloading.ppt08 c++ Operator Overloading.ppt
08 c++ Operator Overloading.ppt
Tareq Hasan
 
class and objects
class and objectsclass and objects
class and objects
Payel Guria
 
Templates in C++
Templates in C++Templates in C++
Templates in C++
Tech_MX
 
Friends function and_classes
Friends function and_classesFriends function and_classes
Friends function and_classes
asadsardar
 

Mais procurados (20)

Operator overloading
Operator overloadingOperator overloading
Operator overloading
 
08 c++ Operator Overloading.ppt
08 c++ Operator Overloading.ppt08 c++ Operator Overloading.ppt
08 c++ Operator Overloading.ppt
 
class and objects
class and objectsclass and objects
class and objects
 
Inline function in C++
Inline function in C++Inline function in C++
Inline function in C++
 
Inheritance in c++
Inheritance in c++Inheritance in c++
Inheritance in c++
 
Encapsulation C++
Encapsulation C++Encapsulation C++
Encapsulation C++
 
Templates in C++
Templates in C++Templates in C++
Templates in C++
 
Function C programming
Function C programmingFunction C programming
Function C programming
 
inheritance c++
inheritance c++inheritance c++
inheritance c++
 
Unary operator overloading
Unary operator overloadingUnary operator overloading
Unary operator overloading
 
Inline Functions and Default arguments
Inline Functions and Default argumentsInline Functions and Default arguments
Inline Functions and Default arguments
 
Functions in c
Functions in cFunctions in c
Functions in c
 
Abstract class in c++
Abstract class in c++Abstract class in c++
Abstract class in c++
 
Pure virtual function and abstract class
Pure virtual function and abstract classPure virtual function and abstract class
Pure virtual function and abstract class
 
Inheritance in c++
Inheritance in c++Inheritance in c++
Inheritance in c++
 
Polymorphism in C++
Polymorphism in C++Polymorphism in C++
Polymorphism in C++
 
Friends function and_classes
Friends function and_classesFriends function and_classes
Friends function and_classes
 
Class and object in C++
Class and object in C++Class and object in C++
Class and object in C++
 
FUNCTIONS IN c++ PPT
FUNCTIONS IN c++ PPTFUNCTIONS IN c++ PPT
FUNCTIONS IN c++ PPT
 
friend function(c++)
friend function(c++)friend function(c++)
friend function(c++)
 

Destaque

Chapter23 friend-function-friend-class
Chapter23 friend-function-friend-classChapter23 friend-function-friend-class
Chapter23 friend-function-friend-class
Deepak Singh
 
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
 
Object oriented methodology & unified modeling language
Object oriented methodology & unified modeling languageObject oriented methodology & unified modeling language
Object oriented methodology & unified modeling language
Ismail El Gayar
 

Destaque (20)

Friend function & friend class
Friend function & friend classFriend function & friend class
Friend function & friend class
 
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
 
[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
 
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
 
Some Basic Concepts of Object Oriented Methodology
Some Basic Concepts of Object Oriented MethodologySome Basic Concepts of Object Oriented Methodology
Some Basic Concepts of Object Oriented Methodology
 
Object oriented methodology & unified modeling language
Object oriented methodology & unified modeling languageObject oriented methodology & unified modeling language
Object oriented methodology & unified modeling language
 

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

Último (20)

Matatag-Curriculum and the 21st Century Skills Presentation.pptx
Matatag-Curriculum and the 21st Century Skills Presentation.pptxMatatag-Curriculum and the 21st Century Skills Presentation.pptx
Matatag-Curriculum and the 21st Century Skills Presentation.pptx
 
Operations Management - Book1.p - Dr. Abdulfatah A. Salem
Operations Management - Book1.p  - Dr. Abdulfatah A. SalemOperations Management - Book1.p  - Dr. Abdulfatah A. Salem
Operations Management - Book1.p - Dr. Abdulfatah A. Salem
 
Morse OER Some Benefits and Challenges.pptx
Morse OER Some Benefits and Challenges.pptxMorse OER Some Benefits and Challenges.pptx
Morse OER Some Benefits and Challenges.pptx
 
The basics of sentences session 4pptx.pptx
The basics of sentences session 4pptx.pptxThe basics of sentences session 4pptx.pptx
The basics of sentences session 4pptx.pptx
 
Introduction to Quality Improvement Essentials
Introduction to Quality Improvement EssentialsIntroduction to Quality Improvement Essentials
Introduction to Quality Improvement Essentials
 
Incoming and Outgoing Shipments in 2 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 2 STEPS Using Odoo 17Incoming and Outgoing Shipments in 2 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 2 STEPS Using Odoo 17
 
slides CapTechTalks Webinar May 2024 Alexander Perry.pptx
slides CapTechTalks Webinar May 2024 Alexander Perry.pptxslides CapTechTalks Webinar May 2024 Alexander Perry.pptx
slides CapTechTalks Webinar May 2024 Alexander Perry.pptx
 
An Overview of the Odoo 17 Discuss App.pptx
An Overview of the Odoo 17 Discuss App.pptxAn Overview of the Odoo 17 Discuss App.pptx
An Overview of the Odoo 17 Discuss App.pptx
 
The impact of social media on mental health and well-being has been a topic o...
The impact of social media on mental health and well-being has been a topic o...The impact of social media on mental health and well-being has been a topic o...
The impact of social media on mental health and well-being has been a topic o...
 
Benefits and Challenges of Using Open Educational Resources
Benefits and Challenges of Using Open Educational ResourcesBenefits and Challenges of Using Open Educational Resources
Benefits and Challenges of Using Open Educational Resources
 
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
 
Sectors of the Indian Economy - Class 10 Study Notes pdf
Sectors of the Indian Economy - Class 10 Study Notes pdfSectors of the Indian Economy - Class 10 Study Notes pdf
Sectors of the Indian Economy - Class 10 Study Notes pdf
 
Salient features of Environment protection Act 1986.pptx
Salient features of Environment protection Act 1986.pptxSalient features of Environment protection Act 1986.pptx
Salient features of Environment protection Act 1986.pptx
 
Advances in production technology of Grapes.pdf
Advances in production technology of Grapes.pdfAdvances in production technology of Grapes.pdf
Advances in production technology of Grapes.pdf
 
Danh sách HSG Bộ môn cấp trường - Cấp THPT.pdf
Danh sách HSG Bộ môn cấp trường - Cấp THPT.pdfDanh sách HSG Bộ môn cấp trường - Cấp THPT.pdf
Danh sách HSG Bộ môn cấp trường - Cấp THPT.pdf
 
GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...
GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...
GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...
 
Pragya Champions Chalice 2024 Prelims & Finals Q/A set, General Quiz
Pragya Champions Chalice 2024 Prelims & Finals Q/A set, General QuizPragya Champions Chalice 2024 Prelims & Finals Q/A set, General Quiz
Pragya Champions Chalice 2024 Prelims & Finals Q/A set, General Quiz
 
The Last Leaf, a short story by O. Henry
The Last Leaf, a short story by O. HenryThe Last Leaf, a short story by O. Henry
The Last Leaf, a short story by O. Henry
 
How to the fix Attribute Error in odoo 17
How to the fix Attribute Error in odoo 17How to the fix Attribute Error in odoo 17
How to the fix Attribute Error in odoo 17
 
Instructions for Submissions thorugh G- Classroom.pptx
Instructions for Submissions thorugh G- Classroom.pptxInstructions for Submissions thorugh G- Classroom.pptx
Instructions for Submissions thorugh G- Classroom.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