C++ Program Using Friend Function

Friend function is used to access private, public and protected data’s of a class from outside of the class. In common the function that is not a member of the class cannot access the data’s of a class.

This program can 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.



Class sample



Int x;


Void getdata (0;

Friend void display (class sample);


Void sample:: getdata ()


Cout<<”\n Enter a value of the x”;



Void display (class sample abc)


Cout<<”\n Entered number is”<<abc.x;


Void main ()


Sample obj;

Obj. getdata ();

Cout<<”\n accessing the private data by non member function”;

Display (obj);

getch ();


Related posts:

  1. Friend Function Friend function The concept of encapsulation and data hiding indicates...
  2. Inline Function Inline Function The inline function is designed to speed up...
  3. Overriding Member Function in C++ Overriding member function We can define data member and member...
  4. C++ Program Using getdata() and display() The program uses member function getdata() and display() in-order to...
  5. C++ Program to Find Sum and Average Using Class Class is the expanded concept of data structure, instead of...