Thursday, March 3, 2016

Friend

FRIEND FUNCTIONS
 A friend function has access to all private and protected members of the class for which it is a friend. To declare a friend function, include its prototype within the class, preceding it with the keyword friend.

FRIENDS
A friend is permitted full access to private and protected members. A friend can be a function, function template, or member function, or a class or class template.
 Use the friend specifier to declare a friend in the class granting friendship.
Note that friendship is given, not taken.
In other words, if class A contains the declaration friend class B;, class B can access the private members of A, but A has no special privileges to access B (unless B declares A as a friend).
By convention, the friend specifier is usually first, although it can appear in any order with other function and type specifiers. The friend declaration can appear anywhere in the class; the access level is not relevant.
You cannot use a storage class specifier in a friend declaration. Instead, you should declare the function before the class definition (with the storage class, but without the friend specifier), then redeclare the function in the class definition (with the friend specifier and without the storage class). The function retains its original linkage. If the friend declaration is the first declaration of a function, the function gets external linkage. For example:
class demo;
void func(demo d);
class demo {
  friend void func(demo);
};



#include <iostream>
class myclass {
int a, b;
public:
friend int sum(myclass x);
void getdata (int i, int j);
};
void myclass:: getdata(int i, int j)
{
a = i;
b = j;
}
// Note: sum() is not a member function of any class.
int sum(myclass x)
{
/* Because sum() is a friend of myclass, it can directly access a and b. */
return (x.a + x.b);
}
int main()
{
myclass A;
A. getdata(3, 4);
cout << sum(n);
return 0;
}
In this example, the sum() function is not a member of myclass. However, it still has full access to its private members. Also, notice that sum() is called without the use of the dot operator. Because it is not a member function, it does not need to be (indeed, it may not be) qualified with an object's name.
Although there is nothing gained by making sum() a friend rather than a member function of myclass, there are some circumstances in which friend functions are quite valuable. First, friends can be useful when you are overloading certain types of operators. Second, friend functions make the creation of some types of I/O functions easier. The third reason that friend functions may be desirable is that in some cases, two or more classes may contain members that are interrelated relative to other parts of your program.

#include<iostream.h>
#include<conio.h>
class A;
class B
{
private:int p;
public:
friend void getdata(A p, B &q);// try without these references
void friend dispdata(A &p, B &q);
};

class A
{
private:int p;
public:
friend void getdata(A p, B &q);
void friend dispdata(A &p, B &q);
};

void getdata(A q, B &r)
{
cout<<"\nenter values\t";
cin>>q.p>>r.p   ;
}

void dispdata(A &q, B &r)
{
cout<<"the values are\t";
cout<<endl<<q.p<<endl<<r.p   ;
}


void main()
{
clrscr();
int a=10;
//add(a);
cout<<endl<<a;
A o1;
B o2;
getdata(o1,o2);
dispdata(o1,o2);
getch();
}

No comments:

Post a Comment