Results 1 to 6 of 6

Thread: What is a friend function?

  1. #1
    Join Date
    May 2009
    Posts
    1,191

    What is a friend function?

    Hi,
    I am first year B.Sc.It student. I just started learning c++ language. When reading c++ book I stop at friend function. I unable to understand this concept. If you any of you have any idea or example about friend function please help me.

  2. #2
    Join Date
    May 2008
    Posts
    2,297

    Re: What is a friend function?

    A friend function is usually used for taking non-public members of a class. You can access private member using friend functions. It is easy to define friend function, you only have to used friend keyword with any function. You can only used friend in function declaration. You can declare friend function in number of classes. You can declare friend function either with private access modifier or public access modifier.

  3. #3
    Join Date
    Oct 2005
    Posts
    2,393

    Re: What is a friend function?

    Just go through following example step by step.


    Code:
    #include
    class crystal
    {
    private:
    int p,q;
    public:
    void test()
    {
    p=100;
    q=200;
    }
    friend int compute(crystal e2)
    
    
    };
    
    int compute(crystal e2)
    {
    
    return int(e2.p+e3.q)-5;
    }
    
    main()
    {
    crystal es;
    es.test();
    cout<<"The result is:"<
    
    }

  4. #4
    Join Date
    May 2008
    Posts
    2,389

    Re: What is a friend function?

    This is the example of friend function to exchange the private values of two classes

    Code:
      
      #include<iostream.h>
      class P;
      class Q;
     
      class P
      {
        private:
          int v1;
        public:
          void in(int p)
            { 
              v1=p;
            }
          void display() 
            {
              cout<<v1;
            }  
          friend void exchange(P&, Q&);
      };
      class Q
      { 
         private:
            int v2;
         public:
            void in(int p)
             {
               v2=p;
             }
            void display()
             {
               cout<< v2;
             }
            friend void exchange (P&, Q&);
      };
      void exchange(P& w, Q& z)
          {
            int temp=w.v1;
            w.v1=z.v2;
            z.v2=temp;
          }
      int main()
         {
           P obj1;
           Q obj2;
           obj1.in(1000);
           obj2.in(2000);
           cout<<"\n Before";
           obj1.display();
           obj2.display();
           cout<<"\n After";
           exchange(obj1, obj2);        
           obj1.display();
           obj2.display();
           return 0;
         }
    
    
    #include <iostream>
    using namespace std;
     
    class Q; 
    class P
    {
    private:
        int p;
    public:
        P() { p=0; }
        friend void show(P& w, Q& z);
    };
     
    class Q
    {
    private:
        int q;
    public:
        Q() { q=6; }
        friend void show(A& w, Q& z);
    };
     
    void show(A& x, Q& y)
    {
      cout << "P::p=" << w.p << endl;
      cout << "Q::q=" << z.q << endl;
    }
     
    int main()
    {
       P p;
       Q q;
       show(p,q);
    }
    
    #include <iostream>
    using namespace std;
     
    class Q; 
    class P
    {
    private:
        int p;
    public:
        P() { p=0; }
        void show(P& w, Q& z);
    };
     
    class Q
    {
    private:
        int q;
    public:
        Q() { q=6; }
        friend void A::show(P& x, Q& y);
    };
     
    void P::show(P& w, Q& z)
    {
      cout << "P::p=" << w.p << endl;
      cout << "Q::q=" << z.q << endl;
    }
     
    int main()
    {
       P p;
       Q q;
       a.show(p,q);
       return 0;
    }

  5. #5
    Join Date
    Feb 2008
    Posts
    1,852

    Re: What is a friend function?

    This is the most simple example of friend function.


    Code:
    #include <iostream>
    
    using namespace std;
    class Point
    {
        friend void CP( Point & );
    public:
        Point( void ) : m(0) {}
        void PP( void ){cout << m<< endl; }
    
    private:
        int m;
    };
    
    void CP ( Point &i ) { p.m++; }
    
    int main()
    {
       Point sPoint;
       sPoint.PP();
       CP(sPoint);
       sPoint.PP();
    }

  6. #6
    Join Date
    Jan 2008
    Posts
    1,521

    Re: What is a friend function?

    Code:
    #include <iostream.h>
    class car
    {
    private:
        int SP;
        char c[30];
    
    public:
    void input( )
    {
    cout<<"\n color : ";
    cin>>c;
    cout<<"\n speed : ";
    cin>>SP;
    }
    
    friend void display(car1); 
    };
    
    void display(car1 x)
    {
    cout<<"\nThe color of the car is : "<<x.c;
    cout<<"\nThe speed of the car is : "<<x.SP;
    }
    
    int main( )
    {
    car1 m;
    m.input( ); 
    display(m); 
    return 0;
    }

Similar Threads

  1. Replies: 6
    Last Post: 27-03-2012, 11:54 PM
  2. How to see who un-friend you on facebook ?
    By Bindu-I in forum Technology & Internet
    Replies: 5
    Last Post: 28-05-2010, 10:04 PM
  3. c# function equivalent to gettime function in javascript
    By Omaar in forum Software Development
    Replies: 4
    Last Post: 10-03-2010, 10:44 PM
  4. How does abstract function differs from virtual function?
    By Maddox G in forum Software Development
    Replies: 5
    Last Post: 29-01-2010, 11:32 AM
  5. Not able to connect my friend on LAN?
    By Bilal in forum Networking & Security
    Replies: 2
    Last Post: 02-03-2009, 08:56 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,751,783,510.22124 seconds with 16 queries