Friday, 12 August 2011

Friend Function

Friend Function:
Friend function is used to access the private members or protected members of some other class.
like friend function we have friend class also

Friend class:
A friend class in C++ can access the data members "private" and "protected" members of the class in some other class which it is declared as a friend keyword.



Why should we use Friend function:

When a data is declared as private inside a class, then it is not accessible from outside the class. A function that is not a member or an external class will not be able to access the private data. A programmer may have a situation where he or she would need to access private data from non-member functions and external classes. For handling such cases, the concept of Friend functions is a useful tool.

  • friend is a keyword, before accessing the data, friend function declaration must be declared in private, or public scope.
  • friend function has no scope (either private or public no matter)
  • friend function is used to access the data only but we can not modify the data of the private members like member function. But we can change the data in member functions.
  • there is no  friend Constructors and Destructors
  • friend functions must be defined out side of its class only, system give error if we declare in side the class as it is non member
  • A friend function may be friend to some other classes also.
  • If class A is a friend of class B, class B is not automatically a friend of class A
  • If class A is a friend of class B, and class B is a friend of class C, class A is not automatically a friend of class C.
  • A friend of class Base is not automatically a friend of class Derived and vice versa; equally if Base is a friend of another class, Derived is not automatically a friend and vice versa

    •  1) Friend of the class can be member of some other class.
        2) Friend of one class can be friend of another class or all the classes in one program, such a friend is known as GLOBAL FRIEND.
        3) Friend can access the private or protected members of the class in which they are declared to be friend, but they can use the members for a specific object.
        4) Friends are non-members hence do not get “this” pointer.
        5) Friends, can be friend of more than one class, hence they can be used for message passing between the classes.
        6) Friend can be declared anywhere (in public, protected or private section) in the class.
Note:

  1. The keyword friend is placed only in the function declaration of the friend function and not in the function definition.
  2. It is possible to declare a function as friend in any number of classes.
  3. When a class is declared as a friend, the friend class has access to the private data of the class that made this a friend.
  4. A friend function, even though it is not a member function, would have the rights to access the private members of the class.
  5. It is possible to declare the friend function as either private or public.
  6. The function can be invoked without the use of an object. The friend function has its argument as objects

//friend function
#include<iostream.h>
#include<conio.h>
class c2
{
 public:
  void f7();
};
class c1
{
 private:
  int a,b,c;
 public:
  c1()
  {
   a=2;
   b=3;
   c=4;
  }
  friend void f7();

};
void f7()
{
 c1 obj;
 cout<<obj.a<<endl<<obj.b<<obj.c<<endl;
}
void main()
{
 clrscr();
 f7();
 getch();
}


No comments:

Post a Comment