Thursday, December 20, 2012

Friend Function – C++.


Friend functions are not considered as class members; they are like any other normal functions. We know that the private data member  of a class can be accessed only by the class' member functions. However , there is an exception. A function which declared with 'friend' keyword will be friendly  with any other class even though it is not a member of that class. The friend function can access the private members of the class. Friend function doesn't need any scope of the class.  They need not need to call with an object or with '.' and '->' operator.


Example : Adding two nos. using friend function.

#include <iostream.h>

  class Frinds;
  class Frinds_1
      {
           private :
              int x;
           public :
              void set(int n)
                   {
                        x=n;
                   }
              //Declaration of Friend Function max() in First Class
              friend void maximum(Frinds,Frinds_1);
      };
  class Frinds
      {
           private:
              int a;
           public:
              void setter(int n)
                   {
                        a=n;
                   }
              //Declaration of Friend Function max() in Second Class
              friend void maximum(Frinds, Frinds_1);
      };

  void maximum(Frinds f, Frinds_1 f1) //Friend Function max() without scope
      {
           if(f.a>f1.x)
              {
                   cout<<"\n\t"<<f.a<<" is big\n";
              }
           else
              {
                   cout<<"\n\t"<<f1.x<<" is big\n";
              }
      }
  void main()
      {
           clrscr();
           int n;
           Frinds f;
           Frinds_1 f1;
           cout<<"\n\tEnter value for first class : ";
           cin>>n;
           f.set(n);
           cout<<"\n\tEnter value for  second class : ";
           cin>>n;
           f1.set(n);
           maximum(f,f1); //calling the freind function
           getch();
      }


Example – Addition of Complex No.

#include <iostream.h>

  class Frinds
      {
           private :
              float re,im;
           public :
              void Read()
                   {
                        cout<<"\nEnter value for real : ";
                        cin>>re;
                        cout<<"\nEnter value for Imaginery : ";
                        cin>>im;
                   }
              void show(Frinds);
              friend Frinds complx(Frinds,Frinds);
      };
  void Frinds::show(Frinds f)
      {
           cout<<"\n"<<f.re<<" + "<<f.im<<"\n";
      }
  Frinds complx(Frinds f, Frinds f1)
      {
           Frinds f2;
           f2.re=f.re+f1.re;
           f2.im=f.im+f1.im;
           return f2;
      }
  void main()
      {
           clrscr();
           Frinds f,f1,f2;
           f.Read();
           f1.Read();
           f2=complx(f,f1);
           cout<<"First Input = ";
           f.show(f);
           cout<<"Second Input = ";
           f1.show(f1);
           cout<<"Result = ";
           f2.show(f2);
           getch();
      }

No comments: