Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Friend Function

    • 0
    • 1
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 464
    Comment on it

    Friend Function

    • These are special functions which can access the private members of a class.
    • Private and protected data of class can be accessed from a friend function.

    Declaration of Friend Function

    class className
    {
        ......  ....   ........
        friend returntype functionname(argument/s);
        ......  ....   ........
    }
    

    Points to remember

    • Friend of the class can be member of some other class.
    • Friend can access the private or protected members of the class .
    • These are non-members hence do not get this pointer.
    • They can be used for message passing between the classes since friends can be friend of more than one class,.
    • This class can be declared anywhere i.e in public, protected or private section in the class.

      E.g of Friend Function

      class  base
      {
          int val1,val2;
         public:
          void get()
              {
                 cout<<"Enter two values:";
                 cin>>val1>>val2;
              }
              friend float mean(base ob);
          };
          float mean(base ob)
          {
             return float(ob.val1+ob.val2)/2;
          }
          void main()
          {
              clrscr();
              base obj;
              obj.get();
              cout<<"\n Mean value is : "<<mean(obj); 
              getch();
      }
      

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: