Create friend function in class

Friday, October 23, 2015 Unknown 0 Comments


To create freind function of class

  • Definition:-
            Friend function is non-member function of class but still that access the private data of that class.

Friend function is declared in class but it not necessary object of class to call it call without object like simple functions.


#include<iostream.h>
#include<conio.h>
class ABC
{
        int a;
        Public:
        void get()
        {
               cout<<"enter value of A : ";
               cin>>a;
        }
        friend void show(ABC &);
};
void show(ABC &i)
{
      cout<<"A = "<<i.a;
}
void main()
{
       ABC A1;
       A1.get();
       show(&A1);
       getch();
}


OUTPUT:-
enter value of A : 10
A = 10

         

To know about God and Godly University please click here : www.pbks.info

0 comments: