Page 80 - DCAP404 _Object Oriented Programming
P. 80

Unit 4: Classes and Objects




                         void  salary(  );                                                      Notes
                         void  get_info(  );
                         void  display_info(  );
                 };
                 employee x, y;  //creates x and y - two objects of the class employee
          By now it should be clear to you that you can use a class just as you use a data type. In fact you can
          also create an array of objects of a particular class.



             Did u know?  What is the difference between defining a class member function completely
             within its class or to include only the prototype and later its definition?

             The only difference between defining a class member function completely within its class
             or to include only the prototype and later its definition, is that in the first case the function
             will automatically be considered an inline member function by the compiler, while in the
             second it will be a normal (not-inline) class member function, which in fact supposes no
             difference in behavior.

          Self Assessment

          Fill in the blanks:
          1.   A class is an expanded concept of a data structure: instead of holding only data, it can hold
               both data and…………………. .

          2.   By default, all members of a class declared with the ………………….. keyword have private
               access for all its members.
          3.   All is very similar to the declaration on data structures, except that we can now include
               also functions and members, but also this new thing called …………………... .
          4.   A class provides a template, which defines the member functions and ………………… that
               are required for objects of a class type.
          5.   A class must be defined prior to the class …………………... .

          4.2 Defining Member Functions

          We have learnt to declare member functions. Let us see how member functions of a class can be
          defined within a class or outside a class.
          A member function is defined outside the class using the :: (double colon symbol) scope resolution
          operator. The general syntax of the member function of a class outside its scope is:
             <return_type>  <  class_name>::  <member_function>(arg1,  arg2....argN)
          The type of member function arguments must exactly match with the types declared in the class
          definition of the <class_name>. The Scope resolution operator (::) is used along with the class
          name in the header of the function definition. It identifies the function as a member of a particular
          class. Without this scope operator the function definition would create an ordinary function,
          subject to the usual function rules of access and scope.








                                           LOVELY PROFESSIONAL UNIVERSITY                                   73
   75   76   77   78   79   80   81   82   83   84   85