Friend Function and Friend Class

FRIEND FUNCTION.

As we discussed C++ is having a great feature of Data Hiding(Encapsulation),that means Wrapping of data into a Single unit.For ex: all the Data members and member functions are wrapped  up  into a single unit called class.

where according to the concept we apply security or data hiding  by using Access Specifiers PRIVATE and PROTECTED,so according to policy ,if you are not a member you cannot access the private and protected members of a class. However to get rid of this situation we came up with a concept of Friend Function where a non -data member of a class can access the private and protected data members by making a non member friend.

The main purpose of friend function is to access the private and protected data members of the class as discussed above.

These can be done in 3 ways:

1)By declaring member function a FRIEND function of the class.

2)By declaring a class as FRIEND class, inside another class.

3)By declaring common FRIEND function for two classes.

Describing each way in detail with example:

1)By declaring member function a FRIEND function of the class.

Rules for using friend function in this step:

Declaration:

class abc

{  private:

………

……….

public:

………

……….

friend return _type func.name(class abc ob1);

………

}

Main point to remember here is if a member function is made friend you need to pass a object of the class as a parameter.

The member function of class can all be made friends at the same time when you make the entire class a friend.

Definition:              

  • In function definition friend keyword is not used. You can define it as a simple member function, if it is defined inside the class, same is the case if you define it outside the class also. There is no need of using Scope resolution operator because when we make  function friend, it is by default INLINE.
  •  Secondly as seen in function declaration of a friend function it is important to  pass object of a class  as a parameter(friend return _type func.name(class abc ob1)),As shown here Abc class Object is passed ,so inorder  to access the private data member inside the function definition you have to access the private member using object and (.)  operator.as shown below:

Function definition:

void area(Abc object)              // func defined outside the class does not need keyword friend

{ int ar;

cout<<“area of Rectangle is: “;

ar=object.l*object.m;            // accessing default data members inside function definition.

cout<<ar<<endl;

}

PROGRAM OF FINDING  AREA & DISPLAYING OUTPUT BY MAKING A  MEMBER FUNCTION   friend OF A CLASS.

#include<iostream>
using namespace std;

class Abc
{private:
int l,m;
public:
friend void area(Abc object);     //Declaring member function friend
void getdata()
{int ar;
cout<<"enter l and m: ";
cin>>l>>m;
}
};
void area(Abc object)             // func. defined outside the class does not need keyword friend
{int ar;
cout<<"area of Rectangle is: ";
ar=object.l*object.m;
cout<<ar<<endl;
}
int main()
{
Abc obj1;
int r=10,a=20,b=30;
obj1.getdata();
area(obj1);         //no need to call friend function using object of class.(object ob1.is passed as actual arg.)
return 0;
    
}

 

2)By declaring a class as FRIEND class, inside another class.

It simply means that we can make Complete class  Friend, inside another class.

Concept:Here Class two is made friend of class one.Means class two can access all the data members and member function of class one ,but Remember,friend function is not Reciprocal,since two class is made friend of class one ,only class two can have all the access of data members of class one,but class one does not have any access on Private data members of class two.

DECLARING CLASS AS FRIEND

class one

{

private:

………….

public:

void getdata();

……………

}

friend class two;                               // making class two friend of  class one

};

CONCLUSION:

Here class two is made friend of class one. It means all the members functions and data members of class one  can be accessed by class two(in simple words class two can access private & protected data members & methods of class one ). But since class two is friend of class one (class one is not friend of class two) so class one cannot access the private and protected member function of class two .

KEY POINTS TO REMEMBER

1.In above example we declared class two friend of class one , so when member function of class two is defined for ex: void sum(one object),in this member function the argument passed will be an object of class one.

Look at this prog:

PROGRAM OF AREA TO MAKE A  friend CLASS USING TWO CLASSES.

#include<iostream>
using namespace std;

