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.
#include<iostream.h>
#include<conio.h>
Class sample
{
Private:
Int x;
Public:
Void getdata (0;
Friend void display (class sample);
};
Void sample:: getdata ()
{
Cout<<”\n Enter a value of the x”;
Cin>>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:
- Friend Function Friend function The concept of encapsulation and data hiding indicates...
- Inline Function Inline Function The inline function is designed to speed up...
- Overriding Member Function in C++ Overriding member function We can define data member and member...
- C++ Program Using getdata() and display() The program uses member function getdata() and display() in-order to...
- C++ Program to Find Sum and Average Using Class Class is the expanded concept of data structure, instead of...