Page 105 - DCAP404 _Object Oriented Programming
P. 105

Object-oriented Programming




                    Notes              printx  ();
                                       }
                                       }
                                   2.  A static object is really just an encapsulation of data and related functions.
                                   3.  A static object does not have to have a main function, but to be useful it typically has at
                                       least one public function that can be called from somewhere else.




                                      Task  By default, an object or variable that is defined outside all blocks has static duration
                                     and external linkage. Justify.

                                   Self Assessment

                                   Fill in the blanks:
                                   6.  Static duration means that the object or variable is allocated when the program starts and
                                       is …………………………. when the program ends.
                                   7.  You cannot declare the members of a ………………… as static.
                                   8.  The  threads invoking  methods  on  static  objects  must  wait  until the  object  is  fully
                                       ……………………. .
                                   9.  A class  is a collection of ……………………….., which  are variables  of various  types,
                                       including other classes.
                                   10.  A member function definition begins with the name of the class, followed by two colons,
                                       the name of the………………………………, and its parameters.

                                   5.4 Friend Functions


                                   Object oriented programming paradigm  secures data because of  the data hiding and  data
                                   encapsulation features. Data members declared as private in a class are restricted from access by
                                   non-member functions. The private data values can be neither read nor written by non-member
                                   functions. Any attempt made directly to access these members, will result in an error message as
                                   “inaccessible data-type” during compilation.
                                   The best way to access a private data member by a non-member function is to change a private
                                   data member to a  public group.  But this goes against  the concept  of data hiding and data
                                   encapsulation. A special mechanism available known as friend function allows non-member
                                   functions to access private data. A friend function may be either declared or defined within the
                                   scope of a class definition. The keyword friend informs the compiler that it is not a member
                                   function nor the property of the class.

                                   The general syntax of the friend function is:
                                          friend  <return_type>  <function_name>(argument  list);
                                   friend is a keyword. A friend declaration is valid only within or outside the class definition. The
                                   following code snippet shows how a friend function is defined.
                                   class  sample
                                   {




          98                                LOVELY PROFESSIONAL UNIVERSITY
   100   101   102   103   104   105   106   107   108   109   110