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

Assignment Help:

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;

public:

void setdata( )

{a=10;

b=20;

}

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;

public:

void setvalue(int i)

{x=i;}

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

};

class ABC

{int a;

public:

void setvalue(int i)

{a=i;}

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

};

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

{if(m.x>n.a)

cout<

 

else

 

}

int main()

{clrscr();

 

 

cout<

 

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.


Related Discussions:- Friendly function and rules of friend function

Prepare an application in c# or c++, Prepare an Application in C# or C++ ...

Prepare an Application in C# or C++ Project Description: We are seeking someone who can write a program in C# or C++. The idea is to prepare the program using a Cross Plat

How does free know the size of memory to be deleted.?, How does free know t...

How does free know the size of memory to be deleted.? int *i = (int *)malloc(12); followed by free(i); how did free function call know how much of memory to delete? A: It bas

Palindrome, A palindrome is a string that reads the same from both the ends...

A palindrome is a string that reads the same from both the ends. Given a string S convert it to a palindrome by doing character replacement. Your task is to convert S to palindrome

Inheritance, example of program to ad two numbers

example of program to ad two numbers

Iteration problem, my program does not run more than 9 iterations when ever...

my program does not run more than 9 iterations when ever i want to iterate more than 100 iterations. what is the reason and how to tackle?

Stack, Implement multiple stacks in a single dimensional array. Write algor...

Implement multiple stacks in a single dimensional array. Write algorithms for various stack operations for them.

Boardcoloring, in this problem u given a board in which some of the element...

in this problem u given a board in which some of the elements are placed as shown in diagram below .each element represent a color .fill the other elements in the board such that n

C program for sorting of long word with different string, C Program for SOR...

C Program for SORTING OF LONG WORD WITH DIFFERENT STRING #include stdio.h> #include conio.h> #include string.h>   void main() {           char n[50],c[25][25]

Write Your Message!

Captcha
Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd