FRIEND FUNCTIONS AND FRIEND CLASS

Friend Functions and Friend Class:

a) The private members of a class are not accessible outside the class.
b) The only exception to this is friend functions and friend class.

Friend Function:
a) The function which is declared in a class as friend can access the private members of the class.
b) The friend function cannot access the this pointer.

class A
{
private:
int i, y;
public:
friend void fri_function();
};
void fri_function()
{
A a;
a.i = 1;
a.y = 2;
}

Friend Class:
a) A class can be declared as a friend to another class.
b) The member functions of the container class can access the private members of the friend class by creating an object of the friend class.

class A;
class B
{
public:
friend A;
};

Thus A becomes a friend of B.

Popular posts from this blog

OBJECT ORIENTED ANALYSIS AND DESIGN (OOAD)

OBJECT ORIENTED PROGRAMMING

STARTING A BUSINESS