Friend Class in C++ with example



Friend class is basically used to access the private information of another class.

Example:
#include<iostream.h>
#include<conio.h>
Class employee
               {
                              private:
                              int id;
                              char name[20];
                              public:
                              void getdata();
                              friend class manager;
               };
void employee :: getdata()
{
cout<<”Enter the employee id & name =”;
cin>>id>>name;    
}
class manager
{
public:
void put (employee t);
};
void manager :: put (employee t)
{
   cout<<” Employee Id = ”<<t.id;
               cout<<” Employee Name = ”<<t.name;
}
void main()
{
   clrscr();
               employee e;
               manager m;
               e.getdata();
               m.put(e);
               getch();
}

No comments:

Post a Comment