Friendly function and rules of friend function, C/C++ Programming

Friendly Function:

A class can have public members and private members.   The public member can be

used through the class using dot operator.   Where as private member cannot be accessed outside the class.  In other word a private member can be accessed only by the members of its class.   In some situation it may be essential to share a function between or among classes in such case the member function is declared as friendly member function in the class.

Note: The  private  data  member can be used outside  the  class only  through  friend function with object class as argument.

Program to read private member from a member function:

class sample


 int a;


int b;


void setdata( )




friend int putdata(sample s);


int putdata(sample s)

{ return (s.a+s.b);


int main( )

{sample x;

x.setdata( );

cout<<"Sum is:"<

return 0;


Rules of friend function:

  • It defined without accessing the scope of the class.
  • It can work similar global function when the argument is not a class.
  • It cannot use the member directly private or public.
  • The private member can be changed or accessed only through its object argument.
  • It can be defined as inline function outside the class only not in prototype.
  • It can be a public or private function without any effect of its meaning.
  • It usually has class as its arguments only then it can be used among defined class arguments.

The member function of one class can be accessed as friend function of another class.  We can also make a class as friend of another class.  When class is defined as friend class all the member-functions from the friend class can be accessed.  Consider the given examples.

class X

{int fun( );


class Y

{friend int X::fun( ); //Member function fun in class X is friend function in class Y


class Z

{friend class X;  //All the members of X is a friend class in X



Program to share function between classes:

Note: Remember the private data member can be accessed outside the class only through friend function with object class as argument.


class ABC; //Forward declaration

class XYZ

{int x;


void setvalue(int i)


friend void max(XYZ,ABC); // friend void max(XYZ &,ABC &);


class ABC

{int a;


void setvalue(int i)


friend void max(XYZ,ABC); // friend void max(XYZ &,ABC &);


void max(XYZ m, ABC n) // void max(XYZ &m, ABC &n)







int main()






ABC abc; XYZ xyz; abc.setvalue(100); xyz.setvalue(200); max(xyz,abc);

return 0;}


The above program depicts how a friend function is shared between classes, class must be declared in the beginning this is called forward declaration.  The above friend function can be called be reference, the advantage of call by reference is the objects are not created locally at the main program instead the pointer to the address of the object is passed.

The member function can be declared constant member function by using the following syntax in the declaration.

void  mult(int,int)  const;  The  two  argument  int  cannot  be  changed  in  the  function definition.  The keyword const must be used as shown above.

Posted Date: 9/20/2012 8:00:28 AM | Location : United States

Related Discussions:- Friendly function and rules of friend function, Assignment Help, Ask Question on Friendly function and rules of friend function, Get Answer, Expert's Help, Friendly function and rules of friend function Discussions

Write discussion on Friendly function and rules of friend function
Your posts are moderated
Related Questions
A:BinaryHeap.h ------------ #ifndef BINARY_HEAP_H_ #define BINARY_HEAP_H_ #include "dsexceptions.h" #include "vector.h" // BinaryHeap class // CONSTRUCTION: wi

There are 25 employees in a business. Each employee has 5 sales each day of the month (Assume there are 30 days in each month of the year). Draw a flowchart for a pr

write a program that counts the number of occurrences of the string in the n-th padovan string p(n)   program 1 : package test.padovanstring; public class PadovanString {

Smugglers are becoming very smart day by day. Now they have developed a new technique of sending their messages from one smuggler to another. In their new technology, they are send

c program to find pebble merchant

Given an integer n and a permutation of numbers 1, 2 ... , n-1, n write a program to print the permutation that lexicographically precedes the given input permutation. If the given

Explain about Evaluation of Expression in c language? An Expressions are evaluated using an assignment statement of the form: variable = expression; The Variable is any v

how can I write a c++ program that generate all evn numbers less than 1000?