Page 108 - DCAP404 _Object Oriented Programming
P. 108

Unit 5: Static Members




          You should see the following output.                                                  Notes
          Enter the value of x
          4
          Display the entered number 4

          There are also other areas of application for friend function. The friend function can also be
          defined within the scope of a class definition itself. Friend function may also have inline member
          functions. If the friend function is defined in the class, then the inline code substitution is done
          automatically. But if defined outside the class, then it is necessary to precede the return type with
          the keyword inline to make the inline code substitution.
          The following program accesses the private data of a class through a friend function where the
          friend function is defined with inline code substitution.

          #include  <conio.h>
          class  example
          {
          private:
          int  x;
          public:

          inline  void  getdata();
          friend  void  disp(example);
          };
          inline  void  example::getdata()
          {
          cout  <<“Enter  the  value  of  x  “  <<  “\n”;
          cin  >>  x;
          }
          inline  void  disp(example  eg)     //Note  the  use  of  the  keyword  inline

          {
                         cout  <<  “Display  the  entered  number”  <<  eg.x  <<  “\n”;
          }
          main()
          {
                         example  eg1  ;
                         eg1.getdata();

                         disp(eg1);
          getche();
          }








                                           LOVELY PROFESSIONAL UNIVERSITY                                   101
   103   104   105   106   107   108   109   110   111   112   113