Common Friend Function Of
More Than One Class
MADE BY:
MEHAK TAWAKLEY
ROLL NO. 04090302016
FRIENDLY FUNCTION
 One of the important features of OOPS is Data encapsulation which is wrapping up
of data and functions.
 The data is accessible to only those functions which are wrapped in the class.
 This insulation of the data from the direct access by the program is called data
hiding.
 We cannot have an access to the private data of class. However, there could be a
situation where we would like two classes to share a particular function. For this
that function would operate on the objects of both classes.
 In such situations, C++ allows the common function to be made friendly with both
the classes, thereby allowing the function to have access to the private data of
these classes. Such a function need not be a member of any of these classes.
 For eg, consider a case where two classes, manager and scientist, have been
defined. We would like to use a function income_tax() to operate on the objects of
both these classes.
FRIENDLY FUNCTION
 Friend functions is a function that can access the private data of more than one
classes.
 Such a function need not to be a member of any of the class.
Syntax
Class ABC
{
……
……
public:
..….
……
friend void xyz(void); //we can declare a friend function by simply declaring
function preceded by the keyword friend
};
DEFINITION AND DECLARATION
 The function declaration should be preceded by the keyword friend.
 The function is defined anywhere in the program like a normal function.
 The function definition does not use either the keyword friend or the scope
operator ::.
 The functions that are declared with the keyword friend are known as friend
function.
 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 members of the classes.
CHARACTERISTICS
 It is not in the scope of the class to which it has been declared as friend.
 Since it is not in the scope of the class, it cannot be called using the
objects of that class.
 It can be invoked like a normal function without the help of any object.
 Unlike member functions, it cannot access the member names directly and
has to use an object name and dot membership operator with each
member name.
 It can be declared either in the public or the private part of a class without
affection its meaning.
 Usually, it has the objects as arguments.
EXAMPLE OF USING FRIEND FUNCTION IN
MORE THAN ONE CLASS:
EXAMPLE OF USING FRIEND FUNCTION IN
MORE THAN ONE CLASS
EXAMPLE OF USING FRIEND FUNCTION IN
MORA THAN ONE CLASS
NOTE:
The function add() has arguments from both val1 and val2. when the function add() is
declared as a friend in val2 for the first time the compiler will not acknowledge the presence
of val1 unless its name is declared in the beginning as
Class val1;.
OUTPUT
Friend Function

Friend Function

  • 1.
    Common Friend FunctionOf More Than One Class MADE BY: MEHAK TAWAKLEY ROLL NO. 04090302016
  • 2.
    FRIENDLY FUNCTION  Oneof the important features of OOPS is Data encapsulation which is wrapping up of data and functions.  The data is accessible to only those functions which are wrapped in the class.  This insulation of the data from the direct access by the program is called data hiding.  We cannot have an access to the private data of class. However, there could be a situation where we would like two classes to share a particular function. For this that function would operate on the objects of both classes.  In such situations, C++ allows the common function to be made friendly with both the classes, thereby allowing the function to have access to the private data of these classes. Such a function need not be a member of any of these classes.  For eg, consider a case where two classes, manager and scientist, have been defined. We would like to use a function income_tax() to operate on the objects of both these classes.
  • 3.
    FRIENDLY FUNCTION  Friendfunctions is a function that can access the private data of more than one classes.  Such a function need not to be a member of any of the class. Syntax Class ABC { …… …… public: ..…. …… friend void xyz(void); //we can declare a friend function by simply declaring function preceded by the keyword friend };
  • 4.
    DEFINITION AND DECLARATION The function declaration should be preceded by the keyword friend.  The function is defined anywhere in the program like a normal function.  The function definition does not use either the keyword friend or the scope operator ::.  The functions that are declared with the keyword friend are known as friend function.  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 members of the classes.
  • 5.
    CHARACTERISTICS  It isnot in the scope of the class to which it has been declared as friend.  Since it is not in the scope of the class, it cannot be called using the objects of that class.  It can be invoked like a normal function without the help of any object.  Unlike member functions, it cannot access the member names directly and has to use an object name and dot membership operator with each member name.  It can be declared either in the public or the private part of a class without affection its meaning.  Usually, it has the objects as arguments.
  • 6.
    EXAMPLE OF USINGFRIEND FUNCTION IN MORE THAN ONE CLASS:
  • 7.
    EXAMPLE OF USINGFRIEND FUNCTION IN MORE THAN ONE CLASS
  • 8.
    EXAMPLE OF USINGFRIEND FUNCTION IN MORA THAN ONE CLASS NOTE: The function add() has arguments from both val1 and val2. when the function add() is declared as a friend in val2 for the first time the compiler will not acknowledge the presence of val1 unless its name is declared in the beginning as Class val1;.
  • 9.