Search This Blog

Monday, 24 February 2014

friend function in c++

/*
   friend function is a special member function which can access private members of a class.
  Syntax for such functions are : friend <return_type> <function_name>(Class_Name Object);
  Ex - friend int add(Number n);
*/


#include<iostream.h>
#include<conio.h>
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();
}

Output



No comments:

Popular Posts