Create friend classes - C++ Class

C++ examples for Class:friend

Description

Create friend classes

Demo Code

#include <iostream>
using namespace std;
class alpha/*from   w  ww.  j  a  v  a2  s . c  o  m*/
{
   private:
   int data1;
   public:
   alpha() : data1(99) {  }   //constructor
   friend class beta;         //beta is a friend class
};
class beta
{                             //all member functions can
    public:                       //access private alpha data
    void func1(alpha a)  { cout << "\ndata1=" << a.data1; }
    void func2(alpha a)  { cout << "\ndata1=" << a.data1; }
};
int main()
{
    alpha a;
    beta b;
    b.func1(a);
    b.func2(a);
    cout << endl;
    return 0;
}

Result


Related Tutorials