Pages

January 30, 2013

Friend Function in C++ (OOPs)


friend function and class in C++ (OOPs) programming language


A Friend Function is a non-member function that can access private data’s. A friend CLASS is a class whose member functions can access another class i.e. private and protected classes. 


Friend function breakup the concept of data hiding and encapsulation in OOP’s. It’s almost similar to ATM banking as there are several bank accounts stored in the ATM programming when we operate it just we can see our account related queries not other person account until or unless we have his card number and password. All the programming is being done under friend function to keep it protected.  

Features:

1. Friend function may be declared friend of more than one CLASS.
2. Friend function can be declared anywhere in the CLASS.
3. SRO(::) is not applicable in friend function.
4. Friend function can’t access the member of the CLASS directly and these can be accessed through objects name and dot operator called as Membership Operator.
5. The relationship between 1-Directional or Uni-Directional by using friend keyword i.e. when class B declared friend of class A, member function of class B can access private and protected data members of class A but the reverse is not true.

Syntax:

class A()
{
………..
}
class B()
{
………….
}
friend(class A, class B) 

/* Program to display values using friend class in C++ */


#include<iostream.h>
#include<conio.h>
class two;
class one
{
      private:
              int a,b;
      public:
             void enter()
             {
                  cout<<"\n Enter any value of a and b";
                  cin>>a>>b;
                  }
                  friend class two;
             };
class two
{
  public:
   void display(one o)
   {
    cout<<"\n Values entered by the user is "<<o.a<<" and "<<o.b;
                  }
};
main()
{
      class one o1;
      class two t;
      o1.enter();
      t.display(o1);
      getch();
      }
output of program to display values using friend class in C++ (OOPs)

/* Program to perform arithmetic operation using friend function*/

#include<iostream.h>
#include<conio.h>
class two;
class one
{
      private:
              int a;
      public:
             void enter()
             {
             cout<<"\n Enter any value of a";
             cin>>a;
             }
             friend int operation(one,two);
             };
class two
{
      private:
              int b;
      public:
             void enter()
             {
             cout<<"\n Enter any value of b";
             cin>>b;
             }
             friend int operation(one,two);
             };
int operation(one o,two t)
{
    int sum=(o.a)+(t.b);
    int sub=(o.a)-(t.b);
    int mul=(o.a)*(t.b);
    int mod=(o.a)%(t.b);
    cout<<"\n Addition is "<<sum;
    cout<<"\n Subtraction is "<<sub;
    cout<<"\n Multiplication is "<<mul;
    cout<<"\n Remainder is "<<mod;
}
main()
{
      class one o1;
      class two t1;
      o1.enter();
      t1.enter();
      operation(o1,t1);
      getch();
      }
output of program to perform arithmetic operation using friend function in C++ (OOPs)

/* Program to display a matrix using friend class */

#include<iostream.h>
#include<conio.h>
class show;
class matrix
{
      private:
              int a[4][4],i,j;
      public:
             void enter()
             {
                  cout<<"\n Enter 16 elements of a matrix";
                  for(i=0;i<4;i++)
                  {
                           for(j=0;j<4;j++)
                           {
                                 cin>>a[i][j];
                                                  }
                                                  }
                                                  }
                  friend class show;
};
class show
{
      public:
             void display(matrix m)
             {
                  cout<<"\n A: ";
                  for(m.i=0;m.i<4;m.i++)
                  {
                           for(m.j=0;m.j<4;m.j++)
                           {
                                cout<<m.a[m.i][m.j]<<" ";
                                         }
                                cout<<"\n    ";
                         }
                       }
     };
main()
{
      class matrix m1;
      class show s;
      m1.enter();
      s.display(m1);
      getch();
      }
output of program to display matrix using friend class in C++ (OOPs)



2 comments:

  1. Very informative article.Thank you author for posting this kind of article .



    http://www.wikitechy.com/view-article/friend-function-in-cpp-with-example-program-and-explanation



    Both are really good,
    Cheers,
    Venkat

    ReplyDelete
  2. This is really a nice blog. It is so informative and helpful post. For more information in this topic, visit here.. Merits and Demerits of Friend Functions – Object Oriented Programming (OOP)

    ReplyDelete