KEMBAR78
Friend Function | PDF | Games & Activities | Computers
0% found this document useful (0 votes)
37 views7 pages

Friend Function

A friend function allows non-member functions to access private and protected members of a class. A friend function is declared inside the class using the friend keyword. While it is not a member of the class, it has private access and can access private data as if it were a member. It is defined and used like a regular function, not as a class member.

Uploaded by

Soumitra
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPSX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
37 views7 pages

Friend Function

A friend function allows non-member functions to access private and protected members of a class. A friend function is declared inside the class using the friend keyword. While it is not a member of the class, it has private access and can access private data as if it were a member. It is defined and used like a regular function, not as a class member.

Uploaded by

Soumitra
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPSX, PDF, TXT or read online on Scribd
You are on page 1/ 7

Friend Function

Friend Function
Any data which is declared private inside a class is
not accessible from outside the class.
A non-member function cannot have an access to
the private data of a class.
C++ support a special function to access private
data of class called friend function or friendly
fun.

2
Friend Function
A friend function is used to access private data in
a class from outside the class.
A friend function is not a member of a class but
has access to the class's private members.
To make a fun friend, the fun declaration should
be preceded by the keyword friend

3
Friend Function
Class s4
{
….
….
public:
friend void show();// declaration
};
Void show() // definition
{
…..
}
4
Friend Function
Friend fun def does not need the keyword and
scope resolution opr (::)
Friend fun are not in the scope of any class, so
they cannot invoked using class object.
They invoked like an ordinary function in c
language
A friend can be declared in any number of classes.

5
Friend Function
• class myclass {
• int a, b;
• public:
• friend int sum(myclass x);
• void set_ab(int i, int j);
• };
• void myclass::set_ab(int i, int j)
• { a = I; b = j; }
• int sum(myclass x)
• { return x.a + x.b; }
• int main()
• {
• myclass n;
• n.set_ab(3, 4);
• cout << sum(n);
• }

6
Friend Function
Characteristics
• The keyword friend is placed only in the function declaration of
the friend function and not in the function definition.
• It is possible to declare a function as friend in any number of
classes.
• When a class is declared as a friend, the friend class has access to
the private data of the class that made this a friend.
• A friend function, even though it is not a member function,
would have the rights to access the private members of the class.
• It is possible to declare the friend function as either private or
public.
• The function cannot be invoked with class object.
• The friend function has its argument as objects.

You might also like