Nietzsche says "God is dead" God says "Nietzsche is dead "
Header image

C++ Friend Functions

Posted by ALonon in C++

A class can allow non-member functions and other classes to access its own private data, by making them as friends. This part of C++ tutorial essentially gives two important points.

  • Once a non-member function is declared as a friend, it can access the private data of the class
  • similarly when a class is declared as a friend, the friend class can have access to the private data of the class which made this a friend

Here is an example

/* Onur AKTAS - alonon.net
 * 15/12/2008
 * Friend Functions
 */
#include 
using namespace std;
class INTEGER
{
	int num; // private variable
public:
	INTEGER(int n=0) {num = n;} // constructor to assign n to num
	friend void add(INTEGER &x,INTEGER &y); // this is friend function, it is not a member function, and can not write in class
};
void add (INTEGER &x,INTEGER &y) // do not write friend at the begining
{
  cout<<"Addition is: "<

	

Related posts

You can follow any responses to this entry through the RSS 2.0 You can leave a response, or trackback.

Leave a Reply