class Abc
{
private:
int l,m;
public:
void getdata();
friend class xyz;        //class declared friend
};
class xyz
{public:
void area(Abc object)
{ int ar;
cout<<"area of Rectangle is: ";
ar=object.l*object.m;
cout<<ar<<endl;
}
};
void Abc::getdata()
{int ar;
cout<<"enter l and m: ";
cin>>l>>m;
}
int main()
{
Abc obj1;
xyz obj2;
obj1.getdata();
obj2.area(obj1);    // friend func.is called by obj.of class abc but obj. of class xyz is passed as parameter
return 0;
}

step 1

class two is declared friend of class one.

step 2:

In function definition of class two ,parameter used will be  the  object of class one.

step 3:

Inside main(),  the objects of respective classes  are made, and member functions are called by their  respective objects.Like

object1.getdata();

object2.sum(object1);                   //here sum() is member function of class two where in actual argument                                                                                                                               //the object of class one is passed.

Example 2:

Finding sum using the concept of Friend function by declaring same member function as friend of two different classes.

//making class friend
//the sum of two numbers declared in a class and display the number and sum using friend class.
#include<iostream>
using namespace std;
class one 
{private :
int a,b,sum;
public:
void getdata()
{
cout<<"Enter two numbers"<<endl;
cin>>a>>b;
}
friend class two;
};
class two
{
public:
void sum(one obj)
{
cout<<endl;
cout<<"Entered numbers are:"<<endl<<obj.a<<obj.b;
obj.sum=obj.a+obj.b;
cout<<endl<<"The sum of entered number is:"<<obj.sum;
}
};
int main()
{one object1;
two object2;
object1.getdata();
object2.sum(object1);
return 0;
}


3)By declaring common FRIEND function for two classes.

Till now studying the first two parts of friend function , I hope you understood the concept of Friend Function.

Right now we will discuss How can we make a common friend Function for two classes?

1.Forward Declaration: You need to declare the Class two before defining class two,which is called forward declaration.

//making same function friend of two classes.
#include<iostream>
using namespace std;
class xyz;            //forward declaration of class2 xyz
class Abc            //class1
{private:
int l;
public:
friend void area(Abc object1,xyz object2);// common member func.made friend
void getdata()
{
cout<<"enter l : ";
cin>>l;
}
};
class xyz
{private:
int m;
public:
friend void area(Abc object1,xyz object2);//// common member func.made friend
void getdata()
{
cout<<"enter m : ";
cin>>m;
}
};
void area(Abc object1,xyz object2)// common definition of friend function.
{int ar;
cout<<"area of Rectangle is: ";
ar=object1.l*object2.m;
cout<<ar<<endl;
}
int main()
{
Abc obj1;
xyz obj2;
obj1.getdata();
obj2.getdata();
area(obj1,obj2);                //func.call of common friend function.
return 0;
}

Step 1: Forward declaration of class xyz is done,before class Abc,to tell compiler that  there exist a class xyz when you are using class xyz inside class Abc.

Step 2: Now here as shown area() is made common friend for two classes:

friend void area(Abc object1,xyz object2);

once you make area(Abc object1,xyz object2) as common friend you need to pass the objects of both the classe as parameters  ,when you define this function in both the classes.

Step 3:

Similarly in Function call , area(obj1,obj2),will be called  only once , and you will be passing objects of both the classes while calling the friend function.

and function will not be called by the objects of the class.

At last lets discuss properties of friend function:

  • 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.

    Some more glory Points.

    Friendship is not inherited, transitive, or reciprocal.

    Inherited: It means if you inherit a friend class it doesn’t means  that,a class which is inherited (base class) will also act as a friend class.

    In Abstract Derived classes don’t receive the privileges of friendship (more on this when we get to inheritance in a few classes)

           Transitive :if class A is friend of class B and class B is friend of class C,so  it doesn’t means  that            Class A is  friend of Class C.

            Or in simple words Class C can’t access private members of class A.

            Reciprocal: If  class A is friend of class B ,According to property only Class A can access private             members of class B,Since Class B is not a friend of class A ,Class B cannot access private                           members of class A.

 

 

 

 

 

 

 

 

 

 

1 thought on “Friend Function and Friend Class

Leave a Comment