Friend functions
Access privileges in C++.
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 sub-
classes. 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.
Access specifier
Visible to own
class members
Visible to
objects of
same/other
class
public Yes Yes
private Yes No
protected Yes No
 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.
• 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.
Syntax:
class ABC
{
………….
public:
friend void xyz(object of class);
};
Friend function characterstics
 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
class demo
{
int x;
public:
demo(int a)
{
x=a;
}
friend void display(demo);
};
void display(demo d1)
{
cout<<d1.x;
}
void main()
{
demo d2(5);
display(d2);
}
 class sample
{
int a;
int b;
public:
void setval(){ a=25,b=40}
friend float mean(sample s);
};
float mean(sample s)
{ return (s.a+s.b)/2.0;
}
void main()
{ sample X;
cout<<mean(X);
}
Friend class
 In previous section of class we declared only one function as a
friend of another class.but it is possible that all member of the
one class can be friend of another class.this is friend class
Friends (a few gory details)
 Friendship is not inherited, transitive, or reciprocal.
 Derived classes don’t receive the privileges of friendship (more
on this when we get to inheritance in a few classes).
 The privileges of friendship aren’t transitive. If class A declares
class B as a friend, and class B declares class C as a friend, class
C doesn’t necessarily have any special access rights to class A.
 If class A declares class B as a friend (so class B can see class A’s
private members), class A is not automatically a friend of class
B (so class A cannot necessarily see the private data members of
class B).
 Friendship is not inherited, transitive, or reciprocal.
Example
class demo
{
private:
int x,y;
public:
demo(int a,int b)
{
x=a;
y=b;
}
friend class frnd;
};
class frnd
{
public:
void display(demo d1)
{
cout<<“x is=”d1.x;
cout<<“y is=”d1.y;
}
};
void main()
{
demo d2(10,40);
frnd f1;
f1.display(d2);
getch();
}
This pointer
Every object in C++ has access to its own address
through an important pointer called this pointer. The
this pointer is an implicit parameter to all member
functions. Therefore, inside a member function, this
may be used to refer to the invoking object.
Friend functions do not have a this pointer, because
friends are not members of a class. Only member
functions have a this pointer.

More Related Content

PPT
friend function(c++)
PPTX
cse l 5.pptx
PPTX
friend class & friend function by Komal V Rokade.pptx
PPTX
Friend function
PPTX
OOP Week 3 Lecture 2.pptx
PPT
4 Classes & Objects
PPTX
Inheritance, friend function, virtual function, polymorphism
PPT
Classes and objects
friend function(c++)
cse l 5.pptx
friend class & friend function by Komal V Rokade.pptx
Friend function
OOP Week 3 Lecture 2.pptx
4 Classes & Objects
Inheritance, friend function, virtual function, polymorphism
Classes and objects

Similar to 12-170211073924.ppthihello heoobchdhdhdhhd (20)

PPT
Set Theory In C++
ODP
Class&objects
PPT
classes data type for Btech students.ppt
PPT
inheritance
PPT
Inheritance, Object Oriented Programming
PPT
11 Inheritance.ppt
PPTX
Friend Function
PDF
Chapter18 class-and-objects
PPT
Inheritance in C++
PPT
cpp class unitdfdsfasadfsdASsASass 4.ppt
PPT
Inheritance
PPTX
Friend function & friend class
PPTX
00ps inheritace using c++
PPT
Class and object in C++
PPTX
OOPs & C++ UNIT 3
PPTX
Class and object
PPTX
L9_Friend Function.pptxyeyeyeyeyyeyeyeye
PPT
Classes in C++ computer language presentation.ppt
PPTX
Constructor and destructor in OOPS with cpp language
Set Theory In C++
Class&objects
classes data type for Btech students.ppt
inheritance
Inheritance, Object Oriented Programming
11 Inheritance.ppt
Friend Function
Chapter18 class-and-objects
Inheritance in C++
cpp class unitdfdsfasadfsdASsASass 4.ppt
Inheritance
Friend function & friend class
00ps inheritace using c++
Class and object in C++
OOPs & C++ UNIT 3
Class and object
L9_Friend Function.pptxyeyeyeyeyyeyeyeye
Classes in C++ computer language presentation.ppt
Constructor and destructor in OOPS with cpp language
Ad

More from SanskritiGupta39 (8)

PPTX
OOPC_Final_U_I.pptx rrrrrrrrrrrrrrrrrrrrrr
PPTX
effective_communication.pptxrrrrrrrrrrrrrrrrrrrrr
PPTX
inheritance_OOPC_datastream.ppttttttttttttttx
PPTX
eseee.pptxhiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiii
PPT
file_handling_in_c.pptbbbbbbbbbbbbbbbbbbbbb
PPTX
ESE project.pptxhi hhji j
PPTX
424570636-friend-function-1-pptx.hiipptx
PPT
OOP-2009 - 12 - FriendClass.ppt it helps the user
OOPC_Final_U_I.pptx rrrrrrrrrrrrrrrrrrrrrr
effective_communication.pptxrrrrrrrrrrrrrrrrrrrrr
inheritance_OOPC_datastream.ppttttttttttttttx
eseee.pptxhiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiii
file_handling_in_c.pptbbbbbbbbbbbbbbbbbbbbb
ESE project.pptxhi hhji j
424570636-friend-function-1-pptx.hiipptx
OOP-2009 - 12 - FriendClass.ppt it helps the user
Ad

