|
Introduction to Friend Functions
The concept of data hiding and
encapsulation dictates that non-member
functions should not be able to access
an object’s private or protected data.
However, there are situations where
such rigid discrimination leads to
considerable inconvenience. In order
to access the non-public members of a
class, C++ provides the friend
keyword. Consider the following class
which represents the metric units for
measuring distance.
Class mks_distance
{
private:
intiMeter;
intiCm;
public:
mks_distance(int, int); //Constructor
void disp_distance(void); //Function
to display distance in meters
};
mks_distance :: mks_distance(iniMt=0,
initCmt=0)
{
iMeter = iMt;
iCm = iCmt;
}
void mks_distance ::
disp_distance(void)
{
cout<<”The distance =”<<iMeter<<”.”<<iCm<<”meters”<<endl;
}
Now let us write a compare() function
that accepts two objects of the
mks_distance object type, and returns
zero if the values of the objects are
equal. This function needs to access
the private members of the class
instances in order to compare the
values. The only way to access the
private data is to use the public
functions defined within the class.
However, if the compare() function is
declared as a friend function of the
mks_distance class, the function can
access all the non-public members of
all instances of the class. A function
can be declared as a friend by using
the friend keyword before the function
declaration. The declaration of the
friend function must be included
within the class of which it is a
friend.
The modified class declaration is a s
follows:
Class mks_distance
{
.
.
.
friend int compare(mks_distance&,
mks_distance&);
.
.
};
The executable statements of the
compare() function can be declared
anywhere in
the program.
The function to compare the two
objects of the mks_distance class and
to return the difference between them
in centimeters is given below:
Int compare(mks_distance &m1,
mks_distance &m2)
{
//Accessing the private members of
objects of mks_distance class
int m_diff = m1.iMeter – m2.iMeter;
int cm_diff = m1.iCm – m2.iCm;
return(m_diff*100 + cm_diff);
}
In the example, by declaring the
function compare() as a frind of
mks_distance class, the function is
allowed to access the non-public
members of the instances of the class.
Declaring the compare() function as a
friend within the class declaration of
the mks_distance clas, does not make
it a member of the class.
Just as a function can be made a
friend of a class, an entire class can
also be made a friend of another
class.
For example,
Class fps_distance
{
.
.
.
friend class mks_distance;
.
.
};
In this example, the mks_distance
class is the friend of the
fps_distance class. All the member of
the fps_distance class can now be
accessed from the mks_distance class.
Program 17.1 illustrates an entire
class which is declared as a friend of
another class.
#include<iostream.h>
class bclass; //Forward declaration of
bclass
class aclass;
{
private:
iniData1;
public:
aclass()
{
iData1 = 10;
}
friend class bclass;
};
class bclass
{
public:
void func(aclass A11)
{
cout<<”The value of iData1 in class
aclass is”<<A11.iData1<<endl;
//The function can access the private
data of a class
}
};
void main()
{
aclass A1;
bclass B1;
B1.func(A1);
}
In Program 17.1, the bclass class is a
friend of the aclass class. Now all
the member functions of bclass can
access the non-public data of aclass.
|