Page 106 - DCAP404 _Object Oriented Programming
P. 106
Unit 5: Static Members
private: Notes
int x;
public:
void getdata( );
friend void disp(sample abc); //friend function
};
void disp(sample abc) // non-member function without scope::
operator
{
cout << “value of x =”<< abc.x;
cout << endl;
}
Notes Note that the function is declared as friend in the class and is defined outside the
class. The keyword friend should not be in both the function declaration and definition.
The friend declaration is unaffected by its location in the class. It can be declared either in
a public or a private section, which does not affect its access right.
Example: The following declarations of a friend function are valid:
1. The friend function disp( ) is declared in the public group
class sample
{
private :
int x;
public:
void getdata( );
friend void disp( );
};
2. The friend function disp( ) is declared in the private group
class sample
{
private:
int x;
friend void disp();
public:
void getdata( );
};
LOVELY PROFESSIONAL UNIVERSITY 99