C++ friendly functions

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
{
…….
…….
…….
public:
……..
……..
friend void xyz(); // declaration
};

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

Example with the use of Friend function

#include
<iostream>
using namespace std;
class sample
{
int a,b;
public:
void setvalue()
{
a=25;
b=40;
}
friend float mean(sample s);
};
float mean(sample s)
{
return float(s.a+s.b)/2.0;
}
int main()
{
sample x; // x object of type sample
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
{
………
………
int fun1(); // member function of x
………
};
class Y
{
……
……
friend int X:: fun1(); //fun1() of X ………
// is friend of y
………
};

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

#include<iostream>
using namespace std;
class Z
{
friend class X; // all member function of x are friends to Z
};
class ABC; // forward declaration
class XYZ
{
int x;
public:
void setvalue(int i)
{
x=i;
}
friend void max(XYZ,ABC);
};
class ABC
{
int a;
public:
void setvalue(int i)
{
a=i;
}
friend void max(XYZ,ABC); // declaration
};
void max(XYZ m, ABC n) // definition of friend
{
if(m.x>=n.a)
{
std::cout <<m.x;
}
else
{
std::cout<<n.a;
}
}
int main()
{
ABC abc;
abc.setvalue(10);
XYZ xyz;
xyz.setvalue(20);
max(xyz,abc);
return 0;
}

output :
20

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

  • C++ Dynamic Initialization of Objects
  • C++ Copy Constructor
  • C++ Dynamic Constructor
  • C++ Destructors
  • C++ Exercise
  • C++ Operator Overloading
  • C++ Overloading Unary Operators
  • Const pointer in C
  • Void pointer in c
  • C++ Overloading Binary Operators
  • C++ Overloading Binary Operators Using Friends
  • C++ Manipulation String Using Operators
  • C++ Rules for overloading operators
  • C++ Exercise
  • C++ Basic To class Type
  • C++ Class TO Basic Type
  • C++ One class To another class type
  • C++ Exercise
  • C++ Inheritance introduction
  • C++ Single Inheritance
  • C++ Multiple Inheritance
  • C++ Ambiguity Resolution in inheritance
  • C++ Hierarchical Inheritance
  • C++ Hybrid Inheritance
  • C++ Virtual Base Classes
  • C++ Exercise
  • C++ abstract class
  • C++ nesting of classes
  • C++ Exercise
  • C++ polymorphism
  • C++ Exercise
  • C++ pointers
  • C++ Pointers TO object
  • C++ this pointer
  • C++ Pointer to Derived class
  • C++ Virtual functions
  • C++ Exercise
  • C++ streams
  • C++ unformatted I/O operations
  • C++ Put() and get()
  • C++ getline() and write()
  • C++ Formatted console I/O
  • C++ Manipulators
  • C++ Exercise
  • C++ file handling
  • C++ file stream classes
  • C++ Open and closing file
  • C++ open using constructor
  • C++ open using open()
  • C++ Detecting End of file
  • C++ File modes
  • C++ File pointers and Manipulators
  • C++ Sequential I/O
  • C++ Reading and Writing
  • C++ Updating a File
  • C++ Error handling In File
  • C++ Command Line Arguments
  • C++ Exercise
  • C++ Template introduction
  • C++ Class Templates with multiple Parameters
  • C++ Function templates
  • C++ Function templates with multiple parameters
  • C++ member function Template
  • C++ Exercise
  • C++ Exception handling
  • C++ Basics of Exception Handling
  • C++ Exception Handling Mechanism
  • C++ Throwing Mechanism
  • C++ Catch Mechanism
  • C++ Catch all Exceptions
  • C++ Re-Throwing An Exception
  • C++ Specifying Exceptions
  • C++ Exercise