C++ friendly functions
in the previous pages we lean that private members can’t be accessed from outside the class. That is a non member function can’t have an access to the private data of class. however there could be situation where two classes to share a particular(same) function. for example consider a case where two classes,scientist, manager have been defined. we would like to use a function income_tax() to operate on objects of both these classes. in such situations, C++ language allows the common functions to be made friendly with both the classes, in such situations C++ language allows the common function to be made friendly with both the classes, thereby allowing function to have access to the private(hidden) data of these classes
To make the outside function friendly to a class we have simple declare
this function as a friend of the class as shown below
class ABC
{
…….
…….
…….
:
……..
……..
xyz();
};
|
The function declaration should be preceded by the keyword friend. The function is defined anywhere in the program like a normal C++ function. The function definition does not use either the keyword friend or the scope operator :: the function that are declared with the keyword friend are known as friend functions. A function can be declared as a friend in any number of classes.
A friend function although not a member function, has full access rights to the private member of the class.
A friend function possesses certain special characterstics
1. It is’nt in the scope of the class to which it has been declared as friend.
2. since it is’nt in the scope of the class, it can’t be called using the object of that class.
3. it can be invoke like a normal function without the help of any object.
4. unlike member functions, it can’t access the member names directly and has to use an object name and dot membership operator with each member name.
5. it can be declared either in the public or the private part of a class without affecting its meaning.
6. usually, it has the objects as arguments.
The friend function are often used in operator overloading which be be discussed later
<iostream>
using namespace std;
class sample
{
a,b;
:
setvalue()
{
a=25;
b=40;
}
mean(sample s);
};
mean(sample s)
{
(s.a+s.b)/2.0;
}
main()
{
sample x;
x.setvalue();
std::cout<<"mean value="<<mean(x) << "\n";
}
|
Output is
mean value=32.5
|
The friend function accesses the class variable a and b by using the dot operator and the object passed to it. The function call mean(x) passes the object X by value to the friend function.
member functions of one class can be friend functions of another class. in such cases they are defined using the scope resolution operator which is show as follows
class X
{
………
………
fun1();
………
};
class Y
{
……
……
X:: fun1(); //fun1() of X
………
………
};
|
The function fun1() is a member of class X and a friend of class Y.
we can also declare all the member function of one class as the friend functions of another class. in such cases, the class is called a friend class. This can be specified as follows
iostream>
using namespace std;
class Z
{
class X;
};
class ABC;
class XYZ
{
x;
:
setvalue(int i)
{
x=i;
}
max(XYZ,ABC);
};
class ABC
{
a;
:
setvalue(int i)
{
a=i;
}
max(XYZ,ABC);
};
max(XYZ m, ABC n)
{
(m.x>=n.a)
{
std::cout <<m.x;
}
{
std::cout<<n.a;
}
}
main()
{
ABC abc;
abc.setvalue(10);
XYZ xyz;
xyz.setvalue(20);
max(xyz,abc);
0;
}
|
Note: The function max() has arguments from both XYZ and ABC. When the function max() is declared as friend in XYZ for the first time, The compiler will not acknowledge the presence of ABC its name is declared in the beginning as
class ABC;
This is known as forward declaration
As pointed out earlier, a friend function can be called by reference. In this case, local copies of the objects are not made, instead a pointer to the address of the object is passed and the called function directly works on the actual object used in the call.
This method can be used to alter the value of the private members of a class. Remember altering the values of private member in against the basic principal of data hiding. It should be used only when absolutely necessary