Friday, 8 March 2013

C++ Programming friend Function and friend Classes


C++ Programming friend Function and friend Classes

http://c-madeeasy.blogspot.in/2012/02/c-program-to-implement-friend-class-for.html

http://www.hscripts.com/tutorials/cpp/friend-function.php

 

One of the important concept of OOP is data hiding, i.e., a nonmember function cannot access an object's private or protected data. But, sometimes this restriction may force programmer to write long and complex codes. So, there is mechanism built in C++ programming to access private or protected data from non-member function which is  friend function and friend class.

friend Function in C++

If a function is defined as a friend function then, the private and protected data of class can be accessed from that function. The complier knows a given function is a friend function by its keyword friend. The declaration of friend function should be made inside the body of class (can be anywhere inside class either in private or public section) starting with keyword friend.
class class_name
{
    ......  ....   ........
    friend return_type function_name(argument/s);
    ......  ....   ........
}
Now, you can define friend function of that name and that function can access the private and protected data of that function. No keywords in used in function definition of friend function.
Explanation
Friend Function is a function that can have access to even the private, protected members of a class. This can be an ordinary function or a member of another class. It acts as a bridge between two classes.
Example:
   #include <iostream.h>
   class friendcl
   {
     private:
      int a,b;
     public:
      friend int sum ( friendcl x);
      void set( int w, int q);
   };
   void friendcl:: set( int w, int q)
   {
     a= w;
     b=q;
    }
   int sum (friendcl x)
    {
    return x.a + x.b;
    }
   int main()
   {
     friendcl r;
     r.set(6,7);
           cout <<  "The sum of the values is::"  << sum (r);
      return 0;
   }

Result:
   The sum of the values is::13
In the above example the class "sum" is a friend function for the class "friendcl". So this can use even the private variables "a,b" of the class "friendcl". The function "sum" is not a member of any class. The "x" is an object of the class "friendcl" function, which is declared in the friend function to pass arguments.
Thus, a bridge between two classes, i.e. "sum" and "friendcl" can be established. 

friend Class in C++ Programming

Similarly like, friend function. A class can be made a friend of another class using keyword friend. For example:
........  .....  ........
class A{
   friend class B;      // class B is a friend class
   ..... ..... .....
}
class B{
   ..... ..... ..... 
}
When a class is made a friend class, all the member functions of that class becomes friend function. In this program, all member functions of class B will be friend function of class A. Thus, any member function of class B can access the private and protected data of class A.
If B is declared friend class of A then, all member functions of class B can access private data and protected data of class A but, member functions of class A cannot private and protected data of class B. Remember, friendship relation in C++ is granted not taken.
Friend Class can access the private and protected members of the class for which it is friend to c,it is done by a declaring prototype of this external function within the class, and with the keywordfriend.

Friend class is used in C++ to gain access to protected members exclusively by the friend class 
The following is an Example Program in C++ using friend class to fins the sum of two numbers
 #include<iostream.h> 
 #include<conio.h> 
 class readint 
 { 
 float a,b; 
 public: 
 void read() 
 { 
 cout<<"\n\nEnter the First Number : "; 
 cin>>a; 
 cout<<"\n\nEnter the Second Number : "; 
 cin>>b; 
 } 
 friend class sum; 
 }; 
 class sum 
 { 
 public: 
 float c; 
 void add(readint rd) 
 { 
 c=rd.a+rd.b; 
 cout<<"\n\nSum="<<c; 
 } 
 }; 
 void main() 
 { 
 int cont; 
 readint rd; 
 sum s; 
 clrscr(); 
 do 
 { 
 clrscr(); 
 rd.read(); 
 s.add(rd); 
 cout<<"\n\nDo you want to continue?(1-YES,0-NO)"; 
 cin>>cont; 
 }while(cont==1); 
 getch(); 
 } 

No comments:

Post a Comment