Monday, 30 March 2026

FRIEND FUNCTIONS in C++

 

FRIEND FUNCTIONS
The main concepts of the object-oriented programming paradigm are data hiding and data encapsulation. Whenever data variables are declared in a private category of a class, these members are restricted from accessing by non-member functions. The private data values can be neither read nor written by nonmember functions. If any attempt is made directly to access these members, the compiler will display an error message as “inaccessible data type”. The best way to access a private data member by a non-member function is to change a private data member to a public group. When the private or protected data member is changed to a public category, it violates the whole concept of data hiding and data encapsulation. To solve this problem, a friend function can be declared to have access to these data members. Friend is a special mechanism for letting non-member functions access private data. A friend function may be either declared or defined within the scope of a class definition. The keyword friend inform the compiler that it is not a member function of the class. If the friend function is declared within the class, it must be defined outside the class, but should not be repeated the keyword friend. The general syntax of the friend function is, 

friend return_type function_name (parameters); 

where friend is a keyword used as a function modifier. A friend declaration is valid only within or outside the class definition.

Example 1.
A program to access the private data of a class by non-member function through friend, where the friend function is declared in the location of public category.

//declaring friend function
#include <iostream>
using namespace std;
class sample {
private :
int x;
public :
void getdata();
friend void display(class sample);
};
void sample :: getdata()
{
cout << “ enter a value for x \n”;
cin >> x;
}
void display(class sample abc)
{
cout << “ Entered number is :” << abc.x;
cout << endl;
}
int main()
{
sample obj;
obj.getdata();
cout <<“ accessing the private data by non-member function \n”;
display(obj);
return 0;
}
Output of the above program
enter a value for x
100
accessing the private data by non-member function
Entered number is: 100

Example 2.
A program to access the private data of a class by non-member function through friend, where the friend function is declared in the location of private category.

//declaring friend function
#include <iostream>
using namespace std;
class sample {
private :
int x;
friend void display(class sample);
public :
void getdata();
};
void sample :: getdata()
{
cout << “ enter a value for x \n”;
cin >> x;
}
void display(class sample abc)
{
cout << “ Entered number is :” << abc.x;
cout << endl;
}
int main()
{
sample obj;
obj.getdata();
cout <<“ accessing the private data by non-member function \n”;
display(obj);
return 0;
}
Output of the above program
enter a value for x
20
accessing the private data by non-member function
Entered number is: 20

Example 3.
A program to access the private data of a class by non-member function through friend, where the friend function is defi ned within the scope of a class defi nition itself.

// friend function is de ned within the scope of a class de nition
#include <iostream>
using namespace std;
class sample {
private :
int x;
public :
inline void getdata();
friend void display(sample abc)
{
cout << “ Entered number is :” << abc.x;
cout << endl;
}
};
inline void sample :: getdata()
{
cout << “ enter a value for x \n”;
cin >> x;
}
int main()
{
sample obj;
obj.getdata();
cout <<“ accessing the private data by non-member function \n”;
display(obj);
return 0;
}
Output of the above program
enter a value for x
300
accessing the private data by non-member function
Entered number is: 300

Example 4.
A program to access the private data of a class by non-member function through a friend specifi er, where the friend function is defi ned with inline code substitution.

//declaring friend function with inline code
#include <iostream>
using namespace std;
class sample {
private :
int x;
public :
inline void getdata();
friend void display(class sample);
};
inline void sample :: getdata()
{
cout << “ enter a value for x \n”;
cin >> x;
}
inline void display(class sample abc)
{
cout << “ Entered number is :” << abc.x;
cout << endl;
}
int main()
{
sample obj;
obj.getdata();
cout <<“ accessing the private data by non-member function \n”;
display(obj);
return 0;
}
Output of the above program
enter a value for x
10
accessing the private data by non-member function
Entered number is: 10
Noe:The storage class modifi er inline is used in both places, friend function declaration and the function defi nition.

Example 5.
A program to access the private data of a class by non-member function through a friend specifi er, where the friend function is defi ned with inline code substitution. The keyword inline is used in both the function declaration and the function defi nition.

#include <iostream>
using namespace std;
class sample {
private:
int x;
public :
inline void getdata();
inline friend void display(class sample);
};
inline void sample :: getdata()
{
cout << “ enter a value for x \n”;
cin >> x;
}
inline void display(class sample abc)
{
cout << “ Entered number is :” << abc.x;
cout << endl;
}
int main()
{
sample obj;
obj.getdata();
cout <<“ accessing the private data by non-member function \n”;
display(obj);
return 0;
}
Output of the above program
enter a value for x
100
accessing the private data by non-member function
Entered number is: 100

Example 6.
A program to demonstrate how a class fi rst has granted its friendship to the class second to access the private data of the class fi rst through the public member function of the class second.

//granting friendship with another class
#include <iostream>
using namespace std;
class rst {
friend class second;
private :
int x;
public :
void getdata();
};
class second {
public :
void disp(class rst temp);
};
inline void rst:: getdata()
{
cout << “ enter a number ? \n”;
cin >> x;
}
inline void second ::disp(class rst temp)
{
cout << “ entered number is = “ << temp.x ;
cout << endl;
}
int main()
{
rst objx;
second objy;
objx.getdata();
objy.disp(objx);
return 0;
}
Output of the above program
enter a number?
10
entered number is = 10
A following program will not be compiled due to an error
//granting friendship with another class
#include <iostream>
using namespace std;
class rst {
friend class second;
public :
void display(second objb)
{
cout << “ object of second = ” << objb.y << endl;
}
};
class second {
private:
int y;
public :
void getdata()
{
cout << “ enter a value for y = \n”;
cin >> y;
}
};
int main()
{
rst objx;
second objy;
objy.getdata();
objx.display(objy);
return 0;
}
Compile time error
forward declaration of class second
Though the class fi rst has granted its friendship to the class second, it cannot access the private data of the class second through its public member function display () of the class fi rst.

Example 7.
A program to calculate the sum of private data of the class fi rst with a private data of another class second through the common friend function.

//friend function is same for both classes
#include <iostream>
using namespace std;
class second; //forward declaration
class rst {
private :
int x;
public :
inline void getdata();
inline void display();
friend int sum( rst,second);
};
class second {
private :
int y;
public :
inline void getdata();
inline void display();
friend int sum( rst,second);
};
inline void rst :: getdata()
{
cout << “ enter a value for x \n”;
cin >> x;
}
inline void second :: getdata()
{
cout << “ enter a value for y \n”;
cin >> y;
}
inline void rst :: display()
{
cout << “ entered number is (x) = “;
cout << x << endl;
}
inline void second :: display()
{
cout << “ entered number is (y) = “;
cout << y << endl;
}
int sum ( rst one, second two)
{
int temp;
temp = one.x+two.y;
return(temp);
}
int main()
{
rst a;
second b;
a.getdata();
b.getdata();
a.display();
b.display();
int te = sum(a,b);
cout << “ sum of the two private data variables (x+y)”;
cout << “ = “ << te << endl;
return 0;
}
Output of the above program
enter a value for x
10
enter a value for y
20
entered number is (x) = 10
entered number is (y) = 20
sum of the two private data variables (x+y) = 30

Note: In the above example, the function sum () needs to have the right to access for fetching the private data members of the both classes in order to add the contents together. Therefore, the function takes formal arguments of the both classes.

friend int sum( rst,second);

0 comments

Post a Comment