Sunday, 9 November 2014

Friend functions and the usage in C++ with the examples

Friend functions and the usage in C++ with the examples:

Data hiding is one of the most important topic in C++, The private data is not accessed to the outside world. That is a nonmember function cannot access the private data of the particular class. This rule does not affect friend function and friend classes.
To declare the function as a friend of another class, there is a keyword called friend.

friend Function in C++:

If any function that we defined as a friend function then, the private and protected data of class can be accessed from that function.  The compiler can easily found that the given function is friend, when it founds the keyword friend.
The below is the syntax of the friend function:

class nameofthe class
{  friend return_type function_name(arguments list); };

The below is the example that will help us to understand the functionality of the friend function in c++
/* friend functions with the example  */
#include <iostream>
using namespace std;
class ClsRect {
    int iWidth, iHeight;
  public:
    void SetVals (int, int);
    int CalArea ();
    friend ClsRect GetVals (ClsRect); 
};
/* This fucntion is used to set the values */
void ClsRect::SetVals (int iW, int iH)
{
  iWidth = iW;
  iHeight = iH;
}
  /* This fucntion is used to calculte the are of the recatangle */
int ClsRect::CalArea ()
  { return (iWidth * iHeight); }
 /* This fucntion is used to get the values */
ClsRect GetVals (ClsRect ocRect)
{
  cout << "I am in the friend function" << endl;
  ClsRect ocRectTemp;
  ocRectTemp.iWidth = ocRect.iWidth * 2;
  ocRectTemp.iHeight = ocRect.iHeight * 2;
  return (ocRect);
}
int _tmain (int argc, _TCHAR* argv[])
{
  ClsRect ocRect, ocRect2;
  ocRect.SetVals (2,3);
  ocRect2 = GetVals (ocRect);
  cout << " Area of the rectangle" << ocRect2.CalArea () << endl;
  return 0;
}

The output of this program is:
 I am in the friend function

 Area of the rectangle6



The below is the different access types according to who can access them in the following way:




The GetVals () function is a friend of ClsRect. From within that function we have been able to access the members iWidth and iHeight ClsRect, which are private members. Notice that neither in the declaration of GetVals () nor in its later use in main () have we considered duplicate a member of class ClsRect. It isn't! It simply has access to its private and protected members without being a member of different objects of type.
The friend functions are useful to perform the different operation on two classes.

Example to operate on Objects of two Different classes using friend Function:

/* friend functions in between the two objects with the example  */
 #include <iostream>
using namespace std;
class ClsSecond;     // forward declaration of ClsSecond
class ClsFirst
{
       int iVal;
    public:
      ClsFirst (): iVal (10) { }
      friend int FriendFunc (ClsFirst , ClsSecond);   //friend function Declaration in the ClsFirst
};
class ClsSecond
{
       int data;
public:
    ClsSecond (): data (1 ){ }
       friend int FriendFunc (ClsFirst ,ClsSecond);  //friend function Declaration ClsSecond
};
int FriendFunc (ClsFirst ocFirst, ClsSecond ocSecond)
/*Function func() is the friend function of both classes A and B. So, the private data of both class can be accessed from this function.*/
{
   return (ocFirst.iVal + ocSecond.data);
}
int _tmain (int argc, _TCHAR* argv[])
{
        ClsFirst oFirst;
        ClsSecond ocSecong;
        cout<<"Value is : "<<FriendFunc (oFirst, ocSecong);
        getchar ();
        return 0;
}

In this program, classes ClsFirst and ClsSecond has declared FriendFunc () as a friend function. Thus, this function can access private data of both classes. In this program, two objects of two different class ClsFirst and ClsSecond are passed as an argument to friend function. Thus, this function can access private and protected data of both classes. Here, FriendFunc () function adds private data of two objects and returns it to main function.
To work this program properly, a forward declaration of a class should be made as in above example (forward declaration of class ClsSecond is made). It is because class ClsSecond is referenced from class ClsFirst using code
 friend int FriendFunc (ClsFirst , ClsSecond); 
So, class ClsFirst should be declared before class ClsSecond to work properly.
The output of the above program is
 Value is : 11




1 comment:

Rumana Badar said...

A derived class' member functions can access Public and Protected members of base class

not private members, as put in the table, if I'm not wrong