Recently uploaded (20)

PDF
Caterpillar Cat 330B L Excavator (Prefix 4RS) Service Repair Manual Instant D...
PDF
CASE CX45B Series 2 Mini Excavator Service Repair Manual Instant Download.pdf
PDF
BOBCAT 442 Excavator Service Repair Manual.pdf
PPTX
ADULT Nutrition.pptxxxxxxxxxxxxxxxxxxxxxx
PPT
industrialsafetyhazards-180118095222.ppt
PDF
Audi A3 20 TFSi Timing Belt Installation
PDF
John Deere 5055e Lubrication System Repair Manual Tm900319.pdf
PDF
Coolant pump Volvo EC160BLC Excavator Service Repair Manual.pdf
PDF
Designyze – Creative Digital Marketing & Design Agency in India
PPTX
Found naturally in fruits, vegetables, n
PDF
dozer bulldozer Komatsu d85 px 15e0 Technical manual.pdf
PPTX
Total quality management and I have a great day
PPTX
EV Charging Network Requirement for Pakisan
PDF
John Deere 410E Articulated Dump Trucks Diagnostic Manuals.pdf
PDF
Volvo Dump Truck A40E Service Repair Manual.pdf
PPTX
ENERGY RESOUCES_Class 10_Geo_2020-21.pptx
DOC
Bài tập bổ trợ I-learn Smart World 9 HK1 Unit 4 (HS) (1).doc
PDF
Deutz D6D EBE2 Volvo EC160BLC Excavator Service Repair Manual.pdf
PDF
Volvo EC160B LC EC160BLC Excavator Service Repair Manual Instant Download.pdf
PPT
157_37315_EA221_2013_1__2_1_Chapter 1, introduction to OR.ppt
Caterpillar Cat 330B L Excavator (Prefix 4RS) Service Repair Manual Instant D...
CASE CX45B Series 2 Mini Excavator Service Repair Manual Instant Download.pdf
BOBCAT 442 Excavator Service Repair Manual.pdf
ADULT Nutrition.pptxxxxxxxxxxxxxxxxxxxxxx
industrialsafetyhazards-180118095222.ppt
Audi A3 20 TFSi Timing Belt Installation
John Deere 5055e Lubrication System Repair Manual Tm900319.pdf
Coolant pump Volvo EC160BLC Excavator Service Repair Manual.pdf
Designyze – Creative Digital Marketing & Design Agency in India
Found naturally in fruits, vegetables, n
dozer bulldozer Komatsu d85 px 15e0 Technical manual.pdf
Total quality management and I have a great day
EV Charging Network Requirement for Pakisan
John Deere 410E Articulated Dump Trucks Diagnostic Manuals.pdf
Volvo Dump Truck A40E Service Repair Manual.pdf
ENERGY RESOUCES_Class 10_Geo_2020-21.pptx
Bài tập bổ trợ I-learn Smart World 9 HK1 Unit 4 (HS) (1).doc
Deutz D6D EBE2 Volvo EC160BLC Excavator Service Repair Manual.pdf
Volvo EC160B LC EC160BLC Excavator Service Repair Manual Instant Download.pdf
157_37315_EA221_2013_1__2_1_Chapter 1, introduction to OR.ppt

12-170211073924.ppthihello heoobchdhdhdhhd

  • 2. Access privileges in C++. You have access privileges in C++ such as public, protected and private that helps in encapsulation of data at various level.
  • 3. 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.
  • 4. Protected Protected members are accessible by the class itself and it's sub- classes. 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.
  • 5. Access specifier Visible to own class members Visible to objects of same/other class public Yes Yes private Yes No protected Yes No
  • 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.
  • 7. • 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.
  • 9. Friend function characterstics  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.
  • 10. Example class demo { int x; public: demo(int a) { x=a; } friend void display(demo); };
  • 11. void display(demo d1) { cout<<d1.x; } void main() { demo d2(5); display(d2); }
  • 12.  class sample { int a; int b; public: void setval(){ a=25,b=40} friend float mean(sample s); }; float mean(sample s) { return (s.a+s.b)/2.0; } void main() { sample X; cout<<mean(X); }
  • 13. Friend class  In previous section of class we declared only one function as a friend of another class.but it is possible that all member of the one class can be friend of another class.this is friend class
  • 14. Friends (a few gory details)  Friendship is not inherited, transitive, or reciprocal.  Derived classes don’t receive the privileges of friendship (more on this when we get to inheritance in a few classes).  The privileges of friendship aren’t transitive. If class A declares class B as a friend, and class B declares class C as a friend, class C doesn’t necessarily have any special access rights to class A.  If class A declares class B as a friend (so class B can see class A’s private members), class A is not automatically a friend of class B (so class A cannot necessarily see the private data members of class B).  Friendship is not inherited, transitive, or reciprocal.
  • 15. Example class demo { private: int x,y; public: demo(int a,int b) { x=a; y=b; } friend class frnd; };
  • 16. class frnd { public: void display(demo d1) { cout<<“x is=”d1.x; cout<<“y is=”d1.y; } };
  • 17. void main() { demo d2(10,40); frnd f1; f1.display(d2); getch(); }
  • 18. This pointer Every object in C++ has access to its own address through an important pointer called this pointer. The this pointer is an implicit parameter to all member functions. Therefore, inside a member function, this may be used to refer to the invoking object. Friend functions do not have a this pointer, because friends are not members of a class. Only member functions have a this pointer.