Page 110 - DCAP404 _Object Oriented Programming
P. 110

Unit 5: Static Members




                         cout  <<“Entered  number  is  “;                                       Notes
                         cout  <<  obj.x;
          }
          void  main()
          {
                         one  obj1;
                         two  obj2;

                         obj1.getdata();
                         obj2.disp(objl);
          getche();
          }
          You should see the output as shown below:
          Enter a number 25

          Entered number is 25
          Though the class first has granted its friendship to the class second, it cannot access the private
          data of the class second through its public member function display( ) of the class first.

          A non-member function can be friendly with one or more classes. When a function has declared
          to have friendship with more than one class, the friend classes should have forward declaration
          as it needs to access the private members of both classes.

          The general syntax of declaring the same friend function with more than one class is:
          class  second;
          class  first
          {
          private:
                 //data  members;
                 //member  functions;
          public:  .
                 //data  members;
                 //member  functions;
                     friend  <return_type>  <fname>(class  first,  class  second.  .  ..);
          };
          class  second
          {
          private:
                 //data  members;
                 //member  functions;
          public:
                 //data  members;





                                           LOVELY PROFESSIONAL UNIVERSITY                                   103
   105   106   107   108   109   110   111   112   113   114   115