0% found this document useful (0 votes)
23 views12 pages

Friend Function and Friend Class in C++

Uploaded by

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

Friend Function and Friend Class in C++

Uploaded by

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

Friend Function and Friend Class

in C++
Friend Function
• Generally, non-member functions
– Cannot access the private members of a particular class

• A friend function is
– A function that is declared outside a class but is capable of
accessing the private and protected members of the class
– May be
• A global function
• A member of another class
When we need a Friend Function?
– There could be situations in programming wherein
• Two classes may want to share their members which may be
data members, member functions or function templates

• In such cases, a function is defined which is a friend to both


these classes which will allow accessing public, private and
protected members of the class
Declaration of a Friend Function in C++
– The friend function is declared
• Using the keyword friend inside the body of the class
• Syntax-
class className
{
... .. ...
friend returnType functionName (arguments);
... .. ...
}
• By using the keyword, the friend
– The compiler understands that the function is a friend
function of the class
Characteristics of a Friend function
• The function is not in the scope of the class
– To which it has been declared as a friend
• It can be declared
– Either in the private or the public part
• It cannot be called using the object as
– It is not in the scope of that class
– It can be invoked like a normal function without using the object
• It cannot access the members of the class directly
– It has to use an object name and dot membership operator with
the member name
Implementing Friend function
• Friend Functions can be implemented in two ways:
– As a Global function
– As a member function of another class
Implementing Friend functions
– As a Global function (Accessing the members of one class)
#include <iostream>
int sum(Calculator c)
using namespace std; {
return (c.x + c.y);
class Calculator }
{
private: int main()
{
int x, y; Calculator c;
public: cout << "\nSum of x & y = “ << sum(c) << endl;
Calculator() return 0;
{ }
x = 5;
y = 10;
} Output:
friend int sum(Calculator); //friend function
}; Sum of x & y = 15
Implementing Friend functions
– As a Global function (Accessing the members of more than one class)
using namespace std;
class B; int sum(A a1, B b1)
{
class A return (a1.x + b1.y);
{ }
private:
int x; int main()
public: {
A() A a;
{ B b;
x = 5;
} cout << "\nSum of a & b = " << sum(a, b) << endl;
friend int sum(A, B); // Friend function
}; return 0;
class B }
{
private:
int y;
public:
B() Output:
{
x = 5; Sum of x & y = 10
}
friend int sum(A, B); // Friend function
};
Implementing Friend functions
– As a member function of another class
#include <iostream> void A::display(B b1)
{
using namespace std; cout << b1.x << endl;
}
class B; // forward declaration of A needed by B
int main()
class A {
{ A a;
public: B b;
void display(B b); //no body declared
}; a.display(b);

class B return 0;
{ }
private:
int x;

public:
B() Output:
{
x = 4; Value of x = 4
}
friend void A::display(B);
};
Implementing Friend Class
• A friend class is used
– When a class needs to access private and protected members of
another class
– Syntax-
class A
{
//Other declarations
friend class B;

};

class B
{

};
Implementing Friend class
#include <iostream> class B
{
using namespace std; public:
void print(A &a1)
class A {
{ cout << "\n Value of x = " << a1.x << endl;
private: }
};
int x;
int main()
public: {
A() A a;
{ B b;
x = 5; b.print(a);
}
friend class B; return 0;
}; }

Output:

Value of x = 5
some important points about friend functions
and classes
• Friends should be used only for limited purpose
– Too many functions or external classes are declared as friends of
a class, it lessens the value of encapsulation in object-oriented
programming
– Friendship is not mutual
• If class A is a friend of B, then B doesn’t become a friend of A
automatically
• Friendship is not inherited

You might also like